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 .两个主机都 ...
随机推荐
- .Net Core Api 使用版本控制
1,安装Microsoft.AspNetCore.Mvc.Versioning NET Core Mvc中,微软官方提供了一个可用的Api版本控制库Microsoft.AspNetCore.Mvc.V ...
- 对XML文档进行修改
怎样对XML文档时行修改.Insus.NET在此举个简单的例子.XML文档,就以这篇博文:http://www.cnblogs.com/insus/p/3274220.html 如果我们想对其中一个节 ...
- 四步走查智能硬件异常Case
此文已由作者于真真授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 相比于软件,智能硬件产品由于涉及硬件和软件两个端的状态,其异常case要更加错综复杂.由于硬件产品的迭代更新 ...
- Luogu2114 [NOI2014]起床困难综合症 【位运算】
题目分析: 按位处理即可 代码: #include<bits/stdc++.h> using namespace std; ; int n,m; int a[maxn],b[maxn]; ...
- [SinGuLaRiTy] NOIP膜你赛-Day 1
[SinGuLRiTy-1022] Copyright (c) SinGuLaRITy 2017. All Rights Reserved. 对于所有题目:Time Limit:1s || Memo ...
- 使用pycharm创建自己的第一个django项目
PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理.代码跳转.智能提示.自动完成.单元测试.版本控制. ...
- 最长双回文串——manacehr
题目 [题目描述] 顺序和逆序读起来完全一样的串叫做回文串.比如 acbca 是回文串,而 abc 不是(abc 的顺序为 “abc”,逆序为 “cba”,不相同).输入长度为 n 的串 S,求 S ...
- 洛谷P3515 [POI2011]Lightning Conductor(决策单调性)
题意 已知一个长度为n的序列a1,a2,...,an. 对于每个1<=i<=n,找到最小的非负整数p满足 对于任意的j, aj < = ai + p - sqrt(abs(i-j)) ...
- 洛谷P3507 [POI2010]GRA-The Minima Game
题目描述 Alice and Bob learned the minima game, which they like very much, recently. The rules of the ga ...
- Java 标准IO高可用类
输出: 使用: * <pre> * public class TestStdOut { * public static void main(String[] args) { * int a ...