Tomcat中的观察者模式 (Lifecycle)

几个重要的类,接口

LifeCycle : 主题接口

LifeCycleBase : 抽象的主题实现

LifeCycleListener : 观察者

具体分析


public interface Lifecycle {   //主题接口

    // ----------------------------------------------------- Manifest Constants

    /**
     * The LifecycleEvent type for the "component before init" event.

     */
    public static final String BEFORE_INIT_EVENT = "before_init";   //定义了tomcat启动时的状态

    /**
     * The LifecycleEvent type for the "component after init" event.

     */
    public static final String AFTER_INIT_EVENT = "after_init";

    /**
     * The LifecycleEvent type for the "component start" event.

     */
    public static final String START_EVENT = "start";

    /**
     * The LifecycleEvent type for the "component before start" event.

     */
    public static final String BEFORE_START_EVENT = "before_start";

    /**
     * The LifecycleEvent type for the "component after start" event.

     */
    public static final String AFTER_START_EVENT = "after_start";

    /**
     * The LifecycleEvent type for the "component stop" event.

     */
    public static final String STOP_EVENT = "stop";

    /**
     * The LifecycleEvent type for the "component before stop" event.

     */
    public static final String BEFORE_STOP_EVENT = "before_stop";

    /**
     * The LifecycleEvent type for the "component after stop" event.

     */
    public static final String AFTER_STOP_EVENT = "after_stop";

    /**
     * The LifecycleEvent type for the "component after destroy" event.

     */
    public static final String AFTER_DESTROY_EVENT = "after_destroy";

    /**
     * The LifecycleEvent type for the "component before destroy" event.

     */
    public static final String BEFORE_DESTROY_EVENT = "before_destroy";

    /**
     * The LifecycleEvent type for the "periodic" event.

     */
    public static final String PERIODIC_EVENT = "periodic";

    /**
     * The LifecycleEvent type for the "configure_start" event. Used by those
     * components that use a separate component to perform configuration and
     * need to signal when configuration should be performed - usually after
     * {@link #BEFORE_START_EVENT} and before {@link #START_EVENT}.

     */
    public static final String CONFIGURE_START_EVENT = "configure_start";

    /**
     * The LifecycleEvent type for the "configure_stop" event. Used by those
     * components that use a separate component to perform configuration and
     * need to signal when de-configuration should be performed - usually after
     * {@link #STOP_EVENT} and before {@link #AFTER_STOP_EVENT}.

     */
    public static final String CONFIGURE_STOP_EVENT = "configure_stop";

    // --------------------------------------------------------- Public Methods

    /**
     * Add a LifecycleEvent listener to this component.

     *
     * @param listener The listener to add
     */
    public void addLifecycleListener(LifecycleListener listener);     //注册观察者的方法

    /**
     * Get the life cycle listeners associated with this life cycle.

     *
     * @return An array containing the life cycle listeners associated with this
     *         life cycle. If this component has no listeners registered, a
     *         zero-length array is returned.

     */
    public LifecycleListener[] findLifecycleListeners();              //获得所有的观察者的方法

    /**
     * Remove a LifecycleEvent listener from this component.

     *
     * @param listener The listener to remove
     */
    public void removeLifecycleListener(LifecycleListener listener);    //移除观察者的方法

    /**
     * Prepare the component for starting. This method should perform any
     * initialization required post object creation. The following
     * {@link LifecycleEvent}s will be fired in the following order:
     * <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;             //&#x521D;&#x59CB;&#x5316;&#x5BB9;&#x5668;&#x7684;&#x65B9;&#x6CD5;

    /**
     * Prepare for the beginning of active use of the public methods other than
     * property getters/setters and life cycle methods of this component. This
     * method should be called before any of the public methods other than
     * property getters/setters and life cycle methods of this component are
     * utilized. The following {@link LifecycleEvent}s will be fired in the
     * following order:
     * <ol>
     *   <li>BEFORE_START_EVENT: At the beginning of the method. It is as this
     *                           point the state transitions to
     *                           {@link LifecycleState#STARTING_PREP}.</li>
     *   <li>START_EVENT: During the method once it is safe to call start() for
     *                    any child components. It is at this point that the
     *                    state transitions to {@link LifecycleState#STARTING}
     *                    and that the public methods other than property
     *                    getters/setters and life cycle methods may be
     *                    used.</li>
     *   <li>AFTER_START_EVENT: At the end of the method, immediately before it
     *                          returns. It is at this point that the state
     *                          transitions to {@link LifecycleState#STARTED}.

     *                          </li>
     * </ol>
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that prevents this component from being used
     */
    public void start() throws LifecycleException;     //&#x542F;&#x52A8;&#x5BB9;&#x5668;&#x7684;&#x65B9;&#x6CD5;

    /**
     * Gracefully terminate the active use of the public methods other than
     * property getters/setters and life cycle methods of this component. Once
     * the STOP_EVENT is fired, the public methods other than property
     * getters/setters and life cycle methods should not be used. The following
     * {@link LifecycleEvent}s will be fired in the following order:
     * <ol>
     *   <li>BEFORE_STOP_EVENT: At the beginning of the method. It is at this
     *                          point that the state transitions to
     *                          {@link LifecycleState#STOPPING_PREP}.</li>
     *   <li>STOP_EVENT: During the method once it is safe to call stop() for
     *                   any child components. It is at this point that the
     *                   state transitions to {@link LifecycleState#STOPPING}
     *                   and that the public methods other than property
     *                   getters/setters and life cycle methods may no longer be
     *                   used.</li>
     *   <li>AFTER_STOP_EVENT: At the end of the method, immediately before it
     *                         returns. It is at this point that the state
     *                         transitions to {@link LifecycleState#STOPPED}.

     *                         </li>
     * </ol>
     *
     * Note that if transitioning from {@link LifecycleState#FAILED} then the
     * three events above will be fired but the component will transition
     * directly from {@link LifecycleState#FAILED} to
     * {@link LifecycleState#STOPPING}, bypassing
     * {@link LifecycleState#STOPPING_PREP}
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that needs to be reported
     */
    public void stop() throws LifecycleException;    //&#x5173;&#x95ED;&#x5BB9;&#x5668;&#x7684;&#x65B9;&#x6CD5;

    /**
     * Prepare to discard the object. The following {@link LifecycleEvent}s will
     * be fired in the following order:
     * <ol>
     *   <li>DESTROY_EVENT: On the successful completion of component
     *                      destruction.</li>
     * </ol>
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that prevents this component from being used
     */
    public void destroy() throws LifecycleException;

    /**
     * Obtain the current state of the source component.

     *
     * @return The current state of the source component.

     */
    public LifecycleState getState();     //&#x83B7;&#x53D6;&#x5BB9;&#x5668;&#x5F53;&#x524D;&#x6240;&#x5904;&#x7684;&#x72B6;&#x6001;

    /**
     * Obtain a textual representation of the current component state. Useful
     * for JMX. The format of this string may vary between point releases and
     * should not be relied upon to determine component state. To determine
     * component state, use {@link #getState()}.

     *
     * @return The name of the current component 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 {
    }
}


public abstract class LifecycleBase implements Lifecycle {   //&#x4E3B;&#x9898;&#x7684;&#x62BD;&#x8C61;&#x5B9E;&#x73B0;

    private static final Log log = LogFactory.getLog(LifecycleBase.class);

    private static final StringManager sm = StringManager.getManager(LifecycleBase.class);

    /**
     * The list of registered LifecycleListeners for event notifications.

     */
    private final List<lifecyclelistener> lifecycleListeners = new CopyOnWriteArrayList<>();   //&#x7528;&#x6765;&#x5B58;&#x50A8;&#x6240;&#x6709;&#x6CE8;&#x518C;&#x7684;&#x89C2;&#x5BDF;&#x8005;

    /**
     * The current state of the source component.

     */
    private volatile LifecycleState state = LifecycleState.NEW;

    /**
     * {@inheritDoc}
     */
    @Override
    public void addLifecycleListener(LifecycleListener listener) {    //&#x6CE8;&#x518C;&#x89C2;&#x5BDF;&#x8005;&#xFF0C;&#x5C06;&#x89C2;&#x5BDF;&#x8005;&#x6DFB;&#x52A0;&#x5230;&#x6570;&#x7EC4;&#x4E2D;
        lifecycleListeners.add(listener);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public LifecycleListener[] findLifecycleListeners() {         //&#x8FD4;&#x56DE;&#x89C2;&#x5BDF;&#x8005;&#x6570;&#x7EC4;
        return lifecycleListeners.toArray(new LifecycleListener[0]);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void removeLifecycleListener(LifecycleListener listener) { //&#x79FB;&#x9664;&#x89C2;&#x5BDF;&#x8005;&#x7684;&#x5B9E;&#x73B0;
        lifecycleListeners.remove(listener);
    }

    /**
     * Allow sub classes to fire {@link Lifecycle} events.

     *
     * @param type  Event type
     * @param data  Data associated with event.

     */
    protected void fireLifecycleEvent(String type, Object data) {   //&#x53D1;&#x5E03;&#x901A;&#x77E5;&#x7684;&#x65B9;&#x6CD5;
        LifecycleEvent event = new LifecycleEvent(this, type, data);  //&#x5C06;&#x4FE1;&#x606F;&#x5C01;&#x88C5;&#x6210;&#x4E00;&#x4E2A;LifecycleEvent&#x7C7B;&#x578B;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;LifecycleEvent&#x5BF9;&#x8C61;&#x7528;&#x6765;&#x7EDF;&#x4E00;&#x5C01;&#x88C5;&#x6240;&#x6709;&#x7684;&#x4FE1;&#x606F;&#x3002;
        for (LifecycleListener listener : lifecycleListeners) {      //&#x904D;&#x5386;&#x6240;&#x6709;&#x7684;&#x89C2;&#x5BDF;&#x8005;&#xFF0C;&#x5E76;&#x53D1;&#x5E03;&#x901A;&#x77E5;
            listener.lifecycleEvent(event);
        }
    }

}
</lifecyclelistener>
public interface LifecycleListener {  //&#x89C2;&#x5BDF;&#x8005;&#x63A5;&#x53E3;

    /**
     * Acknowledge the occurrence of the specified event.

     *
     * @param event LifecycleEvent that has occurred
     */
    public void lifecycleEvent(LifecycleEvent event);  //&#x63A5;&#x53D7;&#x901A;&#x77E5;&#x7684;&#x65B9;&#x6CD5;

}

可见 tomcat中通过观察者模式来对容器的生命周期进行控制

Original: https://www.cnblogs.com/liwangcai/p/11652759.html
Author: 神奇海螺。
Title: Tomcat中的观察者模式 (Lifecycle)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/574178/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • uboot无法通过nfs加载ubuntu18.04中的文件(转)

    问题描述:i.mx6ull开发板,采用alientek官方维护的uboot,使用ubuntu18.04 lts作为nfs server,导致开发板uboot上nfs命令无法加载网络…

    Java 2023年6月6日
    0108
  • WebSocket(SuperSocket.WebSocket实现)服务端主动断开客户端的连接

    WebSocket(SuperSocket.WebSocket实现)服务端主动断开客户端的连接 使用SuperSocket.WebSocket实现的WebSocket服务端,当有W…

    Java 2023年5月30日
    077
  • MySQL的undo日志—MVCC前置知识

    undo日志 前面学习了redo日志,redo日志保证的是崩溃时事务持久性。我们可以从redo日志恢复到系统崩溃以前。 undo日志就是为了保证事务回滚时事务所作所为都能回到事务执…

    Java 2023年6月16日
    068
  • 谈一谈分布式会话

    一、什么是会话 会话Session代表的是客户端与服务器的一次交互过程,这个过程可以是连续也可以是时断时续的。曾经的Servlet时代(jsp)),一旦用户与服务端交互,服务器to…

    Java 2023年6月5日
    086
  • 五、Java控制流程

    Java流程控制* 用户交互Scanner、Scanner进阶使用 用户交互Scanner ​ 之前我们学习的基本语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一…

    Java 2023年6月7日
    059
  • Java多线程之ThreadPoolTaskExecutor用法

    一、简介 ThreadPoolTaskExecutor线程是Spring的线程池,其底层是依据JDK线程池ThreadPoolExecutor来实现的。 二、参数介绍 corePo…

    Java 2023年5月29日
    061
  • 设计模式——创建型设计模式

    创建型设计模式 争对 &#x5BF9;&#x8C61;/&#x7C7B;创建时的优化 工厂方法模式(了解) 通过定义顶层抽象工厂类,通过继承的方式,针对于每…

    Java 2023年6月14日
    056
  • java二维数组

    二维数组 二维数组可以看成以数组为元素的数组。 还可以有二维、三维、甚至更多维数组,但是实际开发中用 的非常少。最多到二维数组(学习容器后,我们一般使用容器,二维数组用的都很少)。…

    Java 2023年6月15日
    082
  • 1.Mybatis-XML模板

    SELECT sr.ROLE_ID AS roleId, sr.ROLE_NAME AS roleName, sr.IS_ACTIVE AS isActive, sr.REMARK…

    Java 2023年6月13日
    074
  • FastAPI启用HTTPS

    前提: 你需要购买一个域名, 假如是国内的法服务器的话, 需要备案, 否则无法解析当你买了域名后, 一般可以免费生成证书 下载证书 由于我是在腾讯云购买的域名, 所以在腾讯云中下载…

    Java 2023年6月7日
    068
  • SpringBoot自动装配原理解析

    首先对于一个SpringBoot工程来说,最明显的标志的就是 @SpringBootApplication它标记了这是一个SpringBoot工程,所以今天的 SpringBoot…

    Java 2023年6月5日
    067
  • 质量问题不是不爆,时候未到

    没有质量,哪来效率,谈什么成本; 最近大半年,团队以极其曲折的方式,将一个支离破碎的应用从重构的边缘给拉了回来,最终项目回到了正常迭代的节奏中; 年初的时候,运营系统相关人员离职,…

    Java 2023年6月15日
    050
  • 详解JS中 call 方法的实现

    摘要:本文将全面的,详细解析call方法的实现原理 本文将全面的,详细解析call方法的实现原理,并手写出自己的call方法,相信看完本文的小伙伴都能从中有所收获。 call 方法…

    Java 2023年6月15日
    066
  • Spring系列25:Spring AOP 切点详解

    @Poincut 的使用格式如下: @Poincut("PCD") // &#x5207;&#x70B9;&#x8868;&#x…

    Java 2023年6月5日
    090
  • java相关知识点

    Java基础、语法 1. 简述Java跨平台原理 2. Java的安全性 3. Java三大版本 4. 什么是JVM?什么是JDK? 什么是JRE? 5. Java三种注释类型 6…

    Java 2023年5月29日
    075
  • google和oracle闹掰,Java 会不会被抛弃?

    眼花缭乱的编程语言 程序界的语言实在太多,但有一种语言不得不说,那就是java语言,Java语言是Android系统的主要开发语言,现在和Google的关系不是很好,但是他会被淘汰…

    Java 2023年6月9日
    067
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球