jetty 通过配置文件嵌入式启动web服务

定义 jetty.xml 启动文件 

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Get name="ThreadPool">
<Set name="minThreads" type="int">10</Set>
<Set name="maxThreads" type="int">100</Set>
<Set name="idleTimeout" type="int">60000</Set>
<Set name="detailedDump">false</Set>
</Get>

<Call name="addBean">
<Arg>
<New class="org.eclipse.jetty.util.thread.ScheduledExecutorScheduler" />
</Arg>
</Call>

<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.ServerConnector">
<Arg>
<Ref refid="Server"></Ref>
</Arg>
<Set name="port">8899</Set>
<Set name="connectionFactories">
<Array type="org.eclipse.jetty.server.ConnectionFactory">
<Item>
<New class="org.eclipse.jetty.server.HttpConnectionFactory"></New>
</Item>
</Array>
</Set>
<Set name="idleTimeout">30000</Set>
<!-- <Set name="lowResourceMaxIdleTime">3000</Set> <set name="lowResourcesConnections">90</set>
<Set name="acceptors">1</Set> -->
</New>
</Arg>
</Call>

<Set name="handler">
<New class="org.eclipse.jetty.server.handler.StatisticsHandler">
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New id="Contexts"
class="org.eclipse.jetty.server.handler.ContextHandlerCollection">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New class="org.eclipse.jetty.servlets.gzip.GzipHandler">
<Set name="mimeTypes">text/html,text/xml,application/json,text/plain,application/xhtml+xml,text/css,application/javascript,image/svg+xml
</Set>
<Set name="minGzipSize">0</Set>
<Set name="handler">
<New class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/</Set>
<Set name="war">./web</Set>
<Call name="setInitParameter">
<Arg>org.eclipse.jetty.servlet.Default.dirAllowed
</Arg>
<Arg>false</Arg>
</Call>
</New>
</Set>
</New>
</Item>
</Array>
</Set>
</New>
</Item>

<Item>
<New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler" />
</Item>

<Item>
<New class="org.eclipse.jetty.server.handler.RequestLogHandler">
<Set name="requestLog">
<New class="org.eclipse.jetty.server.NCSARequestLog">
<Set name="filename">
logs/yyyy_mm_dd.jetty_request.log
</Set>
<Set name="filenameDateFormat">yyyy_MM_dd</Set>
<Set name="LogTimeZone">GMT</Set>
<Set name="retainDays">90</Set>
<Set name="append">true</Set>
<Set name="logLatency">true</Set>
</New>
</Set> 
</New>
</Item>
</Array>
</Set>
</New>
</Set>
</New>
</Set>
</Configure>

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

定义jetty启动类

Jetty.java

 

   

public class Jetty {
private Server server;

public Jetty() throws Exception {
Resource fileserver_xml = Resource.newClassPathResource("jetty.xml");
XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream());
server = (Server) configuration.configure();
}

public Jetty(String config) throws Exception {
Resource fileserver_xml = Resource.newClassPathResource(config);
XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream());
server = (Server) configuration.configure();
}

public void startup() throws Exception {
try {
server.start();
} catch (Exception e) {
throw e;
}
try {
server.join();
} catch (InterruptedException e) {
throw e;
}
}

public void shutdown() throws Exception {
server.stop();
}

public static void main(String[] args) throws Exception {
Jetty jetty = new Jetty();
jetty.startup();
}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

WEB 结构

 

bin  etc  lib  logs  web

web中文件内容同tomcat 类似

posted @ 2016-04-01 16:16  易大侠y  阅读(2170)  评论(0编辑  收藏  举报