本文主要内容:
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();
Connector 的构造方法默认会使用 Http11NioProtocol 协议 (NIO) 来对客户端连接进行处理
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();
- ...........
- }
LifecycleBase 是 Tomcat 中初始化的 server 的父类, 当 Tomcat 的 StandardServer 调用 start()方法时, 会去执行 LifecycleBase 的 start 方法
LifecycleBase 中都保存 用 volatile 修饰的 state 字段, 该字段用来标识 Tomcat 启动时所处的状态, 默认是 NEW
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;
对应事件名
- /**
- * The LifecycleEvent type for the "component before init" event.
- */
- public static final String BEFORE_INIT_EVENT = "before_init";
- /**
- * The LifecycleEvent type for the "component after init" event.
- */
- public static final String AFTER_INIT_EVENT = "after_init";
- /**
- * The LifecycleEvent type for the "component start" event.
- */
- public static final String START_EVENT = "start";
- /**
- * The LifecycleEvent type for the "component before start" event.
- */
- public static final String BEFORE_START_EVENT = "before_start";
- /**
- * The LifecycleEvent type for the "component after start" event.
- */
- public static final String AFTER_START_EVENT = "after_start";
- /**
- * The LifecycleEvent type for the "component stop" event.
- */
- public static final String STOP_EVENT = "stop";
- /**
- * The LifecycleEvent type for the "component before stop" event.
- */
- public static final String BEFORE_STOP_EVENT = "before_stop";
- /**
- * The LifecycleEvent type for the "component after stop" event.
- */
- public static final String AFTER_STOP_EVENT = "after_stop";
- /**
- * The LifecycleEvent type for the "component after destroy" event.
- */
- public static final String AFTER_DESTROY_EVENT = "after_destroy";
- /**
- * The LifecycleEvent type for the "component before destroy" event.
- */
- public static final String BEFORE_DESTROY_EVENT = "before_destroy";
- /**
- * The LifecycleEvent type for the "periodic" event.
- */
- public static final String PERIODIC_EVENT = "periodic";
- /**
- * The LifecycleEvent type for the "configure_start" event. Used by those
- * components that use a separate component to perform configuration and
- * need to signal when configuration should be performed - usually after
- * {@link #BEFORE_START_EVENT} and before {@link #START_EVENT}.
- */
- public static final String CONFIGURE_START_EVENT = "configure_start";
- /**
- * The LifecycleEvent type for the "configure_stop" event. Used by those
- * components that use a separate component to perform configuration and
- * need to signal when de-configuration should be performed - usually after
- * {@link #STOP_EVENT} and before {@link #AFTER_STOP_EVENT}.
- */
- 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;
来源: http://www.bubuko.com/infodetail-2986650.html