Tomcat Startup Sequence(Tomcat启动顺序)

参考引用:

http://tomcat.apache.org/tomcat-8.5-doc/architecture/startup.html

description,A text description of the startup procedure

http://tomcat.apache.org/tomcat-8.5-doc/architecture/startup/serverStartup.txt

diagram,A UML sequence diagram of the startup procedure 

http://tomcat.apache.org/tomcat-8.5-doc/architecture/startup/serverStartup.pdf

Tomcat Startup Sequence

Sequence 1. Start from Command Line
Class: org.apache.catalina.startup.Bootstrap
What it does:
    a) Set up classloaders
        commonLoader (common)-> System Loader
        sharedLoader (shared)-> commonLoader -> System Loader
        catalinaLoader(server) -> commonLoader -> System Loader
        (by default the commonLoader is used for the
         sharedLoader and the serverLoader)
    b) Load startup class (reflection)
        org.apache.catalina.startup.Catalina
        setParentClassloader -> sharedLoader
        Thread.contextClassloader -> catalinaLoader
    c) Bootstrap.daemon.init() complete

Sequence 2. Process command line argument (start, stop)
Class: org.apache.catalina.startup.Bootstrap (assume command->start)
What it does:
    a) Catalina.setAwait(true);
    b) Catalina.load()
        b1) initDirs() -> set properties like
                          catalina.home
                          catalina.base == catalina.home (most cases)
        b2) initNaming
            setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
                    org.apache.naming.java.javaURLContextFactory ->default)
        b3) createStartDigester()
            Configures a digester for the main server.xml elements like
            org.apache.catalina.core.StandardServer (can change of course :)
            org.apache.catalina.deploy.NamingResources
                Stores naming resources in the J2EE JNDI tree
            org.apache.catalina.LifecycleListener
                implements events for start/stop of major components
            org.apache.catalina.core.StandardService
                The single entry for a set of connectors,
                so that a container can listen to multiple connectors
                ie, single entry
            org.apache.catalina.Connector
                Connectors to listen for incoming requests only
            It also adds the following rulesets to the digester
                NamingRuleSet
                EngineRuleSet
                HostRuleSet
                ContextRuleSet
        b4) Load the server.xml and parse it using the digester
            Parsing the server.xml using the digester is an automatic
            XML-object mapping tool, that will create the objects defined in server.xml
            Startup of the actual container has not started yet.
        b5) Assigns System.out and System.err to the SystemLogHandler class
        b6) Calls initialize on all components, this makes each object register itself with the
            JMX agent.
            During the process call the Connectors also initialize the adapters.
            The adapters are the components that do the request pre-processing.
            Typical adapters are HTTP1.1 (default if no protocol is specified,
            org.apache.coyote.http11.Http11NioProtocol)
            AJP1.3 for mod_jk etc.

    c) Catalina.start()
        c1) Starts the NamingContext and binds all JNDI references into it
        c2) Starts the services under <Server> which are:
            StandardService -> starts Engine (ContainerBase -> Realm,Cluster etc)
        c3) StandardHost (started by the service)
                Configures an ErrorReportValve to do proper HTML output for different HTTP
                errors codes
                Starts the Valves in the pipeline (at least the ErrorReportValve)
                Configures the StandardHostValve,
                    this valves ties the Webapp Class loader to the thread context
                    it also finds the session for the request
                    and invokes the context pipeline
                Starts the HostConfig component
                    This component deploys all the webapps
                        (webapps & conf/Catalina/localhost/*.xml)
                    HostConfig will create a Digester for your context, this digester
                    will then invoke ContextConfig.start()
                        The ContextConfig.start() will process the default web.xml (conf/web.xml)
                        and then process the applications web.xml (WEB-INF/web.xml)

        c4) During the lifetime of the container (StandardEngine) there is a background thread that
            keeps checking if the context has changed. If a context changes (timestamp of war file,
            context xml file, web.xml) then a reload is issued (stop/remove/deploy/start)

    d) Tomcat receives a request on an HTTP port
        d1) The request is received by a separate thread which is waiting in the ThreadPoolExecutor
             class. It is waiting for a request in a regular ServerSocket.accept() method.
             When a request is received, this thread wakes up.
        d2) The ThreadPoolExecutor assigns the a TaskThread to handle the request.
            It also supplies a JMX object name to the catalina container (not used I believe)
        d3) The processor to handle the request in this case is Coyote Http11Processor,
            and the process method is invoked.
            This same processor is also continuing to check the input stream of the socket
            until the keep alive point is reached or the connection is disconnected.
        d4) The HTTP request is parsed using an internal buffer class (Http11InputBuffer)
            The buffer class parses the request line, the headers, etc and store the result in a
            Coyote request (not an HTTP request) This request contains all the HTTP info, such
            as servername, port, scheme, etc.
        d5) The processor contains a reference to an Adapter, in this case it is the
            CoyoteAdapter. Once the request has been parsed, the Http11Processor
            invokes service() on the adapter. In the service method, the Request contains a
            CoyoteRequest and CoyoteResponse (null for the first time)
            The CoyoteRequest(Response) implements HttpRequest(Response) and HttpServletRequest(Response)
            The adapter parses and associates everything with the request, cookies, the context through a
            Mapper, etc
        d6) When the parsing is finished, the CoyoteAdapter invokes its container (StandardEngine)
            and invokes the invoke(request,response) method.
            This initiates the HTTP request into the Catalina container starting at the engine level
        d7) The StandardEngine.invoke() simply invokes the container pipeline.invoke()
        d8) By default the engine only has one valve the StandardEngineValve, this valve simply
            invokes the invoke() method on the Host pipeline (StandardHost.getPipeLine())
        d9) the StandardHost has two valves by default, the StandardHostValve and the ErrorReportValve
        d10) The standard host valve associates the correct class loader with the current thread
             It also retrieves the Manager and the session associated with the request (if there is one)
             If there is a session access() is called to keep the session alive
        d11) After that the StandardHostValve invokes the pipeline on the context associated
             with the request.
        d12) The first valve that gets invoked by the Context pipeline is the FormAuthenticator
             valve. Then the StandardContextValve gets invoke.
             The StandardContextValve invokes any context listeners associated with the context.
             Next it invokes the pipeline on the Wrapper component (StandardWrapperValve)
        d13) During the invocation of the StandardWrapperValve, the JSP wrapper (Jasper) gets invoked
             This results in the actual compilation of the JSP.
             And then invokes the actual servlet.
    e) Invocation of the servlet class

 Google翻译

 

Tomcat启动序列

序列1.从命令行
类开始:org.apache.catalina.startup.Bootstrap
它的作用:
    a)设置类加载器
        commonLoader(公共)-> System Loader 
        sharedLoader(共享)-> commonLoader-> System Loader 
        catalinaLoader (服务器)-> commonLoader->系统加载器
        (默认情况下,commonLoader用于
         sharedLoader和serverLoader)
    b)加载启动类(反射)
        org.apache.catalina.startup.Catalina 
        setParentClassloader-> sharedLoader
        Thread.contextClassloader-> catalinaLoader 
    c)Bootstrap.daemon.init()完成

序列2。处理命令行参数(开始,停止)
类:org.apache.catalina.startup.Bootstrap(假定命令->开始)
它的作用:
    a)Catalina.setAwait(true); 
    b)Catalina.load()
        b1)initDirs()->设置属性,例如
                          catalina.home 
                          catalina.base == catalina.home(大多数情况)
        b2)initNaming 
            setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
                    org.apache.naming .java.javaURLContextFactory->默认值)
        b3)createStartDigester()
            为主要的server.xml元素(例如
            org.apache.catalina.core.StandardServer)配置摘要(当然可以更改:)
            org.apache.catalina.deploy.NamingResources
                将命名资源存储在J2EE JNDI树
            org.apache.catalina中。 LifecycleListener
                为主要组件
            org.apache.catalina.core.StandardService
                的启动/停止实现事件。一组连接器的单个条目,
                以便容器可以侦听多个连接器,
                即,单个条目
            org.apache.catalina.Connector
                连接器监听传入请求只有
            它也增加了以下规则集到蒸煮
                NamingRuleSet 
                EngineRuleSet 
                HostRuleSet 
                ContextRuleSet 
        b4)中加载的server.xml并使用消化器解析它
            解析使用蒸煮器的自动在server.xml 
            XML对象的映射工具,将创建server.xml中定义的对象
            。实际容器的启动尚未开始。
        b5)将System.out和System.err分配给SystemLogHandler类
        b6)在所有组件上调用初始化,这使每个对象都向
            JMX代理注册。
            在调用过程中,连接器还会初始化适配器。
            适配器是执行请求预处理的组件。
            典型的适配器是HTTP1.1(如果未指定协议,则默认为
            org.apache.coyote.http11.Http11NioProtocol),
            用于mod_jk的AJP1.3。c 

    )Catalina.start()
        c1)启动NamingContext并将所有JNDI引用绑定到其中
        c2)在<Server>下启动服务,它们是:
            StandardService->启动Engine(ContainerBase-> Realm,Cluster等)
        C3)StandardHost(由服务启动)
                配置的ErrorReportValve做不同的HTTP适当的HTML输出
                错误代码
                开始在管道中的阀门(至少ErrorReportValve)
                配置的StandardHostValve,
                    这个阀门关系webapp的类加载器线程上下文
                    它还查找请求的会话
                    并调用上下文管道。
                启动HostConfig组件。
                    该组件部署所有webapp 
                        (webapps和conf / Catalina / localhost / *。xml)。
                    HostConfig将为您的上下文创建一个摘要器,
                    然后该摘要器将调用ContextConfig.start()
                        ContextConfig.start()将处理默认的web.xml(conf / web.xml)
                        ,然后处理应用程序web.xml(WEB- INF / web.xml)

        c4)在容器(StandardEngine)的生存期内,有一个后台线程
            不断检查上下文是否已更改。如果上下文发生更改(战争文件,
            上下文xml文件,web.xml的时间戳),则发出重新加载操作(停止/删除/部署/启动)

    d)Tomcat在HTTP端口上收到请求
        d1)该请求由一个单独的线程接收,该线程正在ThreadPoolExecutor
             类中等待。它正在等待常规ServerSocket.accept()方法中的请求。
             收到请求后,该线程将唤醒。
        d2)ThreadPoolExecutor分配一个TaskThread来处理请求。
            它还向catalina容器提供了JMX对象名(我相信不使用)
        d3)在这种情况下,处理请求的处理器是Coyote Http11Processor,
            并且调用了处理方法。
            该同一处理器还将继续检查套接字的输入流,
            直到达到保持活动点或断开连接为止。
        d4)使用内部缓冲区类(Http11InputBuffer)解析HTTP请求
            缓冲区类解析请求行,标头等,并将结果存储在
            Coyote请求(不是HTTP请求)中。该请求包含所有HTTP信息,例如
            作为服务器名,端口,方案等
        D5)处理器包含一个适配器的引用,在此情况下它是
            CoyoteAdapter。解析请求后,Http11Processor会
            在适配器上调用service()。在服务方法中,请求包含
            CoyoteRequest和CoyoteResponse(首次为空)
            CoyoteRequest(Response)实现HttpRequest(Response)和HttpServletRequest(Response)
            适配器通过
            Mapper解析所有内容并将其与请求,cookie,上下文等相关联
        d6)解析完成后,CoyoteAdapter调用其容器(StandardEngine)
            并调用invoke(request,response)方法。
            这会从引擎级别
        d7开始向Catalina容器发起HTTP请求。StandardEngine.invoke()仅调用容器管道
        。invoke ()d8)默认情况下,引擎只有一个阀StandardEngineValve,该阀简单
            调用主机管道上的invoke()方法(StandardHost.getPipeLine()
        d9),StandardHost默认具有两个阀门,StandardHostValve和ErrorReportValve 
        d10)标准宿主阀门将正确的类加载器与当前线程相关联,
             它还会检索Manager和与请求关联的会话(如果存在)(如果存在),
             如果存在会话,则调用access()使会话保持活动状态
        d11)之后,StandardHostValve会在
             与请求关联的上下文上调用管道。
        d12)上下文管道调用的第一个阀是FormAuthenticator
             阀门。然后,调用StandardContextValve。
             StandardContextValve调用与上下文关联的所有上下文侦听器。
             接下来,它调用Wrapper组件(StandardWrapperValve)上的管道
        d13)在调用StandardWrapperValve期间,将调用JSP包装器(Jasper)。
             这将导致JSP的实际编译。
             然后调用实际的servlet。
    e)调用Servlet类
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值