tomcat 7 源码分析-8 生命周期lifecycle和监听listener
每个应用都有生命周期lifecycle,可能包括init,start,stop,destroy等更多。针对生命周期的变化,如何做变化作出反 应,tomcat在设计的时候,把时间监听listener结合起来,所以listener取的名字是lifecyclelistener,对 lifecycle进行监听。
总的最底层的来自两个interface的设计Lifecycle和LifecycleListener。
lifecycle这个接口定义了除本身生命周期的函数,另外还定义了整个生命周期阶段的事件类型(这个肯定是为 lifecyclelistener准备的),同时还对LifecycleListener有增加,删除,通知等的函数。 LifecycleListener这个接口定义了对事件Event的反应。简单看下Lifecycle和LifecycleListener的代码片 段。
- public interface Lifecycle {
- /**
- * The LifecycleEvent type for the "component init" event.
- */
- public static final String INIT_EVENT = "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" ;
- [..........]
- // --------------------------------------------------------- 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();
- [.............]
- public void init() throws LifecycleException;
- public void start() throws LifecycleException;
- public void stop() throws LifecycleException;
- public void destroy() throws LifecycleException;
- public LifecycleState getState();
- }
public interface Lifecycle {
/**
* The LifecycleEvent type for the "component init" event.
*/
public static final String INIT_EVENT = "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";
[..........]
// --------------------------------------------------------- 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();
[.............]
public void init() throws LifecycleException;
public void start() throws LifecycleException;
public void stop() throws LifecycleException;
public void destroy() throws LifecycleException;
public LifecycleState getState();
}
- public interface LifecycleListener {
- /**
- * Acknowledge the occurrence of the specified event.
- *
- * @param event LifecycleEvent that has occurred
- */
- public void lifecycleEvent(LifecycleEvent event);
- }
public interface LifecycleListener {
/**
* Acknowledge the occurrence of the specified event.
*
* @param event LifecycleEvent that has occurred
*/
public void lifecycleEvent(LifecycleEvent event);
}
后续的问题是,谁来维护LifecycleListener?谁维护LifecycleListener,必然要提供真正意思上对操作(增,删, 查和通知)函数。Tomcat将这些交给了LifecycleSupport。同时LifecycleSupport还提供对lifecycle传来的 bype,data封装成
LifecycleEvent类,给LifecycleListener用。
- public final class LifecycleSupport {
- public LifecycleSupport(Lifecycle lifecycle) {
- super ();
- this .lifecycle = lifecycle;
- }
- private Lifecycle lifecycle = null ;
- private LifecycleListener listeners[] = new LifecycleListener[ 0 ];
- private final Object listenersLock = new Object(); // Lock object for changes to listeners
- 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;
- }
- }
- public LifecycleListener[] findLifecycleListeners() {
- return listeners;
- }
- public void fireLifecycleEvent(String type, Object data) {
- LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
- LifecycleListener interested[] = listeners;
- for ( int i = 0 ; i < interested.length; i++)
- interested[i].lifecycleEvent(event);
- }
- 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 final class LifecycleSupport {
public LifecycleSupport(Lifecycle lifecycle) {
super();
this.lifecycle = lifecycle;
}
private Lifecycle lifecycle = null;
private LifecycleListener listeners[] = new LifecycleListener[0];
private final Object listenersLock = new Object(); // Lock object for changes to listeners
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;
}
}
public LifecycleListener[] findLifecycleListeners() {
return listeners;
}
public void fireLifecycleEvent(String type, Object data) {
LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
LifecycleListener interested[] = listeners;
for (int i = 0; i < interested.length; i++)
interested[i].lifecycleEvent(event);
}
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;
}
}
}
tomcat的应用都继承之Lifecycle,包括server,service,Connector,GlobalNamingResources等,而针对这些又实现不同的LifecycleListener。