jetty 9 嵌入式开发示例

jetty 9 嵌入应用程序后,小型的web应用直接打成一个单独的jar包,就可以直接运行,非常适合做Demo演示或云端集群部署。

主要代码:

JettyServer的封装类

 1 package yjmyzz.jetty.demo.server;
 2
 3 import org.eclipse.jetty.server.*;
 4 import org.eclipse.jetty.server.handler.HandlerCollection;
 5 import org.eclipse.jetty.server.handler.RequestLogHandler;
 6 import org.eclipse.jetty.server.handler.gzip.GzipHandler;
 7 import org.eclipse.jetty.util.thread.QueuedThreadPool;
 8 import org.eclipse.jetty.util.thread.ThreadPool;
 9 import org.eclipse.jetty.webapp.WebAppContext;
10 import org.slf4j.LoggerFactory;
11
12 import java.io.File;
13
14 public class JettyWebServer {
15
16     private static org.slf4j.Logger logger = LoggerFactory.getLogger(JettyWebServer.class);
17
18     private Server server;
19     private int port;
20     private String host;
21     private String tempDir;
22     private String logDir;
23     private String webDir;
24     private String contextPath;
25
26
27     public JettyWebServer(int port, String host, String tempDir, String webDir, String logDir, String contextPath) {
28
29         logger.info("port:{},host:{},tempDir:{},webDir:{},logDir:{},contextPath:{}", port, host, tempDir, webDir, logDir, contextPath);
30
31         this.port = port;
32         this.host = host;
33         this.tempDir = tempDir;
34         this.webDir = webDir;
35         this.contextPath = contextPath;
36         this.logDir = logDir;
37     }
38
39     public void start() throws Exception {
40         server = new Server(createThreadPool());
41         server.addConnector(createConnector());
42         server.setHandler(createHandlers());
43         server.setStopAtShutdown(true);
44         server.start();
45     }
46
47     public void join() throws InterruptedException {
48         server.join();
49     }
50
51
52     private ThreadPool createThreadPool() {
53         QueuedThreadPool threadPool = new QueuedThreadPool();
54         threadPool.setMinThreads(10);
55         threadPool.setMaxThreads(100);
56         return threadPool;
57     }
58
59
60     private NetworkConnector createConnector() {
61         ServerConnector connector = new ServerConnector(server);
62         connector.setPort(port);
63         connector.setHost(host);
64         return connector;
65     }
66
67     private HandlerCollection createHandlers() {
68         WebAppContext context = new WebAppContext();
69         context.setContextPath(contextPath);
70         context.setWar(webDir);
71         context.setTempDirectory(new File(tempDir));
72
73
74         RequestLogHandler logHandler = new RequestLogHandler();
75         logHandler.setRequestLog(createRequestLog());
76         GzipHandler gzipHandler = new GzipHandler();
77         HandlerCollection handlerCollection = new HandlerCollection();
78         handlerCollection.setHandlers(new Handler[]{context, logHandler, gzipHandler});
79         return handlerCollection;
80     }
81
82     private RequestLog createRequestLog() {
83         //记录访问日志的处理
84         NCSARequestLog requestLog = new NCSARequestLog();
85         requestLog.setFilename(logDir + "/yyyy-mm-dd.log");
86         requestLog.setRetainDays(90);
87         requestLog.setExtended(false);
88         requestLog.setAppend(true);
89         //requestLog.setLogTimeZone("GMT");
90         requestLog.setLogTimeZone("Asia/Shanghai");
91         requestLog.setLogDateFormat("yyyy-MM-dd HH:mm:ss SSS");
92         requestLog.setLogLatency(true);
93         return requestLog;
94     }
95
96 }

View Code

启动代码示例:

  1 package yjmyzz.jetty.demo.main;
  2
  3 import org.slf4j.Logger;
  4 import org.slf4j.LoggerFactory;
  5 import org.springframework.util.StringUtils;
  6 import yjmyzz.jetty.demo.server.JettyWebServer;
  7 import yjmyzz.jetty.demo.util.FileUtil;
  8 import yjmyzz.jetty.demo.util.JarUtils;
  9
 10 import java.util.HashMap;
 11 import java.util.Map;
 12
 13 public class JettyApp {
 14
 15     private static final String PORT = "port";
 16     private static final String WEB_DIR = "web";
 17     private static final String LOG_DIR = "log";
 18     private static final String TEMP_DIR = "temp";
 19     private static final String CONTEXT_PATH = "context";
 20     private static final String HOST = "host";
 21     private static final Map<String, String> param = new HashMap<>();
 22     private static Logger logger = LoggerFactory.getLogger(JettyWebServer.class);
 23
 24
 25     public static void main(String... anArgs) throws Exception {
 26
 27         if (anArgs.length == 0) {
 28             param.put(PORT, "8080");
 29             param.put(WEB_DIR, "web");
 30             param.put(LOG_DIR, "logs");
 31             param.put(TEMP_DIR, "temp");
 32             param.put(CONTEXT_PATH, "/demo");
 33             param.put(HOST, "localhost");
 34         }
 35
 36
 37         for (String arg : anArgs) {
 38             System.out.println(arg);
 39             if (!StringUtils.isEmpty(arg) && arg.contains("=")) {
 40                 String[] t = arg.trim().split("=");
 41                 param.put(t[0], t[1]);
 42             }
 43         }
 44
 45         initParam();
 46
 47         unzipSelf();
 48
 49         new JettyApp().start();
 50     }
 51
 52
 53     private static void initParam() {
 54
 55
 56         String logDir = FileUtil.currentWorkDir + param.get(LOG_DIR);
 57         String tempDir = FileUtil.currentWorkDir + param.get(TEMP_DIR);
 58         String webDir = FileUtil.currentWorkDir + param.get(WEB_DIR);
 59
 60         logger.debug(logDir);
 61         logger.debug(tempDir);
 62         logger.debug(webDir);
 63
 64         String temp = "x.x";//占位
 65         FileUtil.createDirs(logDir + "/" + temp);
 66         FileUtil.createDirs(tempDir + "/" + temp);
 67         FileUtil.createDirs(webDir + "/" + temp);
 68
 69         param.put(LOG_DIR, logDir);
 70         param.put(TEMP_DIR, tempDir);
 71         param.put(WEB_DIR, webDir);
 72     }
 73
 74     private JettyWebServer server;
 75
 76     public JettyApp() {
 77         server = new JettyWebServer(
 78                 Integer.parseInt(param.get(PORT).toString()),
 79                 param.get(HOST),
 80                 param.get(TEMP_DIR),
 81                 param.get(WEB_DIR),
 82                 param.get(LOG_DIR),
 83                 param.get(CONTEXT_PATH));
 84     }
 85
 86     public void start() throws Exception {
 87         server.start();
 88         server.join();
 89     }
 90
 91     private static void unzipSelf() {
 92         //将jar自身解压
 93
 94         String selfPath = FileUtil.getJarExecPath(JettyApp.class);
 95         if (selfPath.endsWith(".jar")) {
 96             // 运行环境
 97             try {
 98                 logger.info("正在将\n" + selfPath + "\n解压至\n" + param.get(WEB_DIR));
 99                 JarUtils.unJar(selfPath, param.get(WEB_DIR));
100             } catch (Exception e) {
101                 logger.error("解压web内容失败!", e);
102             }
103         } else {
104             // IDE环境
105             param.put(WEB_DIR, selfPath);
106         }
107         logger.info(selfPath);
108     }
109 }

View Code

我在github上开源了一个jetty9 + spring mvc4 + velocity2的示例项目,地址:https://github.com/yjmyzz/jetty-embed-demo

时间: 2024-08-02 10:29:38

jetty 9 嵌入式开发示例的相关文章

【详解】嵌入式开发中固件的烧录方式

版本:v1.2   Crifan Li 摘要 本文主要介绍了嵌入式开发过程中,将固件从PC端下载到开发板中的各种方式,主要包括NFS挂载,Nand Flash和Nor Flash,USB,RS232,网卡NIC等方式. 本文提供多种格式供: 在线阅读 HTML HTMLs PDF CHM TXT RTF WEBHELP 下载(7zip压缩包) HTML HTMLs PDF CHM TXT RTF WEBHELP HTML版本的在线地址为: http://www.crifan.com/files/

《测试驱动的嵌入式C语言开发》——1.6节对于嵌入式开发的益处

1.6 对于嵌入式开发的益处嵌入式软件开发面临所有"通常意义上"的软件开发的挑战.例如很难把进度计划做得好且可靠.但嵌入式软件开发也有其自身特有的更多挑战.这并不意味着嵌入式开发不能采用TDD.嵌入式开发者最常引用的借口是嵌入式代码依赖于硬件.依赖关系对于非嵌入式代码也是个大问题.幸运的是,我们有办法来解决这些依赖问题.原则上讲,对硬件设备的依赖和对数据库的依赖没什么区别.嵌入式开发者面临很多挑战,我们将展开讨论如何从TDD借力.嵌入式开发者不仅能收到前面讲过的那些非嵌入式开发者能享受

SharePoint 2013 APP开发示例 (一)List 读写

在这个示例里,我们将创建一个页面测试 SharePoint APP的权限.这个页面有二个按钮,一个从documents里读数据,一个往documents里写数据: 1. 打开Visual Studio 2012,创建一个新的 Sharepoint 2013 app: PermissionTest,选择 Sharepoint-hosted,点击Finish 开发示例 (一)List 读写-sharepoint list"> 2. 打开Default.aspx : 引入knockoutjs &

Windows窗体控件开发示例:扩展TreeView

摘要:讲述了如何向 TreeView 控件添加数据绑定功能,它是一系列 Microsoft Windows 控件开发示例之一.您可以将本文与相关的概述文章结合起来阅读. 简介 在可能的情况下,您应该先使用些现成的控件:因为提供的 Microsoft Windows 窗体控件中包含大量编码和测试成果,如果您要放弃它们从头开始,无疑是一种巨大的浪费.基于此,在本例中,我将继承一个现有 Windows 窗体控件 TreeView ,然后对其进行自定义.在下载该 TreeView 控件的代码时,您还会得

Android SurfaceView游戏开发示例

当我们需要开发一个复杂游戏的时候,而且对程序的执行效率要求很高时,View类就不能满足需求了,这时必须用 SurfaceView类进行开发. 例如,对速度要求很高的游戏时,View类就不能满足需求了,这时必须使用SurfaceView类进 行开发.例如,对速度要求很高的游戏,可以使用双缓冲来显示.游戏中的背景.人物.动画等都需要绘制在一个画布(Canvas) 上,而SurfaceView可以直接访问一个画布,SurfaceView 是提供给需要直接画像素而不是使用窗体部件的应用使用的. 每个 S

XP下超级终端与嵌入式开发板交互技巧

一.简介 超级终端是Windows操作系统自带的一个通用的串行交互软件,可以通过这个工具对路由器交换机等进行配置.使用调制解调器.一条零调制解调电缆或以太网连接,再调用此程序能够连接到其他计算机.Telnet 站点.公告板系统 (BBS).联机服务和主机.我们可以用它来调试电路是否可行. 嵌入式开发板基本都有串口,可以通过超级终端与嵌入式系统的串口交互,使超级终端成为嵌入式系统的"显示器". 使用:开始→程序→附件→通讯→超级终端(可新建或者使用现有的连接对设备进行配置); 启动命令:

用vs2013+velt-0.1.4进行嵌入式开发 进行海思平台 UBOOT 开发

1.1    什么是VELT VELT的全称是Visual EmbedLinuxTools,它是一个与visual gdb类似的visual studio插件,用以辅助完成Linux开发.利用这个插件,将可以在visual studio的IDE中进行Linux应用程序的开发(包括编译和调试),也可以进行uboot和linux内核的编译,并根据编译时的错误信息正确定位到源码.目前的版本是0.1.4,仅支持vs2013.此插件可以在CSDN下载频道下载(http://download.csdn.ne

嵌入式开发基础知识:Linux支持的多种文件系统类型

Linux支持多种文件系统类型,在嵌入式开发中上常用有:ROMFS.JFFS2.NFS.CRAMFS.YAFFS.UBIFS等. JFFS文件系统 JFFS文件系统最早是由瑞典Axis Communications公司基于Linux2.0的内核为嵌入式系统开发的文件系统.JFFS2是RedHat公司基于JFFS开发的闪存文件系统,最初是针对RedHat公司的嵌入式产品eCos开发的嵌入式文件系统,所以JFFS2也可以用在Linux, uCLinux中. Jffs2: 日志闪存文件系统版本2 (J

嵌入式开发-嵌入式的底层驱动方向和上层应用方向的分析

问题描述 嵌入式的底层驱动方向和上层应用方向的分析 刚刚得到帮助了解了方向,觉得搞嵌入式软件这两个方向其中之一,还是想进一步了解两者,以及两者的区别和学习内容,如果可以,给小弟提一点建议,谢谢 解决方案 底层驱动的技术要求比应用高,可以从应用入手,再掌握驱动之类的底层开发 解决方案二: 嵌入式行业新人系列之一 - 如何选择自己的嵌入式开发方向? 嵌入式开发联盟-www.mcuos.com Osboy原创:qq:82475491mcuos.com@gmail.com 废话不多说.首先声明osboy