Lifecycle
Tomcat中进行生命周期管理的接口是Lifecycle,Tomcat中的组件基本都继承了该接口。
下面来看看该接口中生命周期方法的定义:
Lifecycle定义了四种生命周期状态:init、start、stop、destroy,并且提供了四个方法,并且在接口中定义了关于这四个状态多包含的一些事件,整个状态流转如下图:
* The valid state transitions for components that support {@link Lifecycle}
* are:
* <pre>
* start()
* -----------------------------
* | |
* | init() |
* NEW ->-- INITIALIZING |
* | | | | ------------------<-----------------------
* | | |auto | | |
* | | \|/ start() \|/ \|/ auto auto stop() |
* | | INITIALIZED -->-- STARTING_PREP -->- STARTING -->- STARTED -->--- |
* | | | | | |
* | | | | | |
* | | | | | |
* | |destroy()| | | |
* | -->-----<-- auto auto | | |
* | | ---------<----- MUST_STOP ---------------------<-- | |
* | | | | |
* | \|/ ---------------------------<-------------------------------- ^
* | | | |
* | | \|/ auto auto start() |
* | | STOPPING_PREP ------>----- STOPPING ------>----- STOPPED ---->------
* | | ^ | | ^
* | | stop() | | | |
* | | -------------------------- | | |
* | | | auto | | |
* | | | MUST_DESTROY------<------- | |
* | | | | | |
* | | | |auto | |
* | | | destroy() \|/ destroy() | |
* | | FAILED ---->------ DESTROYING ---<----------------- |
* | | ^ | |
* | | destroy() | |auto |
* | -------->----------------- \|/ |
* | DESTROYED |
* | |
* | stop() |
* --->------------------------------>------------------------------
*
LifecycleBase
LifecycleBase是对Lifecycle中的init、start、stop和destroy方法进行了实现,并且提供了四个钩子方法,用于实现自身的初始化
下面主要分析下init和start方法,另外两个方法类似
public final synchronized void init() throws LifecycleException {
//1:用于状态检查等
if (!state.equals(LifecycleState.NEW)) {
invalidTransition(Lifecycle.BEFORE_INIT_EVENT);
}
//2:设置状态
setStateInternal(LifecycleState.INITIALIZING, null, false);
//3:执行钩子方法,内部初始化
try {
initInternal();
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
setStateInternal(LifecycleState.FAILED, null, false);
throw new LifecycleException(
sm.getString("lifecycleBase.initFail",toString()), t);
}
//4:设置状态
setStateInternal(LifecycleState.INITIALIZED, null, false);
}
public final synchronized void start() throws LifecycleException {
//1:状态判断或状态流转
if (LifecycleState.STARTING_PREP.equals(state) ||
LifecycleState.STARTING.equals(state) ||
LifecycleState.STARTED.equals(state)) {
if (log.isDebugEnabled()) {
Exception e = new LifecycleException();
log.debug(sm.getString("lifecycleBase.alreadyStarted",
toString()), e);
} else if (log.isInfoEnabled()) {
log.info(sm.getString("lifecycleBase.alreadyStarted",
toString()));
}
return;
}
if (state.equals(LifecycleState.NEW)) {
init();
} else if (state.equals(LifecycleState.FAILED)){
stop();
} else if (!state.equals(LifecycleState.INITIALIZED) &&
!state.equals(LifecycleState.STOPPED)) {
invalidTransition(Lifecycle.BEFORE_START_EVENT);
}
//2:设置状态
setStateInternal(LifecycleState.STARTING_PREP, null, false);
//3:执行钩子方法
try {
startInternal();
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
setStateInternal(LifecycleState.FAILED, null, false);
throw new LifecycleException(
sm.getString("lifecycleBase.startFail",toString()), t);
}
//4:设置状态
if (state.equals(LifecycleState.FAILED) ||
state.equals(LifecycleState.MUST_STOP)) {
stop();
} else {
// Shouldn't be necessary but acts as a check that sub-classes are
// doing what they are supposed to.
if (!state.equals(LifecycleState.STARTING)) {
invalidTransition(Lifecycle.AFTER_START_EVENT);
}
setStateInternal(LifecycleState.STARTED, null, false);
}
}
生命周期这块可以参考下第二篇中 Tomcat的启动,里面就是父容器分别调用子容器进行初始化和start的