/** * Add a LifecycleEvent listener to this component. * * @param listener The listener to add */ public void addLifecycleListener(LifecycleListener listener);//给该组将添加一个监听器
/** * Get the life cycle listeners associated with this life cycle. If this * component 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);//删除该组件中的一个监听器
LifecycleListener接口
1 2 3 4 5 6 7 8 9
public interface LifecycleListener { /** * Acknowledge the occurrence of the specified event. * * @param event LifecycleEvent that has occurred */ public void lifecycleEvent(LifecycleEvent event); }
/** * 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) {
在 LifecycleBase类里LifecycleSupport是这么初始化的:private final LifecycleSupport lifecycle = new LifecycleSupport(this);,就是把StandardServer(LifecycleBase)的实例作为Lifecycle
1 2 3 4 5 6 7 8
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);
digester.addObjectCreate("Server/Listener", null, // MUST be specified in the element "className"); digester.addSetProperties("Server/Listener"); digester.addSetNext("Server/Listener", "addLifecycleListener", "org.apache.catalina.LifecycleListener");
LifecycleSupport
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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; }
}
private LifecycleListener listeners[] = new LifecycleListener[0];