码迷,mamicode.com
首页 > 其他好文 > 详细

Tomcat之NIO架构源码分析

时间:2019-03-14 00:21:28      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:tin   event   protoc   starting   span   top   详细   opened   listener   

本文主要内容:

    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架构源码分析

标签:tin   event   protoc   starting   span   top   详细   opened   listener   

原文地址:https://www.cnblogs.com/coding400/p/10526993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!