StandardService类是Service接口的标准实现,关联的Container通常是一个Engine实例,但并不是必须的。
StandardService.initInternal,
执行初始化操作,被用于允许connector绑定Unix环境下的限定端口
protected void initInternal() throws LifecycleException {
super.initInternal();
if (container != null) {
container.init();//初始化container
}
// 初始化所有的executor
for (Executor executor : findExecutors()) {
if (executor instanceof LifecycleMBeanBase) {
((LifecycleMBeanBase) executor).setDomain(getDomain());
}
executor.init();
}
// 初始化所有的connector
synchronized (connectors) {
for (Connector connector : connectors) {
try {
connector.init();
} catch (Exception e) {
String message = sm.getString(
"standardService.connector.initFailed", connector);
log.error(message, e);
if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE"))
throw new LifecycleException(message);
}
}
}
}
StandardService.startInternal
初始化各个executor、connector
protected void startInternal() throws LifecycleException {
if(log.isInfoEnabled())
log.info(sm.getString("standardService.start.name", this.name));
setState(LifecycleState.STARTING);
// Start our defined Container first
if (container != null) {
synchronized (container) {
container.start();//执行container的start方法
}
}
synchronized (executors) {//执行所有executor的start方法
for (Executor executor: executors) {
executor.start();
}
}
// Start our defined Connectors second
synchronized (connectors) {//执行所有connector的start方法
for (Connector connector: connectors) {
try {
// If it has already failed, don't try and start it
if (connector.getState() != LifecycleState.FAILED) {
connector.start();
}
} catch (Exception e) {
log.error(sm.getString(
"standardService.connector.startFailed",
connector), e);
}
}
}
}
StandardService.stopInternal
stopInternal的关闭顺序与startInternal刚好相反,暂停connector->关闭container->关闭connector->关闭executor
protected void stopInternal() throws LifecycleException {
// Pause connectors first
synchronized (connectors) {
for (Connector connector: connectors) {//暂停所有connector
try {
connector.pause();
} catch (Exception e) {
log.error(sm.getString(
"standardService.connector.pauseFailed",
connector), e);
}
}
}
if(log.isInfoEnabled())
log.info(sm.getString("standardService.stop.name", this.name));
setState(LifecycleState.STOPPING);
// Stop our defined Container second
if (container != null) {//关闭container
synchronized (container) {
container.stop();
}
}
// Now stop the connectors
synchronized (connectors) {//关闭所有connector
for (Connector connector: connectors) {
if (!LifecycleState.STARTED.equals(
connector.getState())) {
// Connectors only need stopping if they are currently
// started. They may have failed to start or may have been
// stopped (e.g. via a JMX call)
continue;
}
try {
connector.stop();
} catch (Exception e) {
log.error(sm.getString(
"standardService.connector.stopFailed",
connector), e);
}
}
}
synchronized (executors) {//关闭所有executor
for (Executor executor: executors) {
executor.stop();
}
}
}