写点什么

【Tomcat 源码分析】生命周期机制 Lifecycle

作者:派大星
  • 2024-09-02
    辽宁
  • 本文字数:6672 字

    阅读完需:约 22 分钟

前言

Tomcat 内部,如同精密齿轮般运转着各种组件,彼此协作,构建起庞大的 web 服务器架构。在这错综复杂的体系中,Lifecycle(生命周期机制)犹如灵魂,贯穿始终,赋予组件生命,协调运作。


在深入探究各组件之前,我们不妨先解开 Lifecycle 的神秘面纱,领略它如何赋予组件生命,以及它背后的实现原理。

什么是 Lifecycle?

Lifecycle,实则是一台精密的状态机,以时间为轴,管理着组件从诞生到消亡的每个阶段。


当组件处于STARTING_PREPSTARTINGSTARTED状态时,调用 start()方法如同石沉大海,毫无反应。


然而,当它处于 NEW 状态时,调用start()方法则会触发一系列连锁反应:init()方法率先登场,紧随其后的是 start()方法的执行。


反之,当组件处于STOPPING_PREPSTOPPINGSTOPPED状态时,调用 stop()方法亦是徒劳。


但在 NEW 状态下,调用stop()方法则会将组件直接置于 STOPPED 状态,这往往发生在组件启动失败,其子组件尚未启动的情况下。值得注意的是,当一个组件停止时,它会竭尽全力尝试停止其所有子组件,即使这些子组件尚未启动。

Lifecycle 方法

让我们一同揭开 Lifecycle 的面纱,探寻它拥有的方法,如下:


public interface Lifecycle {    // 添加监听器    public void addLifecycleListener(LifecycleListener listener);    // 获取所以监听器    public LifecycleListener[] findLifecycleListeners();    // 移除某个监听器    public void removeLifecycleListener(LifecycleListener listener);    // 初始化方法    public void init() throws LifecycleException;    // 启动方法    public void start() throws LifecycleException;    // 停止方法,和start对应    public void stop() throws LifecycleException;    // 销毁方法,和init对应    public void destroy() throws LifecycleException;    // 获取生命周期状态    public LifecycleState getState();    // 获取字符串类型的生命周期状态    public String getStateName();}
复制代码

LifecycleBase

LifecycleBaseLifecycle的基石,承载着生命周期的基本实现。我们一起深入探索 LifecycleBase 的每个方法:

增加、删除和获取监听器

private final List<LifecycleListener> lifecycleListeners = new CopyOnWriteArrayList<>();
@Overridepublic void addLifecycleListener(LifecycleListener listener) {lifecycleListeners.add(listener);}@Overridepublic LifecycleListener[] findLifecycleListeners() { return lifecycleListeners.toArray(new LifecycleListener[0]);}@Overridepublic void removeLifecycleListener(LifecycleListener listener) {lifecycleListeners.remove(listener);}
复制代码


  1. 生命周期监听器们,守护着组件的每个状态转变。它们被整齐地排列在一个线程安全的 CopyOnWriteArrayList 中。无论是添加还是移除监听器,都直接调用 List 的相应方法,保证了操作的安全性。

  2. 而 findLifecycleListeners 方法则以数组的形式返回所有监听器。为了确保线程安全,每次调用该方法时,都会生成一个新的数组,将监听器们复制其中,避免了并发修改带来的风险。

init()

@Overridepublic final synchronized void init() throws LifecycleException {    // 非NEW状态,不允许调用init()方法    if (!state.equals(LifecycleState.NEW)) {        invalidTransition(Lifecycle.BEFORE_INIT_EVENT);    }
try { // 初始化逻辑之前,先将状态变更为`INITIALIZING` setStateInternal(LifecycleState.INITIALIZING, null, false); // 初始化,该方法为一个abstract方法,需要组件自行实现 initInternal(); // 初始化完成之后,状态变更为`INITIALIZED` setStateInternal(LifecycleState.INITIALIZED, null, false); } catch (Throwable t) { // 初始化的过程中,可能会有异常抛出,这时需要捕获异常,并将状态变更为`FAILED` ExceptionUtils.handleThrowable(t); setStateInternal(LifecycleState.FAILED, null, false); throw new LifecycleException( sm.getString("lifecycleBase.initFail",toString()), t); }}
复制代码


setStateInternal 方法,负责维护组件的当前状态。每一次状态转换,都会经过它的仔细审核,并在转换成功后,它会向外界发出通知。


为了保证状态的实时可见性,state 被声明为 volatile 类型,确保任何线程对 state 的修改都能立即被其他线程感知,避免出现状态滞后的情况。


private volatile LifecycleState state = LifecycleState.NEW;。
复制代码


private synchronized void setStateInternal(LifecycleState state,        Object data, boolean check) throws LifecycleException {    if (log.isDebugEnabled()) {        log.debug(sm.getString("lifecycleBase.setState", this, state));    }
// 是否校验状态 if (check) { // Must have been triggered by one of the abstract methods (assume // code in this class is correct) // null is never a valid state // state不允许为null if (state == null) { invalidTransition("null"); // Unreachable code - here to stop eclipse complaining about // a possible NPE further down the method return; }
// Any method can transition to failed // startInternal() permits STARTING_PREP to STARTING // stopInternal() permits STOPPING_PREP to STOPPING and FAILED to // STOPPING if (!(state == LifecycleState.FAILED || (this.state == LifecycleState.STARTING_PREP && state == LifecycleState.STARTING) || (this.state == LifecycleState.STOPPING_PREP && state == LifecycleState.STOPPING) || (this.state == LifecycleState.FAILED && state == LifecycleState.STOPPING))) { // No other transition permitted invalidTransition(state.name()); } }
// 设置状态 this.state = state; // 触发事件 String lifecycleEvent = state.getLifecycleEvent(); if (lifecycleEvent != null) { fireLifecycleEvent(lifecycleEvent, data); }}
复制代码


看看fireLifecycleEvent方法,


public void fireLifecycleEvent(String type, Object data) {    // 事件监听,观察者模式的另一种方式    LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);    LifecycleListener interested[] = listeners;// 监听器数组 关注 事件(启动或者关闭事件)    // 循环通知所有生命周期时间侦听器    for (int i = 0; i < interested.length; i++)        // 每个监听器都有自己的逻辑        interested[i].lifecycleEvent(event);}
复制代码


首先, 创建一个事件对象, 然通知所有的监听器发生了该事件.并做响应.

start()

@Overridepublic final synchronized void start() throws LifecycleException {    // `STARTING_PREP`、`STARTING`和`STARTED时,将忽略start()逻辑    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; }
// `NEW`状态时,执行init()方法 if (state.equals(LifecycleState.NEW)) { init(); }
// `FAILED`状态时,执行stop()方法 else if (state.equals(LifecycleState.FAILED)) { stop(); }
// 不是`INITIALIZED`和`STOPPED`时,则说明是非法的操作 else if (!state.equals(LifecycleState.INITIALIZED) && !state.equals(LifecycleState.STOPPED)) { invalidTransition(Lifecycle.BEFORE_START_EVENT); }
try { // start前的状态设置 setStateInternal(LifecycleState.STARTING_PREP, null, false); // start逻辑,抽象方法,由组件自行实现 startInternal(); // start过程中,可能因为某些原因失败,这时需要stop操作 if (state.equals(LifecycleState.FAILED)) { // This is a 'controlled' failure. The component put itself into the // FAILED state so call stop() to complete the clean-up. stop(); } else if (!state.equals(LifecycleState.STARTING)) { // Shouldn't be necessary but acts as a check that sub-classes are // doing what they are supposed to. invalidTransition(Lifecycle.AFTER_START_EVENT); } else { // 设置状态为STARTED setStateInternal(LifecycleState.STARTED, null, false); } } catch (Throwable t) { // This is an 'uncontrolled' failure so put the component into the // FAILED state and throw an exception. ExceptionUtils.handleThrowable(t); setStateInternal(LifecycleState.FAILED, null, false); throw new LifecycleException(sm.getString("lifecycleBase.startFail", toString()), t); }}
复制代码

stop()

@Overridepublic final synchronized void stop() throws LifecycleException {    // `STOPPING_PREP`、`STOPPING`和STOPPED时,将忽略stop()的执行    if (LifecycleState.STOPPING_PREP.equals(state) || LifecycleState.STOPPING.equals(state) ||            LifecycleState.STOPPED.equals(state)) {
if (log.isDebugEnabled()) { Exception e = new LifecycleException(); log.debug(sm.getString("lifecycleBase.alreadyStopped", toString()), e); } else if (log.isInfoEnabled()) { log.info(sm.getString("lifecycleBase.alreadyStopped", toString())); }
return; }
// `NEW`状态时,直接将状态变更为`STOPPED` if (state.equals(LifecycleState.NEW)) { state = LifecycleState.STOPPED; return; }
// stop()的执行,必须要是`STARTED`和`FAILED` if (!state.equals(LifecycleState.STARTED) && !state.equals(LifecycleState.FAILED)) { invalidTransition(Lifecycle.BEFORE_STOP_EVENT); }
try { // `FAILED`时,直接触发BEFORE_STOP_EVENT事件 if (state.equals(LifecycleState.FAILED)) { // Don't transition to STOPPING_PREP as that would briefly mark the // component as available but do ensure the BEFORE_STOP_EVENT is // fired fireLifecycleEvent(BEFORE_STOP_EVENT, null); } else { // 设置状态为STOPPING_PREP setStateInternal(LifecycleState.STOPPING_PREP, null, false); }
// stop逻辑,抽象方法,组件自行实现 stopInternal();
// Shouldn't be necessary but acts as a check that sub-classes are // doing what they are supposed to. if (!state.equals(LifecycleState.STOPPING) && !state.equals(LifecycleState.FAILED)) { invalidTransition(Lifecycle.AFTER_STOP_EVENT); } // 设置状态为STOPPED setStateInternal(LifecycleState.STOPPED, null, false); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); setStateInternal(LifecycleState.FAILED, null, false); throw new LifecycleException(sm.getString("lifecycleBase.stopFail",toString()), t); } finally { if (this instanceof Lifecycle.SingleUse) { // Complete stop process first setStateInternal(LifecycleState.STOPPED, null, false); destroy(); } }}
复制代码

destroy()

@Overridepublic final synchronized void destroy() throws LifecycleException {    // `FAILED`状态时,直接触发stop()逻辑    if (LifecycleState.FAILED.equals(state)) {        try {            // Triggers clean-up            stop();        } catch (LifecycleException e) {            // Just log. Still want to destroy.            log.warn(sm.getString(                    "lifecycleBase.destroyStopFail", toString()), e);        }    }
// `DESTROYING`和`DESTROYED`时,忽略destroy的执行 if (LifecycleState.DESTROYING.equals(state) || LifecycleState.DESTROYED.equals(state)) {
if (log.isDebugEnabled()) { Exception e = new LifecycleException(); log.debug(sm.getString("lifecycleBase.alreadyDestroyed", toString()), e); } else if (log.isInfoEnabled() && !(this instanceof Lifecycle.SingleUse)) { // Rather than have every component that might need to call // destroy() check for SingleUse, don't log an info message if // multiple calls are made to destroy() log.info(sm.getString("lifecycleBase.alreadyDestroyed", toString())); }
return; }
// 非法状态判断 if (!state.equals(LifecycleState.STOPPED) && !state.equals(LifecycleState.FAILED) && !state.equals(LifecycleState.NEW) && !state.equals(LifecycleState.INITIALIZED)) { invalidTransition(Lifecycle.BEFORE_DESTROY_EVENT); }
try { // destroy前状态设置 setStateInternal(LifecycleState.DESTROYING, null, false); // 抽象方法,组件自行实现 destroyInternal(); // destroy后状态设置 setStateInternal(LifecycleState.DESTROYED, null, false); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); setStateInternal(LifecycleState.FAILED, null, false); throw new LifecycleException( sm.getString("lifecycleBase.destroyFail",toString()), t); }}
复制代码

模板方法

从上述源码看得出来,LifecycleBase 类巧妙地运用了状态机模板模式的结合,以构筑其功能。


// 初始化方法protected abstract void initInternal() throws LifecycleException;// 启动方法protected abstract void startInternal() throws LifecycleException;// 停止方法protected abstract void stopInternal() throws LifecycleException;// 销毁方法protected abstract void destroyInternal() throws LifecycleException;
复制代码

总结

Lifecycle,看似简单,代码简洁,却又设计精妙,展现出设计模式的优雅与力量。


深入剖析 Lifecycle 的实现,不仅让我们对组件的生命周期有了更深刻的理解,更让我们从中可以领悟到设计模式的精髓,为我们未来设计更优雅、更强大的系统提供了宝贵的参考。


如有问题,欢迎微信搜索【码上遇见你】。


免费的Chat GPT可微信搜索【AI贝塔】进行体验,无限使用。


好了,本章节到此告一段落。希望对你有所帮助,祝学习顺利。

发布于: 刚刚阅读数: 5
用户头像

派大星

关注

微信搜索【码上遇见你】,获取更多精彩内容 2021-12-13 加入

微信搜索【码上遇见你】,获取更多精彩内容

评论

发布
暂无评论
【Tomcat源码分析】生命周期机制 Lifecycle_tomcat源码解读_派大星_InfoQ写作社区