Tomcat Service的设计和实现:StandardService
·
server.xml
- 首先要看下server.xml中Service的配置,这样你便知道了需要了解的4个部分
xml
<!--
每个Service元素只能有一个Engine元素.元素处理在同一个<Service>中所有<Connector>元素接收到的客户请求
-->
<Service name="Catalina">
<!-- 1. 属性说明
name:Service的名称
-->
<!--2. 一个或多个excecutors -->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!--
3.Connector元素:
由Connector接口定义.<Connector>元素代表与客户程序实际交互的组件,它负责接收客户请求,以及向客户返回响应结果.
-->
<Connector port="80" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
<!-- 属性说明
port:服务器连接器的端口号,该连接器将在指定端口侦听来自客户端的请求。
enableLookups:如果为true,则可以通过调用request.getRemoteHost()进行DNS查询来得到远程客户端的实际主机名;
若为false则不进行DNS查询,而是返回其ip地址。
redirectPort:服务器正在处理http请求时收到了一个SSL传输请求后重定向的端口号。
acceptCount:当所有可以使用的处理请求的线程都被用光时,可以放到处理队列中的请求数,超过这个数的请求将不予处理,而返回Connection refused错误。
connectionTimeout:等待超时的时间数(以毫秒为单位)。
maxThreads:设定在监听端口的线程的最大数目,这个值也决定了服务器可以同时响应客户请求的最大数目.默认值为200。
protocol:必须设定为AJP/1.3协议。
address:如果服务器有两个以上IP地址,该属性可以设定端口监听的IP地址,默认情况下,端口会监听服务器上所有IP地址。
minProcessors:服务器启动时创建的处理请求的线程数,每个请求由一个线程负责。
maxProcessors:最多可以创建的处理请求的线程数。
minSpareThreads:最小备用线程 。
maxSpareThreads:最大备用线程。
debug:日志等级。
disableUploadTimeout:禁用上传超时,主要用于大数据上传时。
-->
<Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
<!-- 负责和其他HTTP服务器建立连接。在把Tomcat与其他HTTP服务器集成时就需要用到这个连接器。 -->
<!--
4. Engine
-->
<Engine name="Catalina" defaultHost="localhost">
</Engine>
</Service>
Service中的接口设计
- 公共属性, name等
java
/** * @return the name of this Service. */ public String getName(); /** * Set the name of this Service. * * @param name The new service name */ public void setName(String name);
- 父Server相关
java
/**
* @return the <code>Server</code> with which we are associated (if any).
*/
public Server getServer();
/**
* Set the <code>Server</code> with which we are associated (if any).
*
* @param server The server that owns this Service
*/
public void setServer(Server server);
/**
* @return the parent class loader for this component. If not set, return
* {@link #getServer()} {@link Server#getParentClassLoader()}. If no server
* has been set, return the system class loader.
*/
public ClassLoader getParentClassLoader();
/**
* Set the parent class loader for this service.
*
* @param parent The new parent class loader
*/
public void setParentClassLoader(ClassLoader parent);
/**
* @return the domain under which this container will be / has been
* registered.
*/
public String getDomain();
- Connector相关
java
/** * Add a new Connector to the set of defined Connectors, and associate it * with this Service's Container. * * @param connector The Connector to be added */ public void addConnector(Connector connector); /** * Find and return the set of Connectors associated with this Service. * * @return the set of associated Connectors */ public Connector[] findConnectors(); /** * Remove the specified Connector from the set associated from this * Service. The removed Connector will also be disassociated from our * Container. * * @param connector The Connector to be removed */ public void removeConnector(Connector connector);
- Engine
java
/** * @return the <code>Engine</code> that handles requests for all * <code>Connectors</code> associated with this Service. */ public Engine getContainer(); /** * Set the <code>Engine</code> that handles requests for all * <code>Connectors</code> associated with this Service. * * @param engine The new Engine */ public void setContainer(Engine engine);
- Excutor相关
java
/** * Adds a named executor to the service * @param ex Executor */ public void addExecutor(Executor ex); /** * Retrieves all executors * @return Executor[] */ public Executor[] findExecutors(); /** * Retrieves executor by name, null if not found * @param name String * @return Executor */ public Executor getExecutor(String name); /** * Removes an executor from the service * @param ex Executor */ public void removeExecutor(Executor ex);
StandardService的实现
属性和父Server相关比较简单,这里主要看下其它的方法:
Engine相关
java
private Engine engine = null;
@Override
public Engine getContainer() {
return engine;
}
@Override
public void setContainer(Engine engine) {
Engine oldEngine = this.engine;
if (oldEngine != null) {
oldEngine.setService(null);
}
this.engine = engine;
if (this.engine != null) {
this.engine.setService(this);
}
if (getState().isAvailable()) {
if (this.engine != null) {
更多推荐
所有评论(0)