Tomcat生命周期
在上篇文章中我們看到了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的核心啟動流程,了解Tomcat啟動的時候主要做了什麼事情!!!