AbstractLifeCycle类实现了LifeCycle接口(start(), stop()),并提供了doStart()和doStop()方法
public final void start() throws Exception { synchronized (_lock) { try { if (_state == STARTED || _state == STARTING) return; setStarting(); doStart(); setStarted(); } // code omitted } }
AbstractLifeCycle的子类需要重写doStart()和doStop()方法,如Server类:
protected void doStart() throws Exception { for (int i=0;_realms !=null && i<_realms.length; i++) { if (_realms[i] instanceof LifeCycle) ((LifeCycle)_realms[i]).start(); // 启动realms } Iterator itor = _dependentLifeCycles.iterator(); while (itor.hasNext()) { try { ((LifeCycle)itor.next()).start(); // 启动依赖组件 } catch (Throwable e) {mex.add(e);} } if (_threadPool==null) { QueuedThreadPool tp=new QueuedThreadPool(); setThreadPool(tp); // 设置线程池 } if (_sessionIdManager!=null) _sessionIdManager.start(); // 启动会话ID管理器 try { if (_threadPool instanceof LifeCycle) ((LifeCycle)_threadPool).start(); // 启动线程池 } catch(Throwable e) { mex.add(e);} try { super.doStart(); } catch(Throwable e) { Log.warn("Error starting handlers",e); } if (_connectors!=null) { for (int i=0;i<_connectors.length;i++) { try{_connectors[i].start();} // 启动连接器 catch(Throwable e) { mex.add(e); } } } mex.ifExceptionThrow(); }