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