1. 程式人生 > >Tomcat之NIO架構源碼分析

Tomcat之NIO架構源碼分析

mave protocol cat lean should etc perf oid artifact

本文主要內容:

    Tomcat-NIO啟動源碼分析,關於Tomcat響應一次HTTP請求進行詳細的分析。Tomcat版本:9.0.6

技術分享圖片

啟動Tomcat方式這裏采取編程的方式,maven引入坐標

  

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version
>9.0.6</version> </dependency>

1.啟動方法

        Tomcat tomcat = new Tomcat();
        Connector connector = new Connector();
        connector.setPort(8080);
        tomcat.setConnector(connector);
        tomcat.start();
        tomcat.getServer().await();
  1. Connector的構造方法默認會使用Http11NioProtocol協議(NIO)來對客戶端連接進行處理
  2. Tomcat.start() 會初始化一個StandardServer和StandardService

2.LifecycleBase.start() 初始化與啟動

  

@Override
    public final synchronized void start() throws LifecycleException {
        //state默認是 LifecycleState.NEW,首次執行時,先通過init()方法實現初始化
        if (state.equals(LifecycleState.NEW)) {
            init();
        }
        
try { setStateInternal(LifecycleState.STARTING_PREP, null, false); //初始化之後,開始啟動tomcat服務器 startInternal();
     ........... }

  技術分享圖片

  1. LifecycleBase 是 Tomcat中初始化的server的父類,當Tomcat的StandardServer調用start()方法時,會去執行LifecycleBase的 start 方法

  2. LifecycleBase 中都保存 用volatile 修飾的state字段,該字段用來標識 Tomcat 啟動時所處的狀態,默認是 NEW
  3. Tomcat 服務器的每個狀態,都有對應的事件可以通過實現 LifecycleListener 接口在加載時都會被放進 lifecycleListeners 中
public enum LifecycleState {
    NEW(false, null),
    INITIALIZING(false, Lifecycle.BEFORE_INIT_EVENT),
    INITIALIZED(false, Lifecycle.AFTER_INIT_EVENT),
    STARTING_PREP(false, Lifecycle.BEFORE_START_EVENT),
    STARTING(true, Lifecycle.START_EVENT),
    STARTED(true, Lifecycle.AFTER_START_EVENT),
    STOPPING_PREP(true, Lifecycle.BEFORE_STOP_EVENT),
    STOPPING(false, Lifecycle.STOP_EVENT),
    STOPPED(false, Lifecycle.AFTER_STOP_EVENT),
    DESTROYING(false, Lifecycle.BEFORE_DESTROY_EVENT),
    DESTROYED(false, Lifecycle.AFTER_DESTROY_EVENT),
    FAILED(false, null);

    private final boolean available;
    private final String lifecycleEvent;

對應事件名

技術分享圖片
 1    /**
 2      * The LifecycleEvent type for the "component before init" event.
 3      */
 4     public static final String BEFORE_INIT_EVENT = "before_init";
 5 
 6 
 7     /**
 8      * The LifecycleEvent type for the "component after init" event.
 9      */
10     public static final String AFTER_INIT_EVENT = "after_init";
11 
12 
13     /**
14      * The LifecycleEvent type for the "component start" event.
15      */
16     public static final String START_EVENT = "start";
17 
18 
19     /**
20      * The LifecycleEvent type for the "component before start" event.
21      */
22     public static final String BEFORE_START_EVENT = "before_start";
23 
24 
25     /**
26      * The LifecycleEvent type for the "component after start" event.
27      */
28     public static final String AFTER_START_EVENT = "after_start";
29 
30 
31     /**
32      * The LifecycleEvent type for the "component stop" event.
33      */
34     public static final String STOP_EVENT = "stop";
35 
36 
37     /**
38      * The LifecycleEvent type for the "component before stop" event.
39      */
40     public static final String BEFORE_STOP_EVENT = "before_stop";
41 
42 
43     /**
44      * The LifecycleEvent type for the "component after stop" event.
45      */
46     public static final String AFTER_STOP_EVENT = "after_stop";
47 
48 
49     /**
50      * The LifecycleEvent type for the "component after destroy" event.
51      */
52     public static final String AFTER_DESTROY_EVENT = "after_destroy";
53 
54 
55     /**
56      * The LifecycleEvent type for the "component before destroy" event.
57      */
58     public static final String BEFORE_DESTROY_EVENT = "before_destroy";
59 
60 
61     /**
62      * The LifecycleEvent type for the "periodic" event.
63      */
64     public static final String PERIODIC_EVENT = "periodic";
65 
66 
67     /**
68      * The LifecycleEvent type for the "configure_start" event. Used by those
69      * components that use a separate component to perform configuration and
70      * need to signal when configuration should be performed - usually after
71      * {@link #BEFORE_START_EVENT} and before {@link #START_EVENT}.
72      */
73     public static final String CONFIGURE_START_EVENT = "configure_start";
74 
75 
76     /**
77      * The LifecycleEvent type for the "configure_stop" event. Used by those
78      * components that use a separate component to perform configuration and
79      * need to signal when de-configuration should be performed - usually after
80      * {@link #STOP_EVENT} and before {@link #AFTER_STOP_EVENT}.
81      */
82     public static final String CONFIGURE_STOP_EVENT = "configure_stop";
View Code

public enum LifecycleState {
NEW(false, null),
INITIALIZING(false, Lifecycle.BEFORE_INIT_EVENT),
INITIALIZED(false, Lifecycle.AFTER_INIT_EVENT),
STARTING_PREP(false, Lifecycle.BEFORE_START_EVENT),
STARTING(true, Lifecycle.START_EVENT),
STARTED(true, Lifecycle.AFTER_START_EVENT),
STOPPING_PREP(true, Lifecycle.BEFORE_STOP_EVENT),
STOPPING(false, Lifecycle.STOP_EVENT),
STOPPED(false, Lifecycle.AFTER_STOP_EVENT),
DESTROYING(false, Lifecycle.BEFORE_DESTROY_EVENT),
DESTROYED(false, Lifecycle.AFTER_DESTROY_EVENT),
FAILED(false, null);

private final boolean available;
private final String lifecycleEvent;

Tomcat之NIO架構源碼分析