Jetty Server 应用

下面提供了一段最基本的Jetty Server应用代码,很简单的不做说明了,适合入门者。

int port=8080;

/**
*核心操作对象
*/
HandlerCollection handlerCollection=new HandlerCollection();
ContextHandlerCollection contextHandlerCollection=new ContextHandlerCollection();
DefaultHandler defaultHandler=new DefaultHandler();
RequestLogHandler requestLogHandler=new RequestLogHandler();
handlerCollection.setHandlers(new Handler[]{contextHandlerCollection, defaultHandler, requestLogHandler});
server.setHandler(handlerCollection);

/**
*线程池
*/
QueuedThreadPool threadPool=new QueuedThreadPool();
threadPool.setMinThreads(10);
threadPool.setMaxThreads(200);
threadPool.setLowThreads(20);
threadPool.setSpawnOrShrinkAt(2);
server.setThreadPool(threadPool);

/**
*连接器
*/
SelectChannelConnector connector=new SelectChannelConnector();
connector.setPort(port);
connector.setMaxIdleTime(30000);
connector.setAcceptors(2);
connector.setStatsOn(false);
connector.setConfidentialPort(8443);
connector.setLowResourceMaxIdleTime(5000);
connector.setLowResourcesConnections(5000);
server.addConnector(connector);

/**
*应用部署
*/
WebAppDeployer webAppDeployer=new WebAppDeployer();
webAppDeployer.setContexts(contextHandlerCollection);
webAppDeployer.setWebAppDir(home+"./src/main/webapps");
webAppDeployer.setParentLoaderPriority(false);
webAppDeployer.setExtract(true);
webAppDeployer.setAllowDuplicates(false);
server.addLifeCycle(webAppDeployer);

/**
*日志
*/
NCSARequestLog ncsaRequestLog=new NCSARequestLog();
ncsaRequestLog.setFilename(home+"./src/main/logs/yyyy_mm_dd.request.log");
ncsaRequestLog.setFilenameDateFormat("yyyy_MM_dd");
ncsaRequestLog.setRetainDays(90);
ncsaRequestLog.setAppend(true);
ncsaRequestLog.setExtended(false);
ncsaRequestLog.setLogCookies(false);
ncsaRequestLog.setLogTimeZone("GMT");
requestLogHandler.setRequestLog(ncsaRequestLog);

server.setStopAtShutdown(true);
server.setSendDateHeader(true);
server.setSendDateHeader(true);
server.setGracefulShutdown(1000);
server.start();

猜你喜欢

转载自mrchaohe.iteye.com/blog/1994951