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 .两个主机都 ...
随机推荐
- 访问其他电脑的c盘
访问其他电脑的c盘 \\192.168.0.1\C$
- Glib之主事件循环
介绍 GLib和GTK+应用的主事件循环管理着所有事件源.这些事件的来源有很多种比如文件描述符(文件.管道或套接字)或超时.新类型的事件源可以通过g_source_attach()函数添加. 为了让多 ...
- 正经学C#_循环[do while,while,for]:[c#入门经典]
在c#中循环语句总共三种,do...while ,while,for这三种语句. 循环语句,是为了解决一些繁琐的计算.比如输出0-10这10个数字. 在不循环的情况下你可以能这么写 Console.W ...
- 区块链中的密码学(-)区块链中运用最广的散列算法-SHA256算法分析与实现
在很多技术人员的眼中,区块链并不是一种新的技术,而是过去很多年计算机技术的组合运用.而在这个方方面面技术的运用上,基于密码学的加密算法可以说是区块链各种特点得以表现的根本,一旦目前使用的加密算法被证实 ...
- 【Python OpenGL】【2】第一个三角形(Pyopengl)
根据顶点缓存来生成图元(Python OpenGL) 原文(英文链接)http://ogldev.atspace.co.uk/www/tutorial03/tutorial03.html __auth ...
- luogu3380 树套树之线段树套线段树
个人感觉可能是最不需要脑子写的方法 不过也不太好调 就是用一个普通的线段树维护这个序列,但是对于线段树的每一个区间,再开一个动态开点的权值线段树,里面存储这个区间所有元素值 单点修改只会涉及到log棵 ...
- BestCoder Round #80 待填坑
Lucky Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- 【笔记】MySQL的基础学习
[笔记]MySQL的基础学习 老男孩 MySQL 一 安装与配置 1 下载安装 官网:http://dev.mysql.com/downloads/mysql/ 下载相应版本的压缩包 解压压缩包至任 ...
- Django之ORM其他骚操作 执行原生SQl
Django ORM执行原生SQL # extra # 在QuerySet的基础上继续执行子语句 # extra(self, select=None, where=None, params=Non ...
- java的Spring学习1--spring引用及属性(setter,getter)注入
1.目录结构 项目->src->main->java->com.test.ppmoney 设置java文件夹为代码 在Project Structure 里的 Modules ...