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 .两个主机都 ...
随机推荐
- Failed to export using the options you specified. Please check your options and try again
参考这篇<从ASP.NET传递参数给水晶报表> http://www.cnblogs.com/insus/p/3281114.html 是可以传递参了.但是点击报表的菜单条上的打印图标没 ...
- c# 字符串填充占位
C# 字符串PadLeft函数的使用 1.Demo: 需求: 将111改变成0000111 使用字符串PadLeft函数可以解决: int num = 111; string s= num.ToSt ...
- bsdasm
bsdasm 来源 http://www.int80h.org/bsdasm/ Preface by G. Adam StanislavWhiz Kid Technomagic Assembly la ...
- 解决vue-cli相对路径问题 about css assert path ,two Solution(css路径的问题解决方案) #179
https://github.com/vuejs/vue-cli/issues/179
- P1900 自我数
题意: 对于每一个正整数n,我们定义d(n)为n加上它每一位数字的和. 例如,d(75)=75+7+5=87.给定任意正整数n作为一个起点,都能构造出一个无限递增的序列:n, d(n), d(d(n) ...
- 15.Subtree of Another Tree(判断一棵树是否为另一颗树的子树)
Level: Easy 题目描述: Given two non-empty binary trees s and t, check whether tree t has exactly the s ...
- 下载GitHub仓库的某个子文件夹
http://downgit.zhoudaxiaa.com/#/home
- C语言中typedef的解释_2
typedef工具是一个高级数据特性.利用typedef可以为某一类型自定义一个新的名称.这样可以提高程序的可读性,可移植性,向用户表明特定用途. typedef没有创建任何新的类型,它只是为某个已存 ...
- BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP
Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧 ...
- CF D. Fair(思维+DFS)
http://codeforces.com/contest/987/problem/D 题目大概: 给出一个n个城镇m条边的图,给出每个城镇拥有的特产(可能多个城镇有相同特产).有k种不同特产. 要求 ...