本文分析Engine组件,它表示整个Catalina servlet引擎,可以包含一个或多个子容器,如Host或者Context等。
Engine组件
Engine组件表示整个请求处理的机构,它从一个或多个Connector中接收并处理请求,并将响应返回给Connector。Service元素内有且仅能有一个Engine元素。Engine接口继承了Container接口,StandardEngine类继承ContainerBase基类并实现了Engine接口,其部分代码如下所示:
public class StandardEngine extends ContainerBase implements Engine {
public StandardEngine() {
super();
pipeline.setBasic(new StandardEngineValve());
/* Set the jmvRoute using the system property jvmRoute */
try {
setJvmRoute(System.getProperty("jvmRoute"));
} catch(Exception ex) {
log.warn(sm.getString("standardEngine.jvmRouteFail"));
}
// By default, the engine will hold the reloading thread
backgroundProcessorDelay = 10;
}
private String defaultHost = null;
private Service service = null;
@Override
public String getDefaultHost() {
return (defaultHost);
}
@Override
public void setDefaultHost(String host) {
String oldDefaultHost = this.defaultHost;
if (host == null) {
this.defaultHost = null;
} else {
this.defaultHost = host.toLowerCase(Locale.ENGLISH);
}
support.firePropertyChange("defaultHost", oldDefaultHost,
this.defaultHost);
}
@Override
public Service getService() {
return (this.service);
}
@Override
public void setService(Service service) {
this.service = service;
}
@Override
public void addChild(Container child) {
if (!(child instanceof Host))
throw new IllegalArgumentException
(sm.getString("standardEngine.notHost"));
super.addChild(child);
}
// 省略一些代码
}
- defaultHost表示Engine中的默认主机名,使用servel.xml配置时必须与一个Host元素的名称相匹配;
- service表示该Engine所处的Service;
- 其余变量的含义可以参考Engine的配置文档。
构造函数为自己的Pipeline添加了基本阀StandardEngineValve。Engine组件没有父容器,只有父Service(因为Engine在server.xml中是Service的子元素),addChild方法只能添加Host组件。
组件初始化
初始化时调用基类ContainerBase的对应方法为自己创建线程池。
@Override
protected void initInternal() throws LifecycleException {
// Ensure that a Realm is present before any attempt is made to start
// one. This will create the default NullRealm if necessary.
getRealm();
super.initInternal();
}
组件启动
启动时调用基类ContainerBase的对应方法启动各Host。
@Override
protected synchronized void startInternal() throws LifecycleException {
// Log our server identification information
if(log.isInfoEnabled())
log.info( "Starting Servlet Engine: " + ServerInfo.getServerInfo());
// Standard container startup
super.startInternal();
}