今天总结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 LifecycleListeners
lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
started = true;
try {
// Start our subordinate components, if any
if ((loader != null) && (loader instanceof Lifecycle))
((Lifecycle) loader).start();
// Start our child containers, if any
Container 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 any
if (pipeline instanceof Lifecycle)
((Lifecycle) pipeline).start();
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(START_EVENT, null);
} catch (Exception e) {
e.printStackTrace();
}
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
}
参考:
how tomcat works