java jetty 嵌入式_jetty 9 嵌入式开发示例

1 packageyjmyzz.jetty.demo.server;2

3 import org.eclipse.jetty.server.*;4 importorg.eclipse.jetty.server.handler.HandlerCollection;5 importorg.eclipse.jetty.server.handler.RequestLogHandler;6 importorg.eclipse.jetty.server.handler.gzip.GzipHandler;7 importorg.eclipse.jetty.util.thread.QueuedThreadPool;8 importorg.eclipse.jetty.util.thread.ThreadPool;9 importorg.eclipse.jetty.webapp.WebAppContext;10 importorg.slf4j.LoggerFactory;11

12 importjava.io.File;13

14 public classJettyWebServer {15

16 private static org.slf4j.Logger logger = LoggerFactory.getLogger(JettyWebServer.class);17

18 privateServer server;19 private intport;20 privateString host;21 privateString tempDir;22 privateString logDir;23 privateString webDir;24 privateString contextPath;25

26

27 public JettyWebServer(intport, 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() throwsException {40 server = newServer(createThreadPool());41 server.addConnector(createConnector());42 server.setHandler(createHandlers());43 server.setStopAtShutdown(true);44 server.start();45 }46

47 public void join() throwsInterruptedException {48 server.join();49 }50

51

52 privateThreadPool createThreadPool() {53 QueuedThreadPool threadPool = newQueuedThreadPool();54 threadPool.setMinThreads(10);55 threadPool.setMaxThreads(100);56 returnthreadPool;57 }58

59

60 privateNetworkConnector createConnector() {61 ServerConnector connector = newServerConnector(server);62 connector.setPort(port);63 connector.setHost(host);64 returnconnector;65 }66

67 privateHandlerCollection createHandlers() {68 WebAppContext context = newWebAppContext();69 context.setContextPath(contextPath);70 context.setWar(webDir);71 context.setTempDirectory(newFile(tempDir));72

73

74 RequestLogHandler logHandler = newRequestLogHandler();75 logHandler.setRequestLog(createRequestLog());76 GzipHandler gzipHandler = newGzipHandler();77 HandlerCollection handlerCollection = newHandlerCollection();78 handlerCollection.setHandlers(newHandler[]{context, logHandler, gzipHandler});79 returnhandlerCollection;80 }81

82 privateRequestLog createRequestLog() {83 //记录访问日志的处理

84 NCSARequestLog requestLog = newNCSARequestLog();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 returnrequestLog;94 }95

96 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值