blob: 2443812101cc867c9b3d0e2629821b7c17aba35a [file] [log] [blame]
Hongqing Liufd5ee812014-05-10 16:32:51 +08001 Licensed to the Apache Software Foundation (ASF) under one or more
2 contributor license agreements. See the NOTICE file distributed with
3 this work for additional information regarding copyright ownership.
4 The ASF licenses this file to You under the Apache License, Version 2.0
5 (the "License"); you may not use this file except in compliance with
6 the License. You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
刘洪青6266f992017-05-15 21:21:03 +080016Tomcat Startup Sequence
Hongqing Liufd5ee812014-05-10 16:32:51 +080017
18Sequence 1. Start from Command Line
19Class: org.apache.catalina.startup.Bootstrap
20What it does:
刘洪青6266f992017-05-15 21:21:03 +080021 a) Set up classloaders
22 commonLoader (common)-> System Loader
23 sharedLoader (shared)-> commonLoader -> System Loader
24 catalinaLoader(server) -> commonLoader -> System Loader
25 (by default the commonLoader is used for the
26 sharedLoader and the serverLoader)
27 b) Load startup class (reflection)
28 org.apache.catalina.startup.Catalina
29 setParentClassloader -> sharedLoader
30 Thread.contextClassloader -> catalinaLoader
31 c) Bootstrap.daemon.init() complete
Hongqing Liufd5ee812014-05-10 16:32:51 +080032
刘洪青6266f992017-05-15 21:21:03 +080033Sequence 2. Process command line argument (start, stop)
34Class: org.apache.catalina.startup.Bootstrap (assume command->start)
35What it does:
36 a) Catalina.setAwait(true);
37 b) Catalina.load()
38 b1) initDirs() -> set properties like
39 catalina.home
40 catalina.base == catalina.home (most cases)
41 b2) initNaming
42 setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
43 org.apache.naming.java.javaURLContextFactory ->default)
44 b3) createStartDigester()
45 Configures a digester for the main server.xml elements like
46 org.apache.catalina.core.StandardServer (can change of course :)
47 org.apache.catalina.deploy.NamingResources
48 Stores naming resources in the J2EE JNDI tree
49 org.apache.catalina.LifecycleListener
50 implements events for start/stop of major components
51 org.apache.catalina.core.StandardService
52 The single entry for a set of connectors,
53 so that a container can listen to multiple connectors
54 ie, single entry
55 org.apache.catalina.Connector
56 Connectors to listen for incoming requests only
57 It also adds the following rulesets to the digester
58 NamingRuleSet
59 EngineRuleSet
60 HostRuleSet
61 ContextRuleSet
62 b4) Load the server.xml and parse it using the digester
63 Parsing the server.xml using the digester is an automatic
64 XML-object mapping tool, that will create the objects defined in server.xml
65 Startup of the actual container has not started yet.
66 b5) Assigns System.out and System.err to the SystemLogHandler class
67 b6) Calls initialize on all components, this makes each object register itself with the
68 JMX agent.
69 During the process call the Connectors also initialize the adapters.
70 The adapters are the components that do the request pre-processing.
71 Typical adapters are HTTP1.1 (default if no protocol is specified,
72 org.apache.coyote.http11.Http11Protocol)
73 AJP1.3 for mod_jk etc.
74
75 c) Catalina.start()
76 c1) Starts the NamingContext and binds all JNDI references into it
77 c2) Starts the services under <Server> which are:
78 StandardService -> starts Engine (ContainerBase -> Realm,Cluster etc)
79 c3) StandardHost (started by the service)
80 Configures a ErrorReportValvem to do proper HTML output for different HTTP
81 errors codes
82 Starts the Valves in the pipeline (at least the ErrorReportValve)
83 Configures the StandardHostValve,
84 this valves ties the Webapp Class loader to the thread context
85 it also finds the session for the request
86 and invokes the context pipeline
87 Starts the HostConfig component
88 This component deploys all the webapps
89 (webapps & conf/Catalina/localhost/*.xml)
90 HostConfig will create a Digester for your context, this digester
91 will then invoke ContextConfig.start()
92 The ContextConfig.start() will process the default web.xml (conf/web.xml)
93 and then process the applications web.xml (WEB-INF/web.xml)
94
95 c4) During the lifetime of the container (StandardEngine) there is a background thread that
96 keeps checking if the context has changed. If a context changes (timestamp of war file,
97 context xml file, web.xml) then a reload is issued (stop/remove/deploy/start)
98
99 d) Tomcat receives a request on an HTTP port
100 d1) The request is received by a separate thread which is waiting in the ThreadPoolExecutor
101 class. It is waiting for a request in a regular ServerSocket.accept() method.
102 When a request is received, this thread wakes up.
103 d2) The ThreadPoolExecutor assigns the a TaskThread to handle the request.
104 It also supplies a JMX object name to the catalina container (not used I believe)
105 d3) The processor to handle the request in this case is Coyote Http11Processor,
106 and the process method is invoked.
107 This same processor is also continuing to check the input stream of the socket
108 until the keep alive point is reached or the connection is disconnected.
109 d4) The HTTP request is parsed using an internal buffer class (Http11InputBuffer)
110 The buffer class parses the request line, the headers, etc and store the result in a
111 Coyote request (not an HTTP request) This request contains all the HTTP info, such
112 as servername, port, scheme, etc.
113 d5) The processor contains a reference to an Adapter, in this case it is the
114 CoyoteAdapter. Once the request has been parsed, the Http11Processor
115 invokes service() on the adapter. In the service method, the Request contains a
116 CoyoteRequest and CoyoteResponse (null for the first time)
117 The CoyoteRequest(Response) implements HttpRequest(Response) and HttpServletRequest(Response)
118 The adapter parses and associates everything with the request, cookies, the context through a
119 Mapper, etc
120 d6) When the parsing is finished, the CoyoteAdapter invokes its container (StandardEngine)
121 and invokes the invoke(request,response) method.
122 This initiates the HTTP request into the Catalina container starting at the engine level
123 d7) The StandardEngine.invoke() simply invokes the container pipeline.invoke()
124 d8) By default the engine only has one valve the StandardEngineValve, this valve simply
125 invokes the invoke() method on the Host pipeline (StandardHost.getPipeLine())
126 d9) the StandardHost has two valves by default, the StandardHostValve and the ErrorReportValve
127 d10) The standard host valve associates the correct class loader with the current thread
128 It also retrieves the Manager and the session associated with the request (if there is one)
129 If there is a session access() is called to keep the session alive
130 d11) After that the StandardHostValve invokes the pipeline on the context associated
131 with the request.
132 d12) The first valve that gets invoked by the Context pipeline is the FormAuthenticator
133 valve. Then the StandardContextValve gets invoke.
134 The StandardContextValve invokes any context listeners associated with the context.
135 Next it invokes the pipeline on the Wrapper component (StandardWrapperValve)
136 d13) During the invocation of the StandardWrapperValve, the JSP wrapper (Jasper) gets invoked
137 This results in the actual compilation of the JSP.
138 And then invokes the actual servlet.
139 e) Invocation of the servlet class