Tomcat之NIO架构源码分析

本文主要内容:

    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";

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;

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

时间: 2024-08-28 08:40:54

Tomcat之NIO架构源码分析的相关文章

【MyBatis】MyBatis Tomcat JNDI原理及源码分析

一. Tomcat JNDI JNDI(java nameing and drectory interface),是一组在Java应用中访问命名和服务的API,所谓命名服务,即将对象和名称联系起来,使得可以通过名称访问并获取对象. 简单原理介绍:点击访问 tomcat已经集成该服务(内置并默认使用DBCP连接池),简单来说就是键值对的mapping,而且在tomcat服务器启动的首页configuration中就已经有完成的示例代码.要想使用tomcat的JNDI服务,只需要导入相关的jar包,

tomcat的url-pattern的源码分析

1 静态文件的处理前言分析 最近想把SpringMVC对于静态资源的处理策略弄清楚,如它和普通的请求有什么区别吗? 有人可能就要说了,现在有些静态资源都不是交给这些框架来处理,而是直接交给容器来处理,这样更加高效.我想说的是,虽然是这样,处理静态资源也是MVC框架应该提供的功能,而不是依靠外界. 这里以tomcat容器中的SpringMVC项目为例.整个静态资源的访问,效果图如下: 可以分成如下2个大的过程 tomcat根据url-pattern选择servlet的过程 SpringMVC对静态

详解Tomcat系列(一)-从源码分析Tomcat的启动

在整个Tomcat系列文章讲解之前, 我想说的是虽然整个Tomcat体系比较复杂, 但是Tomcat中的代码并不难读, 只要认真花点功夫, 一定能啃下来. 由于篇幅的原因, 很难把Tomcat所有的知识点都放到同一篇文章中, 我将把Tomcat系列文章分为Tomcat的启动, Tomcat中各模块的介绍和Tomcat中的设计模式三部分, 欢迎阅读与关注. 一:通过idea搭建Tomcat源码阅读环境 首先我们到Tomcat的官网(http://tomcat.apache.org/)上下载Tomc

Collection架构源码分析(基于1.8)

Collection接口有三个子接口,我们主要来分析一下其中的两种:List和Set List:有序集合,其中元素可以重复. Set:无序集合,元素不可以重复. List和Set两个接口都各自的抽象实现类. Collection源码分析 源码中的API: public interface Collection<E> extends Iterable<E> { int size(): boolean isEmpty()(); boolean contains(Object o); I

Eureka微服务云架构源码分析

在看具体源码前,我们先回顾一下之前我们所实现的内容,从而找一个合适的切入口去分析.首先,服务注册中心.服务提供者.服务消费者这三个主要元素来说,后两者(也就是Eureka客户端)在整个运行机制中是大部分通信行为的主动发起者,而注册中心主要是处理请求的接收者.所以,我们可以从Eureka的客户端作为入口看看它是如何完成这些主动通信行为的. 我们在将一个普通的Spring Boot应用注册到Eureka Server中,或是从Eureka Server中获取服务列表时,主要就做了两件事: 在应用主类

本书已出版&lt;拨云见日:基于android的内核与系统架构源码分析 &gt;

已陆续倒到各大电商网站及新华书店 http://item.jd.com/11594135.html http://www.amazon.cn/%E6%8B%A8%E4%BA%91%E8%A7%81%E6%97%A5-%E5%9F%BA%E4%BA%8Eandroid%E7%9A%84%E5%86%85%E6%A0%B8%E4%B8%8E%E7%B3%BB%E7%BB%9F%E6%9E%B6%E6%9E%84%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90-%E7%8E%8

SpringMVC处理静态文件源码分析

SpringMVC处理静态资源,主要是两个标签,mvc:resources和mvc:default-servlet-handler.在详细说明他们的原理之前,需要先简单说明下SpringMVC中请求处理机制:HandlerMapping和HandlerAdapter. 1 HandlerMapping和HandlerAdapter的来由 用过python Django框架的都知道Django对于访问方式的配置就是,一个url路径和一个函数配对,你访问这个url,就会直接调用这个函数,简单明了 然

Tomcat源码分析二:先看看Tomcat的整体架构

Tomcat源码分析二:先看看Tomcat的整体架构 Tomcat架构图 我们先来看一张比较经典的Tomcat架构图: 从这张图中,我们可以看出Tomcat中含有Server.Service.Connector.Container等组件,接下来我们一起去大致的看看这些组件的作用和他们之间的相互联系.在这之前,我们先补充一个知识点,也就是Tomcat它实现的功能点是什么呢?通过查找一些资料,这里参考下极客时间<深入拆解Tomcat_Jetty>中的总结,即Tomcat 要实现 2 个核心功能:

Tomcat 源码分析(转)

Tomcat源码分析(一)--服务启动 1. Tomcat主要有两个组件,连接器和容器,所谓连接器就是一个http请求过来了,连接器负责接收这个请求,然后转发给容器.容器即servlet容器,容器有很多层,分别是Engine,     Host,Context,Wrapper.最大的容器Engine,代表一个servlet引擎,接下来是Host,代表一个虚拟机,然后是Context,代表一个应用,Wrapper对应一个servlet.从连接器     传过来连接后,容器便会顺序经过上面的容器,最