Tomcat Lifecycle
catalina在设计上运行一个组件包含其它组件,其中父组件负责启动和关闭它的子组件,这种单一启动/关闭机制就是通过lifecycle接口实现的,start和stop方法是很关键的。
1 /** 2 * Common interface for component life cycle methods. Catalina components 3 * may, but are not required to, implement this interface (as well as the 4 * appropriate interface(s) for the functionality they support) in order to 5 * provide a consistent mechanism to start and stop the component. 6 * 7 * @author Craig R. McClanahan 8 * 9 */ 10 11 public interface Lifecycle { 12 13 14 // ----------------------------------------------------- Manifest Constants 15 16 17 /** 18 * The LifecycleEvent type for the "component init" event. 19 */ 20 public static final String INIT_EVENT = "init"; 21 22 23 /** 24 * The LifecycleEvent type for the "component start" event. 25 */ 26 public static final String START_EVENT = "start"; 27 28 29 /** 30 * The LifecycleEvent type for the "component before start" event. 31 */ 32 public static final String BEFORE_START_EVENT = "before_start"; 33 34 35 /** 36 * The LifecycleEvent type for the "component after start" event. 37 */ 38 public static final String AFTER_START_EVENT = "after_start"; 39 40 41 /** 42 * The LifecycleEvent type for the "component stop" event. 43 */ 44 public static final String STOP_EVENT = "stop"; 45 46 47 /** 48 * The LifecycleEvent type for the "component before stop" event. 49 */ 50 public static final String BEFORE_STOP_EVENT = "before_stop"; 51 52 53 /** 54 * The LifecycleEvent type for the "component after stop" event. 55 */ 56 public static final String AFTER_STOP_EVENT = "after_stop"; 57 58 59 /** 60 * The LifecycleEvent type for the "component destroy" event. 61 */ 62 public static final String DESTROY_EVENT = "destroy"; 63 64 65 /** 66 * The LifecycleEvent type for the "periodic" event. 67 */ 68 public static final String PERIODIC_EVENT = "periodic"; 69 70 71 // --------------------------------------------------------- Public Methods 72 73 74 /** 75 * Add a LifecycleEvent listener to this component. 76 * 77 * @param listener The listener to add 78 */ 79 public void addLifecycleListener(LifecycleListener listener); 80 81 82 /** 83 * Get the lifecycle listeners associated with this lifecycle. If this 84 * Lifecycle has no listeners registered, a zero-length array is returned. 85 */ 86 public LifecycleListener[] findLifecycleListeners(); 87 88 89 /** 90 * Remove a LifecycleEvent listener from this component. 91 * 92 * @param listener The listener to remove 93 */ 94 public void removeLifecycleListener(LifecycleListener listener); 95 96 97 /** 98 * Prepare for the beginning of active use of the public methods of this 99 * component. This method should be called before any of the public 100 * methods of this component are utilized. It should also send a 101 * LifecycleEvent of type START_EVENT to any registered listeners. 102 * 103 * @exception LifecycleException if this component detects a fatal error 104 * that prevents this component from being used 105 */ 106 public void start() throws LifecycleException; 107 108 109 /** 110 * Gracefully terminate the active use of the public methods of this 111 * component. This method should be the last one called on a given 112 * instance of this component. It should also send a LifecycleEvent 113 * of type STOP_EVENT to any registered listeners. 114 * 115 * @exception LifecycleException if this component detects a fatal error 116 * that needs to be reported 117 */ 118 public void stop() throws LifecycleException; 119 120 121 }
LifecycleEvent表示事件
/** * General event for notifying listeners of significant changes on a component * that implements the Lifecycle interface. In particular, this will be useful * on Containers, where these events replace the ContextInterceptor concept in * Tomcat 3.x. * * @author Craig R. McClanahan * */ public final class LifecycleEvent extends EventObject { // ----------------------------------------------------------- Constructors /** * Construct a new LifecycleEvent with the specified parameters. * * @param lifecycle Component on which this event occurred * @param type Event type (required) */ public LifecycleEvent(Lifecycle lifecycle, String type) { this(lifecycle, type, null); } /** * Construct a new LifecycleEvent with the specified parameters. * * @param lifecycle Component on which this event occurred * @param type Event type (required) * @param data Event data (if any) */ public LifecycleEvent(Lifecycle lifecycle, String type, Object data) { super(lifecycle); this.lifecycle = lifecycle; this.type = type; this.data = data; } // ----------------------------------------------------- Instance Variables /** * The event data associated with this event. */ private Object data = null; /** * The Lifecycle on which this event occurred. */ private Lifecycle lifecycle = null; /** * The event type this instance represents. */ private String type = null; // ------------------------------------------------------------- Properties /** * Return the event data of this event. */ public Object getData() { return (this.data); } /** * Return the Lifecycle on which this event occurred. */ public Lifecycle getLifecycle() { return (this.lifecycle); } /** * Return the event type of this event. */ public String getType() { return (this.type); } }
负责监听LifecycleEvent的监听接口LifecycleListener
/** * Interface defining a listener for significant events (including "component * start" and "component stop" generated by a component that implements the * Lifecycle interface. * * @author Craig R. McClanahan * */ public interface LifecycleListener { /** * Acknowledge the occurrence of the specified event. * * @param event LifecycleEvent that has occurred */ public void lifecycleEvent(LifecycleEvent event); }
LifecycleSupport是一个工具类,用来管理对组件注册监听器
/** * Support class to assist in firing LifecycleEvent notifications to * registered LifecycleListeners. * * @author Craig R. McClanahan * */ public final class LifecycleSupport { // ----------------------------------------------------------- Constructors /** * Construct a new LifecycleSupport object associated with the specified * Lifecycle component. * * @param lifecycle The Lifecycle component that will be the source * of events that we fire */ public LifecycleSupport(Lifecycle lifecycle) { super(); this.lifecycle = lifecycle; } // ----------------------------------------------------- Instance Variables /** * The source component for lifecycle events that we will fire. */ private Lifecycle lifecycle = null; /** * The set of registered LifecycleListeners for event notifications. */ private LifecycleListener listeners[] = new LifecycleListener[0]; private final Object listenersLock = new Object(); // Lock object for changes to listeners /** * Tracks the current state of lifecycle object based on the events that * are fired. As far as possible, matches the state names used in Tomcat 7 * for consistency. */ private String state = "NEW"; // --------------------------------------------------------- Public Methods /** * Add a lifecycle event listener to this component. * * @param listener The listener to add */ public void addLifecycleListener(LifecycleListener listener) { synchronized (listenersLock) { LifecycleListener results[] = new LifecycleListener[listeners.length + 1]; for (int i = 0; i < listeners.length; i++) results[i] = listeners[i]; results[listeners.length] = listener; listeners = results; } } /** * Get the lifecycle listeners associated with this lifecycle. If this * Lifecycle has no listeners registered, a zero-length array is returned. */ public LifecycleListener[] findLifecycleListeners() { return listeners; } /** * 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) { if (Lifecycle.INIT_EVENT.equals(type)) { state = "INITIALIZED"; } else if (Lifecycle.BEFORE_START_EVENT.equals(type)) { state = "STARTING_PREP"; } else if (Lifecycle.START_EVENT.equals(type)) { state = "STARTING"; } else if (Lifecycle.AFTER_START_EVENT.equals(type)) { state = "STARTED"; } else if (Lifecycle.BEFORE_STOP_EVENT.equals(type)) { state = "STOPPING_PREP"; } else if (Lifecycle.STOP_EVENT.equals(type)) { state = "STOPPING"; } else if (Lifecycle.AFTER_STOP_EVENT.equals(type)) { state = "STOPPED"; } else if (Lifecycle.DESTROY_EVENT.equals(type)) { state = "DESTROYED"; } LifecycleEvent event = new LifecycleEvent(lifecycle, type, data); LifecycleListener interested[] = listeners; for (int i = 0; i < interested.length; i++) interested[i].lifecycleEvent(event); } /** * Remove a lifecycle event listener from this component. * * @param listener The listener to remove */ public void removeLifecycleListener(LifecycleListener listener) { synchronized (listenersLock) { int n = -1; for (int i = 0; i < listeners.length; i++) { if (listeners[i] == listener) { n = i; break; } } if (n < 0) return; LifecycleListener results[] = new LifecycleListener[listeners.length - 1]; int j = 0; for (int i = 0; i < listeners.length; i++) { if (i != n) results[j++] = listeners[i]; } listeners = results; } } public String getState() { return state; } }
整个事件监听机制其实就是观察者模式的提现

浙公网安备 33010602011771号