Chapter 6 : Lifecycle
1.
Lifecycle接口:
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;
}
每一个component都是一个Lifecycle接口的实现。都需要实现其中的方法。
LifecycleEvent是传递的事件的类型。
LifecycleListener类里面含有一个方法:lifecycleEvent(LifecycleEvent event)。这个方法里面就是事件发生后,所触发的动作。
swing中的事件机制可以帮助理解:
ColorAction yellowAction = new ColorAction(Color.YELLOW);
yellowButton.addActionListener(yellowAction);
private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
这里边,LifecycleListener == ActionListener
ActionEvent == LifecycleEvent
yellowButton == Container
接下来的SimpleContextLifecycleListener == ColorAction
yellowButton的addActionListener方法类似于SimpleContext的addLifecycleListener方法。
2、LifecycleSupport类
这是一个运行在底层的类。这是SimpleContext类的一个属性(SimpleContext的listener的相关的方法实际上都是由调用LifecycleSupport类的方法来完成)。这个类的理解也有助于理解swing的事件机制的底层实现。
这个类的主要方法有:
addLifecycleListener()// SimpleContext调用add listener时,调用的就是这个方法
removeLifecycleListener()// 同上
fireLifecycleEvent()// 当一个事件发生时,这个方法被调用。这个方法会依次调用所有LifecycleListener接口中的lifecycleEvent方法。
所有的listener都是放在一个数组中维护的。具体算法看书。
3、Lifecycle在application中的使用
由于众多的组件都有生命周期的概念(即都实现了Lifecycle接口)。在启动关闭的时候,相应的start stop方法都需要调用。这里每一个组件lifecycle方法的调用我们只需要在最高层container层(connector层)调用其start stop方法,他们便会自动的调用子容器的lifecycle方法。
首先,有必要先里清楚各个组件之间的依赖关系:
Connector connector = new HttpConnector();//连接器
Wrapper wrapper1 = new SimpleWrapper();//第一个wrapper容器
wrapper1.setName("Primitive");
wrapper1.setServletClass("PrimitiveServlet");
Wrapper wrapper2 = new SimpleWrapper();//第二个wrapper容器
wrapper2.setName("Modern");
wrapper2.setServletClass("ModernServlet");
Context context = new SimpleContext();//context容器
context.addChild(wrapper1);//给context容器添加wrapper子容器
context.addChild(wrapper2);//给context容器添加wrapper子容器
Mapper mapper = new SimpleContextMapper();//创建mapper
mapper.setProtocol("http");
LifecycleListener listener = new SimpleContextLifecycleListener();//创建listener
((Lifecycle) context).addLifecycleListener(listener);//context容器添加listener
context.addMapper(mapper);//context添加mapper
Loader loader = new SimpleLoader();//创建loader
context.setLoader(loader);//context容器添加loader
// context.addServletMapping(pattern, name);
context.addServletMapping("/Primitive", "Primitive");
context.addServletMapping("/Modern", "Modern");
connector.setContainer(context);
try {
connector.initialize();
((Lifecycle) connector).start();
((Lifecycle) context).start();//启动生命周期
// make the application wait until we press a key.
System.in.read();
((Lifecycle) context).stop();
}
catch (Exception e) {
e.printStackTrace();
}
其次,需要跟踪一下context 的 start方法中的一句话:
lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
这里的lifecycle对象是LifecycleSupport类型。这个fireLifecycleEvent方法如下:
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);
}
可见,在一个事件发生以后,fire方法会进而调用listener接口的lifecycleEvent方法,从而使得监听器方法执行。