1. Service
public interface Service extends Lifecycle {
// ------------------------------------------------------------- Properties
public Engine getContainer();
public void setContainer(Engine engine);
public String getName();
public void setName(String name);
public Server getServer();
public void setServer(Server server);
public ClassLoader getParentClassLoader();
public void setParentClassLoader(ClassLoader parent);
public String getDomain();
// --------------------------------------------------------- Public Methods
public void addConnector(Connector connector);
public Connector[] findConnectors();
public void removeConnector(Connector connector);
public void addExecutor(Executor ex);
public Executor[] findExecutors();
public Executor getExecutor(String name);
public void removeExecutor(Executor ex);
Mapper getMapper();
}
2. StandardService
StandardService是Service的实现类。
StandardService中关于Tomcat启动过程中重要的方法是initInternal()、startInternal():
initInternal():
protected void initInternal() throws LifecycleException {
super.initInternal();
// Engine init
if (engine != null) {
engine.init();
}
// Executor init
for (Executor executor : findExecutors()) {
if (executor instanceof JmxEnabled) {
((JmxEnabled) executor).setDomain(getDomain());
}
executor.init();
}
// MapperListener init
mapperListener.init();
// Connector init
synchronized (connectorsLock) {
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);
}
}
}
}
startInternal():
protected void startInternal() throws LifecycleException {
if(log.isInfoEnabled())
log.info(sm.getString("standardService.start.name", this.name));
setState(LifecycleState.STARTING);
// Engine start
if (engine != null) {
synchronized (engine) {
engine.start();
}
}
// Executor start
synchronized (executors) {
for (Executor executor: executors) {
executor.start();
}
}
// MapperListener start
mapperListener.start();
// Connector start
synchronized (connectorsLock) {
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);
}
}
}
}