Lifecycle在其他框架中也很常见,比如spring,它常用于具有生命周期的组件,由Lifecycle控制组件的初始化、启动、销毁等动作,方便应用程序获取、释放某些资源,或者是触发某些特定的事件。Tomcat也是如此,在学习整个启动流程之前,我们先行了解下Lifecycle的实现机制,便于理解整个流程。
Lifecycle
Lifecycle接口是一个公用的接口,定义了组件生命周期的一些方法,用于启动、停止Catalina组件。它是一个非常重要的接口,组件的生命周期包括:init、start、stop、destory,以及各种事件的常量、操作LifecycleListener的API,典型的观察者模式
public interface Lifecycle {
// ----------------------- 定义各种EVENT事件 -----------------------
public static final String BEFORE_INIT_EVENT = "before_init";
public static final String AFTER_INIT_EVENT = "after_init";
public static final String START_EVENT = "start";
// 省略事件常量定义……
/**
* 注册一个LifecycleListener
*/
public void addLifecycleListener(LifecycleListener listener);
/**
* 获取所有注册的LifecycleListener
*/
public LifecycleListener[] findLifecycleListeners();
/**
* 移除指定的LifecycleListener
*/
public void removeLifecycleListener(LifecycleListener listener);
/**
* 组件被实例化之后,调用该方法完成初始化工作,发会出以下事件
* <ol>
* <li>INIT_EVENT: On the successful completion of component initialization.</li>
* </ol>
* @exception LifecycleException if this component detects a fatal error
* that prevents this component from being used
*/
public void init() throws LifecycleException;
/**
* 在组件投入使用之前调用该方法,先后会发出以下事件:BEFORE_START_EVENT、START_EVENT、AFTER_START_EVENT
* @exception LifecycleException if this component detects a fatal error
* that prevents this component from being used
*/
public void start() throws LifecycleException;
/**
* 使组件停止工作
*/
public void stop() throws LifecycleException;
/**
* 销毁组件时被调用
*/
public void destroy() throws LifecycleException;
/**
* Obtain the current state of the source component.
*/
public LifecycleState getState();
/**
* 获取state的文字说明
*/
public String getStateName();
/**
* Marker interface used to indicate that the instance should only be used
* once. Calling {@link #stop()} on an instance that supports this interface
* will automatically call {@link #destroy()} after {@link #stop()}
* completes.
*/
public interface SingleUse {
}
各大组件均实现了Lifecycle接口,类图如下所示(查看原图):
- LifecycleBase:它实现了Lifecycle的init、start、stop等主要逻辑,向注册在LifecycleBase内部的LifecycleListener发出对应的事件,并且预留了initInternal、startInternal、stopInternal等模板方法,便于子类完成自己的逻辑
- MBeanRegistration:JmxEnabled 的父类, jmx框架提供的注册MBean的接口,引入此接口是为了便于使用JMX提供的管理功能
- LifecycleMBeanBase:JmxEnabled的子类,通过重写initInternal、destroyInternal方法,统一向jmx中注册/取消注册当前实例,方便利用jmx对实例对象进行管理,代码上特别强调要求子类先行调用super.initInternal
- ContainerBase、StandardServer、StandardService、WebappLoader、Connector、StandardContext、StandardEngine、StandardHost、StandardWrapper等容器都继承了LifecycleMBeanBase,因此这些容器都具有了同样的生命周期并可以通过JMX进行管理
tomcat允许我们使用jmx对tomcat进行监控、管理,可以使用jconsole工具,准备后续写一篇博客分析tomcat jmx
public abstract class LifecycleMBeanBase extends LifecycleBase
implements JmxEnabled {
/**
* Sub-classes wishing to perform additional initialization should override
* this method, ensuring that super.initInternal() is the first call in the
* overriding method.
*/
protected void initInternal() throws LifecycleException {
if (oname == null) {
mserver = Registry.getRegistry(null, null).getMBeanServer();
oname = register(this, getObjectNameKeyProperties());
}
}
}
public class Connector extends LifecycleMBeanBase {
protected void initInternal() throws LifecycleException {
super.initInternal();
adapter = new CoyoteAdapter(this);
protocolHandler.setAdapter(adapter);
// other code......
}
LifecycleState
LifecycleState是枚举类,定义了各种状态
public enum LifecycleState {
// LifecycleBase实例化完成时的状态
NEW(false, null),
// 容器正在初始化的状态,在INITIALIZED之前
INITIALIZING(false, Lifecycle.BEFORE_INIT_EVENT),
// 初始化完成的状态
INITIALIZED(false, Lifecycle.AFTER_INIT_EVENT),
// 启动前
STARTING_PREP(false, Lifecycle.BEFORE_START_EVENT),
// 启动过程中的状态
STARTING(true, Lifecycle.START_EVENT),
// 启动完成
STARTED(true, Lifecycle.AFTER_START_EVENT),
// 停止前的状态
STOPPING_PREP(true, Lifecycle.BEFORE_STOP_EVENT),
// 停止过程中
STOPPING(false, Lifecycle.STOP_EVENT),
// 停止完成
STOPPED(false, Lifecycle.AFTER_STOP_EVENT),
// 销毁中
DESTROYING(false, Lifecycle.BEFORE_DESTROY_EVENT),
// 完成销毁
DESTROYED(false, Lifecycle.AFTER_DESTROY_EVENT),
// 启动、停止过程中出现异常
FAILED(false, null);
private final boolean available;
private final String lifecycleEvent;
private LifecycleState(boolean available, String lifecycleEvent) {
this.available = available;
this.lifecycleEvent = lifecycleEvent;
}
public boolean isAvailable() {
return available;
}
public String getLifecycleEvent() {
return lifecycleEvent;
}