Jetty数据同步使用
1. Jetty简介
Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境。Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布。开发人员可以将Jetty容器实例化成一个对象,可以迅速为一些独立运行(stand-alone)的Java应用提供网络和web连接
2. 项目工作中的使用
近期项目中需要同步业务方的相关数据,因此打算使用Jetty完成此目标,具体的代码信息如下:
(1) 封装Jetty的服务类
public class JettyServerController {
private Server jettyServer;
private int jettyServerPort = 1999;
private String contextPathString;
private String startPageString;
private boolean jettyDirectoryListed = false;
private HashMap<String, Servlet> servletInstanceHashMap = new HashMap<String, Servlet>();
/**
* Creates an instance of the jettyServer on the portJettyServer
*
* @param jettyServerPort
*/
public JettyServerController( int jettyServerPort, String startPageString, String contextPathString, boolean jettyDirectoryListed )
{
// set the start page string
if (startPageString != null)
this.startPageString = startPageString;
// set the context path string
if (contextPathString != null)
this.contextPathString = contextPathString;
// set the directory listing property
this.jettyDirectoryListed = jettyDirectoryListed;
// set the jetty operating port
this.jettyServerPort = jettyServerPort;
// bounds check and assign the input port
if ( jettyServerPort < 65536 && jettyServerPort > 1000) {
this.jettyServerPort = jettyServerPort;
}
// create a new jetty server instance
jettyServer = new Server();
// Set up a channel selector object
SelectChannelConnector connector = new SelectChannelConnector();
// set the port number
connector.setPort( this.jettyServerPort );
// add the connector to the server
jettyServer.addConnector( connector );
}
public void setHandler(ContextHandlerCollection contexts) {
jettyServer.setHandler(contexts);
}
public void startAndLaunchBrowser() throws Exception {
// Create the resource handler
ResourceHandler resource_handler = new ResourceHandler();
// disallow directory listing
resource_handler.setDirectoriesListed( this.jettyDirectoryListed );
// Set the initial load file
resource_handler.setWelcomeFiles(new String[] { this.startPageString });
// point to the local directory
resource_handler.setResourceBase(".");
// use sessions
ServletContextHandler servletContextHandler = new ServletContextHandler(
ServletContextHandler.SESSIONS);
// set /web as the default path
servletContextHandler.setContextPath( this.contextPathString );
// add the handler
jettyServer.setHandler( servletContextHandler );
// get the servlets
for (String servletPath : servletInstanceHashMap.keySet()) {
// add a servlet
servletContextHandler.addServlet(new ServletHolder(
servletInstanceHashMap.get( servletPath )), servletPath);
}
// create a handler list
HandlerList handlers = new HandlerList();
// add three handlers
handlers.setHandlers( new Handler[] { servletContextHandler,
resource_handler, new DefaultHandler() });
// pass the handlers to the server
jettyServer.setHandler(handlers);
// start the session
jettyServer.start();
}
public void stop() throws Exception {
jettyServer.stop();
jettyServer.join();
}
public void join() throws InterruptedException{
jettyServer.join();
}
public boolean isStarted() {
return jettyServer.isStarted();
}
public boolean isStopped() {
return jettyServer.isStopped();
}
public void addServlet( String servletPath, Servlet servlet )
{
servletInstanceHashMap.put( servletPath, servlet);
}
public void removeServlet ( String servletPath )
{
servletInstanceHashMap.remove( servletPath );
}
}
(2) 插入Servlet组件
public class ServletHelper {
private static final Logger logger = LoggerFactory.getLogger(ServletHelper.class);
public static void main(String[] args) throws Exception {
initJetty(8080);
}
public static void initJetty(int port) throws Exception {
// Create a new JettyServerController
JettyServerController controller = new JettyServerController(port,"/query","/",true);
controller.addServlet("/test", new TestServlet());
// start the session and launch the default page
controller.startAndLaunchBrowser();
System.in.read();
controller.stop();// stop the local Jetty ajax services
}
}
(3) Servlet业务功能
接收业务方POST请求发送的数据,并存储至数据库
public class TestServlet extends HttpServlet{
private static final long serialVersionUID = -2385860009337093194L;
private static final int SUCCESS = 0;
private static final int FAILED = -1;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("application/json; charset=utf-8");
PrintWriter out = resp.getWriter();
int resultCode = -1;
String info = "";
// 获取参数
String para = "";
try {
para = req.getParameter("para");
} catch (Exception e) {
}
if(para.trim().length() == 0){
resultCode = FAILED;
}else{
byte[] buffer = getRequestPostBytes(req);
if(buffer == null || buffer.length == 0){
resultCode = FAILED;
}else{
String charEncoding = req.getCharacterEncoding();
if(charEncoding == null) charEncoding = "UTF-8";
info = new String(buffer,charEncoding);
// 将获取的信息更新至数据库
......
}
}
JSONObject result = new JSONObject();
result.put("resultCode",resultCode);
result.put("content",info);
resp.addHeader("resultCode",""+resultCode);
out.print(result.toString());
LogUtil.info(result.toString());
}
byte[] getRequestPostBytes(HttpServletRequest request) throws IOException{
int length = request.getContentLength();
if(length == 0){
return null;
}
byte buffer[] = new byte[length];
for(int i = 0; i < length;){
int readLen = request.getInputStream().read(buffer, i, length-i);
if(readLen == -1) break;
i += readLen;
}
return buffer;
}
}
Jetty数据同步使用的更多相关文章
- solr 简单搭建 数据库数据同步(待续)
原来在别的公司负责过文档检索模块的维护(意思就是不是俺开发的啦). 所以就略微接触和研究了下文档检索. 文档检索事实上是全文检索.是通过一种技术把N多文档进行一定规律的分割归类,然后创建易于搜索的索引 ...
- 增量数据同步中间件DataLink分享(已开源)
项目介绍 名称: DataLink['deitə liŋk]译意: 数据链路,数据(自动)传输器语言: 纯java开发(JDK1.8+)定位: 满足各种异构数据源之间的实时增量同步,一个分布式.可扩展 ...
- 大数据实践-数据同步篇tungsten-relicator(mysql->mongo)
// mongo)";digg_bgcolor = "#FFFFFF";digg_skin = "normal"; // ]]> // [导读] ...
- postman使用之二:数据同步和创建测试集
数据同步 启动postman 后在右上角可以登录账号,登录后就可以同步自己的api测试脚本,连上网在办公区在家都可以同步. 创建测试集 1.点击collections,点击add folder 2.c ...
- C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 能支撑10万以上客户端的数据同步下载问题
庞大的业务系统,特别是需要有离线作业操作支持的核心业务系统,需要有强大的基础数据同步功能,基础数据有在增加.有在变动.有在失效,同时有大量的客户端全天侯的在连接服务器.不间断的在处理核心数据. 经过2 ...
- C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 大型软件系统客户端数据同步的问题解决
作为一个完整的整体信息化解决方案需要有足够强大的各种功能,这些功能相对独立,又互相依存.当有需要这样的功能时可以随时拿出来用,适当修改一下就可以满足要求.只有这样才能快速开发各种信息化系统,才能满足各 ...
- rsync数据同步备份
一.rsync简介 (1)rsync是什么? rsync是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据同步备份的优秀工具. (2)rsync作用比较 远程拷贝:有点类似ssh的scp ...
- Windows远程数据同步工具cwRsync
1. cwRsync简介cwRsync是Rsync在Windows上的实现版本,Rsync通过使用特定算法的文件传输技术,可以在网络上传输只修改了的文件.cwRsync主要用于Windows上的远程文 ...
- 怎么通过 Mysql 实现数据同步呢?
怎么使 mysql 数据同步先假设有主机 A 和 B ( linux 系统),主机 A 的 IP 分别是 1.2.3.4 (当然,也可以是动态的),主机 B 的 IP 是 5.6.7.8 .两个主机都 ...
随机推荐
- jmeter接口参数化获取tocken后保存批量保存在本地
jmeter目录结构如下: 1,读取文件配置的ID提取tocken 2,CSV 数据文件设置,第一个为文件目录,第二个为参数化的参数名. 3,正则表达式提取tocken 4,BeanShell Pos ...
- kolla-build镜像时,问题汇总
记录下kolla-build镜像时,遇到的一些问题,既为了方便自己以后问题的查找,也为了帮助别人避免踩这些坑.遇到的问题会持续更新在博客里面. 问题1:使用的kolla 版本是ocata版本,本地已经 ...
- kolla出现问题时的定位方式
前提,对官网问题的一个翻译 Troubleshooting Guide排障手册 1.Failures(失败) If Kolla fails, often it is caused by a CTRL- ...
- console的使用
一.显示信息的命令 console.log("normal"); // 用于输出普通信息 console.info("information"); // 用于输 ...
- dmp文件恢复oracle数据库
–创建用户 create user anhui identified by anhui -给予用户权限 grant create session to anhuigrant connect,resou ...
- dede地图显示最新文章的解决方法
以DEDECMS5.6为例:sitemap.htm 在/templets/plus/目录里,就算添加了织梦相关标签调用,但却不能显示文章. 这是因为makehtml_map.php不能解析织梦的相关调 ...
- asp.net core 自定视图主题 实现IViewLocationExpander接口
新建ThemeViewLocationExpander.cs 实现IViewLocationExpander接口 /// <summary> /// 自定视图主题 实现IViewLocat ...
- CF796D Police Stations 思维
Inzane finally found Zane with a lot of money to spare, so they together decided to establish a coun ...
- CoreAnimation 核心动画 / CABasicAnimation/ CAKeyframeAnimation
- (void)createBaseAnimation{ //基础动画 CABasicAnimation *animation = [CABasicAnimation animation]; anim ...
- for别名
name:for(int i =0 ;i< a.length;i++){ System.out.println(i); for(int j =0;j<i;j++){ continue na ...