在熟悉server部分源码之前,我们先看看Server主要干什么?
Jetty 官网对它的简单描述:"the plumbing between a collection of Connectors that accept HTTP connections and a collection of Handlers that service requests from the connections and produce responses, with threads from a thread pool doing the work",简单明了,server就是一个管道,管道的一头是负责 接受各种http连接请求的Connector组件,另外一头就是一堆的Handler,这些handler就负责处理来自Connetor的各种request请求,它维护了一个线程池来完成 真正的请求任务处理并将结果返回client。所以说白了,Server组件主要工作就是负责打通Connector和Handler.

1. Jetty Server配置
废话不多说,直接上一段
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Get class="java.lang.System" name="out">
<Call name="println">
<Arg>Redirecting stderr/stdout to <Ref id="ServerLogName"/></Arg>
</Call>
</Get>
<Get name="ThreadPool">
<Set name="minThreads" type="int">20</Set>
<Set name="maxThreads" type="int">200</Set>
<Set name="name">jetty-worker</Set>
</Get>
<Set name="connectors">
<Array type="org.eclipse.jetty.server.Connector">
<Item>
<New class="org.eclipse.jetty.server.ServerConnector">
<Arg><Ref refid="Server"/></Arg>
<Set name="port">8080</Set>
<Set name="host"><!--<SystemProperty name="jetty.host"/>--></Set>
</New>
</Item>
</Array>
</Set>
<Array id="plusConfig" type="java.lang.String">
<Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
<Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
<Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
<Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
<Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
<Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
<Item>org.eclipse.jetty.annotations.AnnotationConfiguration</Item>
<Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
</Array>
<Set name="handler">
<New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="resourceBase">src/main/webapp</Set>
<Set name="contextPath">/</Set>
<Set name="parentLoaderPriority">true</Set>
<Set name="descriptor">src/main/webapp/WEB-INF/web.xml</Set>
<Set name="configurationClasses"><Ref id="plusConfig"/></Set>
</New>
</Item>
</Array>
</Set>
</New>
</Set>
<Set name="stopAtShutdown">true</Set>
<Set name="stopTimeout">1000</Set>
<!--如果打开了会打非常恐怖的日志,导致启动日志没法看了-->
<Set name="dumpAfterStart">false</Set>
<Set name="dumpBeforeStop">false</Set>
</Configure>
网友评论