Lifecycle接口(第六章.生命周期)
Catalina的设计允许一个组件包含其它的组件。例如一个容器可以包含一系列的组件如加载器、管理器等。一个父组件负责启动和停止其子组件。Catalina的设计成所有的组件被一个父组件来管理(in custody),所以启动bootstrap类只需启动一个组件即可。这种单一的启动停止机制通过继承Lifecycle来实现。
1.监听器Listener:(第六章.生命周期)
StandardWrapper中有InstanceSupport instanceSupport = new InstanceSupport(this);
StandardManager中有LifecycleSupport lifecycle = new LifecycleSupport(this);
而LifecycleSupport又将StandardManager设为他的属性值
StandardManager中:
public void addLifecycleListener(LifecycleListener listener) {
lifecycle.addLifecycleListener(listener);
}
在LifecycleSupport中有:专门存放监听器(*Support含有LifecycleListener listeners[]属性)
private LifecycleListener listeners[] = new LifecycleListener[0];
通过这个方法触发事件
public void fireLifecycleEvent(String type, Object data) {
//将StandardManager和type(start_before,stop_aftert,……)和要传得data数据
//包装LifecycleEvent传递给监听器Listener
LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
LifecycleListener interested[] = null;
synchronized (listeners) {
interested = (LifecycleListener[]) listeners.clone();
}
for (int i = 0; i < interested.length; i++)
//从而让监听器Listener触发:
interested[i].lifecycleEvent(event);
}
监听器Listener必须实现LifecycleListener接口
public interface LifecycleListener {
public void lifecycleEvent(LifecycleEvent event);
}
监听器Listener:就可以根据需要做一些操作
下面是ServerLifecycleListener类的lifecycleEvent方法:
public void lifecycleEvent(LifecycleEvent event) {
Lifecycle lifecycle = event.getLifecycle();
if (Lifecycle.START_EVENT.equals(event.getType())) {
if (lifecycle instanceof Server) {
// Loading additional MBean descriptors
loadMBeanDescriptors();
createMBeans();
}
/*
// Ignore events from StandardContext objects to avoid
// reregistering the context
if (lifecycle instanceof StandardContext)
return;
createMBeans();
*/
} else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
if (lifecycle instanceof Server) {
destroyMBeans();
}
} else if (Context.RELOAD_EVENT.equals(event.getType())) {
// Give context a new handle to the MBean server if the
// context has been reloaded since reloading causes the
// context to lose its previous handle to the server
if (lifecycle instanceof StandardContext) {
// If the context is privileged, give a reference to it
// in a servlet context attribute
StandardContext context = (StandardContext)lifecycle;
if (context.getPrivileged()) {
context.getServletContext().setAttribute
(Globals.MBEAN_REGISTRY_ATTR,
MBeanUtils.createRegistry());
context.getServletContext().setAttribute
(Globals.MBEAN_SERVER_ATTR,
MBeanUtils.createServer());
}
}
}
}