本文分析Engine組件,它表示整個Catalina servlet引擎木蹬,可以包含一個或多個子容器强重,如Host或者Context等。
Engine組件
Engine組件表示整個請求處理的機構(gòu)庆揩,它從一個或多個Connector中接收并處理請求,并將響應(yīng)返回給Connector跌穗。Service元素內(nèi)有且僅能有一個Engine元素订晌。Engine接口繼承了Container接口,StandardEngine類繼承ContainerBase基類并實現(xiàn)了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的配置文檔奕枢。
構(gòu)造函數(shù)為自己的Pipeline添加了基本閥StandardEngineValve。Engine組件沒有父容器佩微,只有父Service(因為Engine在server.xml中是Service的子元素)缝彬,addChild方法只能添加Host組件。
組件初始化
初始化時調(diào)用基類ContainerBase的對應(yīng)方法為自己創(chuàng)建線程池哺眯。
@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();
}
組件啟動
啟動時調(diào)用基類ContainerBase的對應(yīng)方法啟動各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();
}