Tomcat的生命周期管理
来源:互联网 发布:沈阳医疗软件代理 编辑:程序博客网 时间:2024/06/04 20:49
今天总结Tomcat的LifeCycle机制。首先先说容器们都实现了的Lifecycle接口。这个接口来控制容器的生命周期。首先,它定义了六个生命周期的状态,BEFORE_START_EVENT,AFTER_START_EVENT,START_EVENT,STOP_EVENT,BEFORE_STOP_EVENT,AFTER_STOP_EVENT来管理容器的生命周期。而且它里面定义了两个很重要的方法start和stop,用于启动容器和关闭容器时候用。剩下的三个方法是关于listener的addLifecycleListener,findLifecycleListeners, removeLifecycleListener。在tomcat启动的时候,首先先用addLifecycleListener,把相应的listener加进去。
容器们都实现了Lifecycle接口,那么下一步是要给他们加Listener。。。加什么样的Listener呢?LifecycleListener出现啦。listener要实现这个接口才可以哦。这个接口里面只定义了一个方法lifecycleEvent(LifecycleEvent event)。这个方法就是用来写在触发了listener之后要实现什么功能的。那么如何触发listener呢。。。就一个类出现了LifecycleSupport。我们要在容器中实例下这个。然后调用它的fireLifecycleEvent(String type, Object data)方法。它会对找出来容器相关的listener把它们clone一下,然后遍历它们,执行lifecycleEvent方法。这里还要说下LifecycleEvent,这个类表示一个生命周期事件,也是具体存储事件的类。里面有三个属性Lifecycle lifecycle, String type, Object data,分别表示这个事件所属的lifecycle,它的类型,还有要传递的data.
粘代码啦:
这个是LifecycleSupport的fireLifecycleEvent方法
参考:
容器们都实现了Lifecycle接口,那么下一步是要给他们加Listener。。。加什么样的Listener呢?LifecycleListener出现啦。listener要实现这个接口才可以哦。这个接口里面只定义了一个方法lifecycleEvent(LifecycleEvent event)。这个方法就是用来写在触发了listener之后要实现什么功能的。那么如何触发listener呢。。。就一个类出现了LifecycleSupport。我们要在容器中实例下这个。然后调用它的fireLifecycleEvent(String type, Object data)方法。它会对找出来容器相关的listener把它们clone一下,然后遍历它们,执行lifecycleEvent方法。这里还要说下LifecycleEvent,这个类表示一个生命周期事件,也是具体存储事件的类。里面有三个属性Lifecycle lifecycle, String type, Object data,分别表示这个事件所属的lifecycle,它的类型,还有要传递的data.
粘代码啦:
这个是LifeCycle:
public interface Lifecycle { // ----------------------------------------------------- Manifest Constants /** * 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"; // --------------------------------------------------------- Public Methods /** * Add a LifecycleEvent listener to this component. * * @param listener The listener to add */ public void addLifecycleListener(LifecycleListener listener); /** * Get the lifecycle listeners associated with this lifecycle. If this * Lifecycle 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 for the beginning of active use of the public methods of this * component. This method should be called before any of the public * methods of this component are utilized. It should also send a * LifecycleEvent of type START_EVENT to any registered listeners. * * @exception LifecycleException if this component detects a fatal error * that prevents this component from being used */ public void start() throws LifecycleException; /** * Gracefully terminate the active use of the public methods of this * component. This method should be the last one called on a given * instance of this component. It should also send a LifecycleEvent * of type STOP_EVENT to any registered listeners. * * @exception LifecycleException if this component detects a fatal error * that needs to be reported */ public void stop() throws LifecycleException;}
这个是LifecycleSupport的fireLifecycleEvent方法
public final class LifecycleSupport {... /** * Notify all lifecycle event listeners that a particular event has * occurred for this Container. The default implementation performs * this notification synchronously using the calling thread. * * @param type Event type * @param data Event data */ public void fireLifecycleEvent(String type, Object data) { LifecycleEvent event = new LifecycleEvent(lifecycle, type, data); LifecycleListener interested[] = null; synchronized (listeners) { interested = (LifecycleListener[]) listeners.clone(); } for (int i = 0; i < interested.length; i++) interested[i].lifecycleEvent(event); }...}
public synchronized void start() throws LifecycleException {if (started)throw new LifecycleException("SimpleContext has already started");// Notify our interested LifecycleListenerslifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);started = true;try {// Start our subordinate components, if anyif ((loader != null) && (loader instanceof Lifecycle))((Lifecycle) loader).start();// Start our child containers, if anyContainer children[] = findChildren();for (int i = 0; i < children.length; i++) {if (children[i] instanceof Lifecycle)((Lifecycle) children[i]).start();}// Start the Valves in our pipeline (including the basic),// if anyif (pipeline instanceof Lifecycle)((Lifecycle) pipeline).start();// Notify our interested LifecycleListenerslifecycle.fireLifecycleEvent(START_EVENT, null);} catch (Exception e) {e.printStackTrace();}// Notify our interested LifecycleListenerslifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);}
参考:
how tomcat works
1 0
- Tomcat的生命周期管理
- Tomcat的生命周期管理
- Tomcat的生命周期管理简要分析
- Tomcat的生命周期管理简要分析
- Tomcat的生命周期管理简要分析
- Tomcat的生命周期管理简要分析
- tomcat生命周期的管理——生命周期统一接口Lifecycle
- Tomcat(2)生命周期管理
- TOMCAT源码分析-生命周期管理
- Tomcat源码分析--生命周期管理
- Tomcat生命周期的实现
- Tomcat 体系结构、关键组件及生命周期管理
- tomcat源码分析二:生命周期管理
- Tomcat生命周期管理与观察者模式
- TOMCAT源码分析-生命周期管理2
- 管理Activity的生命周期
- session的生命周期管理
- 管理Activity的生命周期
- 集训队专题(5.1)1003 maximum shortest distance
- Ubuntu 14.04LTS修改显示分辨率
- 【Boost】Interprocess - 共享内存、文件映射介绍
- 将TXT文件中的数据整理成sql数据库
- jenkins git can't work ERROR: Timeout after 10 minutes ERROR: Error fetching remote repo 'origin'
- Tomcat的生命周期管理
- 碰到的C语言的关于循环取数的方法
- Notifydatesetchanged的用法:
- BZOJ1008越狱
- [多线程] 生产者消费者模型的BOOST实现
- Linux下增加、删除Swap文件
- 通信之进程间通信-AIDL
- Java学习笔记 2016.2.17 方法重载、继承剖析
- Gradle 自定义构建全局变量