StandardService类
该类是Service 接口的标准实现,另外还是先lifecycle接口和MBeanRegistration 接口。该类签名如下:
public class StandardService
implements Lifecycle, Service, MBeanRegistration
Service接口是一个或多个连接(Connector)共享一个容器,这样就允许一个SSL连接和一个非SSL连接共享同一个应用程序。
一个JVM可以有多个Service实例。
该接口的主要方法有:
Method Summary | |
void |
addConnector(Connector connector) Add a new Connector to the set of defined Connectors, and associate it with this Service's Container. |
Connector[] |
findConnectors() Find and return the set of Connectors associated with this Service. |
Container |
getContainer() Return the Container that handles requests for all Connectors associated with this Service. |
java.lang.String |
getInfo() Return descriptive information about this Service implementation and the corresponding version number, in the format <description>/<version> . |
java.lang.String |
getName() Return the name of this Service. |
Server |
getServer() Return the Server with which we are associated (if any). |
void |
initialize() Invoke a pre-startup initialization. |
void |
removeConnector(Connector connector) Remove the specified Connector from the set associated from this Service. |
void |
setContainer(Container container) Set the Container that handles requests for all Connectors associated with this Service. |
void |
setName(java.lang.String name) Set the name of this Service. |
void |
setServer(Server server) Set the Server with which we are associated (if any). |
原removeConnector方法:
synchronized (connectors) {
int j = -1;
for (int i = 0; i < connectors.length; i++) {
if (connector == connectors[i]) {
j = i;
break;
}
}
if (j < 0)
return;
if (started && (connectors[j] instanceof Lifecycle)) {
try {
((Lifecycle) connectors[j]).stop();
} catch (LifecycleException e) {
log.error("Connector.stop", e);
}
}
connectors[j].setContainer(null);
connector.setService(null);
int k = 0;
Connector results[] = new Connector[connectors.length - 1];
for (int i = 0; i < connectors.length; i++) {
if (i != j)
results[k++] = connectors[i];
}
connectors = results;
// Report this property change to interested listeners
support.firePropertyChange("connector", connector, null);
}
修改后为:
synchronized (connectors) {
List<Connector>list=Arrays.asList(connectors);
synchronized (list) {
if(list.contains(connector))
list.remove(connector);
else
return;
connectors=list.toArray(new Connector[0]);
}
}
这里暂且不清楚第二个synchronized是否有必要...
start()方法:
public void start() throws LifecycleException {
// Validate and update our current component state
if (log.isInfoEnabled() && started) {
log.info(sm.getString("standardService.start.started"));
}
if( ! initialized )
init();
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
if(log.isInfoEnabled())
log.info(sm.getString("standardService.start.name", this.name));
lifecycle.fireLifecycleEvent(START_EVENT, null);
started = true;
// Start our defined Container first
if (container != null) {
synchronized (container) {
if (container instanceof Lifecycle) {
((Lifecycle) container).start();
}
}
}
synchronized (executors) {
for ( int i=0; i<executors.size(); i++ ) {
executors.get(i).start();
}
}
// Start our defined Connectors second
synchronized (connectors) {
for (int i = 0; i < connectors.length; i++) {
if (connectors[i] instanceof Lifecycle)
((Lifecycle) connectors[i]).start();
}
}
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
}
protected ArrayList<Executor> executors = new ArrayList<Executor>();
该列表管理的是Executor,该接口在6.0中才引入,由于文档质量原因,目前还无法搞明白。留待后文!
它的标准实现为org.apache.catalina.core.StandardThreadExecutor,下面会对该类进行分析。