Tomcat生命周期詳解
目錄
- 引言
- 1、LifeCycle接口設計
- 1.1 生命周期的方法
- 1.2 相關的狀態處理
- 2.監聽器和事件的設計
- 3.LifecycleBase
- 3.1 事件處理
- 3.2 生命周期方法
引言
在上篇文章中我們看到了Tomcat架構中的核心組件,而且各個組件都有各自的作用,各司其職,而且相互之間也有對應的父子關系,那么這些對象的創建,調用,銷毀等操作是怎么處理呢?
也就是在Tomcat中的組件的對象生命周期是怎么管理的呢?針對這個問題,在Tomcat中設計了Lifecycle接口來統一管理Tomcat中的核心組件的生命周期,所以本文我們就系統的來介紹下Lifecycle接口的設計
1、LifeCycle接口設計
為了統一管理Tomcat中的核心組件的生命周期,而專門設計了LifeCycle接口來統一管理,我們來看看在LifeCycle接口中聲明了哪些內容。
1.1 生命周期的方法
在LifeCycle中聲明了和生命周期相關的方法,包括init(),start(),stop(),destory()等方法。
在聲明的方法執行的過程中會涉及到對應的狀態的轉換,在LifeCycle接口的頭部文檔中很清楚的說了。
1.2 相關的狀態處理
通過上圖我們可以很清楚的看到相關的方法執行會涉及到的相關狀態的轉換,比如init()會從New這個狀態開始,然后會進入 INITIALIZING 和 INITIALIZED 等。因為這塊涉及到了對應的狀態轉換,在Lifecycle中聲明了相關的狀態和事件的生命周期字符串。
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 static final String AFTER_DESTROY_EVENT = "after_destroy"; public static final String BEFORE_DESTROY_EVENT = "before_destroy"; /** * The LifecycleEvent type for the "periodic" event. * 周期性事件(后臺線程定時執行一些事情,比如:熱部署、熱替換) */ public static final String PERIODIC_EVENT = "periodic"; public static final String CONFIGURE_START_EVENT = "configure_start"; public static final String CONFIGURE_STOP_EVENT = "configure_stop";
在LifecycleState中建立了對應關系
針對特定的事件就會有相關的監聽器來監聽處理。在Lifecycle中定義了相關的處理方法。
public void addLifecycleListener(LifecycleListener listener); public LifecycleListener[] findLifecycleListeners(); public void removeLifecycleListener(LifecycleListener listener);
通過方法名稱我們就能很清楚該方法的相關作用,就不過程介紹了。然后來看下對應的監聽器和事件接口的對應設計。
2.監聽器和事件的設計
接下來看下LifecycleListener的設計。其實代碼非常簡單。
public interface LifecycleListener { /** * Acknowledge the occurrence of the specified event. * 觸發監聽器后要執行邏輯的方法 * @param event LifecycleEvent that has occurred */ public void lifecycleEvent(LifecycleEvent event);}
然后來看下事件的接口
public final class LifecycleEvent extends EventObject { private static final long serialVersionUID = 1L; /** * 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) {super(lifecycle); // 向上轉型,可接受一切實現了生命周期的組件this.type = type;this.data = data; } /** * The event data associated with this event. * 攜帶的額外的數據,傳遞給監聽器的數據 */ private final Object data; /** * The event type this instance represents. * 事件類型 */ private final String type; /** * @return the event data of this event. */ public Object getData() {return data; } /** * @return the Lifecycle on which this event occurred. */ public Lifecycle getLifecycle() {return (Lifecycle) getSource(); } /** * @return the event type of this event. */ public String getType() {return this.type; }}
也是非常簡單,不過多的贅述。
3.LifecycleBase
通過上面的介紹我們可以看到在Tomcat中設計了Lifecycle和LifecycleListener和LifecycleEvent來管理核心組件的生命周期,那么我們就需要讓每一個組件都實現相關的接口。這時你會發現交給子類的工作量其實是比較大的,不光要完成各個組件的核心功能,還得實現生命周期的相關處理,耦合性很強,這時在Tomcat中給我們提供了一個LifecycleBase的抽象類,幫助我們實現了很多和具體業務無關的處理,來簡化了具體組件的業務。
3.1 事件處理
在上面的接口設計中對于監聽對應的事件處理是沒有實現的,在LifecycleBase把這塊很好的實現了,我們來看下。首先定義了一個容器來存儲所有的監聽器
// 存儲了所有的實現了LifecycleListener接口的監聽器 private final List<LifecycleListener> lifecycleListeners = new CopyOnWriteArrayList<>();
同時提供了觸發監聽的相關的方法,綁定了對應的事件。
/** * Allow sub classes to fire {@link Lifecycle} events. * 監聽器觸發相關的事件 * @param type Event type 事件類型 * @param data Data associated with event. */ protected void fireLifecycleEvent(String type, Object data) {LifecycleEvent event = new LifecycleEvent(this, type, data);for (LifecycleListener listener : lifecycleListeners) { listener.lifecycleEvent(event);} }
已經針對Listener相關的處理方法
// 添加監聽器 @Override public void addLifecycleListener(LifecycleListener listener) {lifecycleListeners.add(listener); } // 查找所有的監聽并轉換為了數組類型 @Override public LifecycleListener[] findLifecycleListeners() {return lifecycleListeners.toArray(new LifecycleListener[0]); } // 移除某個監聽器 @Override public void removeLifecycleListener(LifecycleListener listener) {lifecycleListeners.remove(listener); }
3.2 生命周期方法
在LifecycleBase中最核心的還是實現了Lifecycle中的生命周期方法,以init方法為例我們來看。
/** * 實現了 Lifecycle 中定義的init方法 * 該方法和對應的組件的狀態產生的關聯 * @throws LifecycleException */ @Override public final synchronized void init() throws LifecycleException {if (!state.equals(LifecycleState.NEW)) { // 無效的操作 只有狀態為 New 的才能調用init方法進入初始化 invalidTransition(Lifecycle.BEFORE_INIT_EVENT);}try { // 設置狀態為初始化進行中....同步在方法中會觸發對應的事件 setStateInternal(LifecycleState.INITIALIZING, null, false); initInternal(); // 交給子類具體的實現 初始化操作 // 更新狀態為初始化完成 同步在方法中會觸發對應的事件 setStateInternal(LifecycleState.INITIALIZED, null, false);} catch (Throwable t) { handleSubClassException(t, "lifecycleBase.initFail", toString());} }
源碼解析:
- 我們看到首先會判斷當前對象的state狀態是否為NEW,因為init方法只能在NEW狀態下才能開始初始化
- 如果1條件滿足則會更新state的狀態為
INITIALIZED
同時會觸發這個事件 - 然后initInternale()方法會交給子類具體去實現,
- 等待子類處理完成后會把狀態更新為
INITIALIZED
。
我們可以進入setStateInternal方法查看最后的關鍵代碼:
// ....this.state = state; // 更新狀態// 根據狀態和事件的綁定關系獲取對應的事件String lifecycleEvent = state.getLifecycleEvent();if (lifecycleEvent != null) { // 發布對應的事件 fireLifecycleEvent(lifecycleEvent, data);}
可以看到和對應的事件關聯起來了。init方法的邏輯弄清楚后,你會發現start方法,stop方法,destory方法的處理邏輯都是差不多的,可自行觀看。
而對應的 initInternal()方法的邏輯我們需要在 Server Service Engine Connector等核心組件中再看,這個我們會結合Tomcat的啟動流程來帶領大家一起查看。
以上就是Tomcat生命周期詳解的詳細內容,更多關于Tomcat生命周期的資料請關注其它相關文章!
