为了学习jetty的启动过程,看了很多文章都是给出了下面这个启动流程图,现在结合jetty.xml来理解jetty的启动过程
![](https://wiki.sankuai.com/download/attachments/621504813/image2016-9-26%2016%3A2%3A39.png?version=1&modificationDate=1474883512000&api=v2)
在工程中找到jetty.xml,在第一行会看到关于Server的配置,此时会创建一个Server的对象,org.eclipse.jetty.server.Server是Server类所在位置,_threadPool、_connectors、_stopAtShutdown这些都是Server中的字段。
<Configure id=
"Server"
class
=
"org.eclipse.jetty.server.Server"
>
|
<Set name=
"ThreadPool"
>
<New
class
=
"org.eclipse.jetty.util.thread.QueuedThreadPool"
>
<Set name=
"minThreads"
>
20
</Set>
<Set name=
"maxThreads"
>
1024
</Set>
</New>
</Set>
|
使用的Set,所以就会使用Server中setThreadPool方法设置一个QueuedThreadPool线程池,同时设置了最小线程数位20,最大线程数为1024。
public
void
setThreadPool(ThreadPool threadPool)
{
if
(_threadPool!=
null
)
removeBean(_threadPool);
_container.update(
this
, _threadPool, threadPool,
"threadpool"
,
false
);
_threadPool = threadPool;
if
(_threadPool!=
null
)
addBean(_threadPool);
}
|
<Call name=
"addConnector"
>
<Arg>
<New
class
=
"org.eclipse.jetty.server.nio.SelectChannelConnector"
>
<Set name=
"host"
><SystemProperty name=
"jetty.host"
/></Set>
<Set name=
"port"
><SystemProperty name=
"jetty.port"
default
=
"8080"
/></Set>
<Set name=
"maxIdleTime"
>
3000
</Set>
<Set name=
"Acceptors"
>
5
</Set>
<Set name=
"statsOn"
>
false
</Set>
<!-- Set name=
"confidentialPort"
>
8063
</Set -->
<Set name=
"lowResourcesConnections"
>
5000
</Set>
<Set name=
"lowResourcesMaxIdleTime"
>
5000
</Set>
</New>
</Arg>
</Call>
|
这是jetty.xml中connector的配置,标签使用的是Call,就会直接调用Server中的addConnector方法添加connector,其中connector使用的是Connector的子类SelectChannelConnector。
maxIdleTime是一个连接的最大空闲时间,如果空闲时间超过这个值就会关闭连接。
Acceptors:用于表示用于调用ServerSocket或ServerSocketChannel中accept方法的线程数,建议这个数字小于或等于可用处理器的2倍。
//AcceptQueueSize:用于表示ServerSocket 或ServerSocketChannel中最大可等待的请求数.