11.tomcat生命周期
Catalina由多个组件组成,当Catalina启动的时候,这些组件也会启动。当Catalina停止的时候,这些组件也必须有机会被清除。例如,当一个容器停止工作的时候,它必须唤醒所有加载的servlet的destroy方法,而session管理器要保存session到二级存储器中。
保持组件启动和停止一致的的机制通过实现org.apache.catalina.Lifecycle接口来实现。
一个实现了Lifecycle接口的组件同是会触发一个或多个下列事件:BEFORE_START_EVENT, START_EVENT, AFTER_START_EVENT, BEFORE_STOP_EVENT, STOP_EVENT, and AFTER_STOP_EVENT。
当组件被启动的时候前三个事件会被触发,而组件停止的时候会触发后边三个事件。另外,如果一个组件可以触发事件,那么必须存在相应的监听器来对触发的事件作出回应。
监听器使用org.apache.catalina.LifecycleListener来表示。
Lifecycle接口
public interface Lifecycle {
public static final String START_EVENT = "start";
public static final String BEFORE_START_EVENT = "before_start";
public static final String AFTER_START_EVENT = "after_start";
public static final String STOP_EVENT = "stop";
public static final String BEFORE_STOP_EVENT = "before_stop";
public static final String AFTER_STOP_EVENT = "after_stop";
public void addLifecycleListener(LifecycleListener listener);
public LifecycleListener[] findLifecycleListeners();
public void removeLifecycleListener(LifecycleListener listener);
public void start() throws LifecycleException;
public void stop() throws LifecycleException;
}
LifecycleEvent类
LifecycleEvent表示一个生命周期事件
public final class LifecycleEvent extends EventObject {
public LifecycleEvent(Lifecycle lifecycle, String type) {
this(lifecycle, type, null);
}
public LifecycleEvent(Lifecycle lifecycle, String type, Object data) {
super(lifecycle);
this.lifecycle = lifecycle;
this.type = type;
this.data = data;
}
private Object data = null;
private Lifecycle lifecycle = null;
rivate String type = null;
public Object getData() { return (this.data); }
public Lifecycle getLifecycle() { return (this.lifecycle); }
public String getType() { return (this.type); }
}
LifecycleListener接口
LifecycleListener接口可以表示生命周期监听器
public interface LifecycleListener {
public void lifecycleEvent(LifecycleEvent event);
}
LifecycleSupport类
Catalina提供了一个公用类org.apache.catalina.util.LifecycleSupport来简化组件处理监听器和触发生命周期事件
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;
// --------------------------------------------------------- Public Methods
/**
* Add a lifecycle event listener to this component.
*
* @param listener The listener to add
*/
public void addLifecycleListener(LifecycleListener listener) {
synchronized (listeners) {
LifecycleListener results[] =
new LifecycleListener;
for (int i = 0; i < listeners.length; i++)
results = listeners;
results = 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) {
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.lifecycleEvent(event);
}
/**
* Remove a lifecycle event listener from this component.
*
* @param listener The listener to remove
*/
public void removeLifecycleListener(LifecycleListener listener) {
synchronized (listeners) {
int n = -1;
for (int i = 0; i < listeners.length; i++) {
if (listeners == listener) {
n = i;
break;
}
}
if (n < 0)
return;
LifecycleListener results[] =
new LifecycleListener;
int j = 0;
for (int i = 0; i < listeners.length; i++) {
if (i != n)
results = listeners;
}
listeners = results;
}
}
}
LifecycleSupport类存储所有的生命周期监听器到一个数组中,该数组为listenens,它初始化的时候没有任何成员。
private LifecycleListener listeners[] = new LifecycleListener;
当一个监听器通过addLifecycleListener方法被添加的时候,一个新的数组(长度比旧数组大1)会被创建。
然后就数组中的元素会被拷贝到新数组中并把新事件添加到数组中。
当一个事件被删除的时候,一个新的数组(长度为旧数组-1)的数组会被创建并将所有的元素存储到其中。
页:
[1]