lifecycle接口
实现lifecycle接口的组件启动或关闭时可以触发before_start_event, start_event...stop_event...事件,管理生命周期
public interface Lifecycle{ public void addLifecycleListener(LifecycleListener listener); public LifecycleListener[] findLifecycleListener(); public void removeLifecycleListener(LifecycleListener listener); public void start(); public void stop(); }
LifecycleEvent
public final class LifecycleEvent extends EventObject{ private Object data= null; private Lifecycle lifecycle = null; private String type = null; //... public LifecycleEvent(Lifecycle lifecycle, String type, Object data){ ... } }
LifecycleListener
public interface LifecycleListener{ public ovid lifecycleEvent(LifecycleEvent event);//触发event事件 }
LifecycleSupport,实现了Lifecycle接口,将所有注册的事件监听器存储到一个数组中,帮助组件管理监听器,并触发相应的生命周期
public final class LifecycleSupport{ private Lifecycle lifecycle = null; private LifecycleListener[] listeners = new LifecycleListener[0]; public LifecycleSupport(Lifecycle lifecycle){ //... } public void addLifecycleListener(LifecycleListener listener){ //add listener to listeners } //findLifecycleListenres() //removeLifecycleListener public void fireLifecycleEvent(String type, Object data){ LifecycleEvent event = new LifecycleEvent(lifecycle, type, data); //trigger listeners lifecycleEvent method } }
public class SimpleContext implements Context, Container, Lifecycle{ //... protected LifecycleSupport = new LifecycleSupport(this); public synchronized void start{ if (started){ started = true; lifecycle.fireLifycycleEvent(BEFORE_START_EVENT, null); if(loader instanceof Lifecycle){ loader.start(); } if(pipeline instanceof Lifecycle){ pipeline.start(); } Container[] children = findChidren(); for (Container child : children){ if(child instanceof Lifecycle){ child.start(); } } lifecycle.fireLifycycleEvent(START_EVENT, null); } lifecycle.fireLifycycleEvent(AFTER_START_EVENT, null); } //public void stop as start method }