Skip to content

Commit 8f11c73

Browse files
committed
谈谈 Tomcat 架构及启动过程[含部署]:v1.0
- 未包含请求处理相关类的介绍,该部分内容会在 v2.0 中添加
1 parent 4773eb7 commit 8f11c73

11 files changed

+636
-0
lines changed

reading-notes/谈谈 Tomcat 架构及启动过程[含部署].md

Lines changed: 142 additions & 0 deletions
Large diffs are not rendered by default.

res/tomcat-architecture.png

34.6 KB
Loading

res/tomcat-architecture.pu

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
@startuml
2+
3+
class Bootstrap
4+
5+
class Catalina
6+
7+
class StandardServer
8+
9+
class StandardService
10+
11+
class Connector
12+
interface Container
13+
class ContainerBase
14+
Container <|.. ContainerBase
15+
class StandardEngine
16+
class StandardHost
17+
class StandardContext
18+
class StandardWrapper
19+
class StandardPipeline
20+
21+
Bootstrap ..> Catalina
22+
23+
Catalina *-- StandardServer
24+
StandardServer "1" *-- "*" StandardService
25+
StandardService "1" *-- "*" Connector
26+
StandardService *-- StandardEngine
27+
StandardEngine "1" *-- "*" StandardHost
28+
StandardHost "1" *-- "*" StandardContext
29+
StandardContext "1" *-- "*" StandardWrapper
30+
31+
32+
ContainerBase <|-- StandardEngine
33+
ContainerBase <|-- StandardHost
34+
ContainerBase <|-- StandardContext
35+
ContainerBase <|-- StandardWrapper
36+
ContainerBase *-- StandardPipeline
37+
38+
footer
39+
图 1 Tomcat architecture
40+
endfooter
41+
42+
@enduml

res/tomcat-background-thread.png

48.5 KB
Loading

res/tomcat-background-thread.pu

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@startuml
2+
3+
activate ContainerBackgroundProcessor
4+
ContainerBackgroundProcessor -> ContainerBackgroundProcessor : processChildren(StandardEngine);
5+
activate ContainerBackgroundProcessor
6+
ContainerBackgroundProcessor -> StandardEngine : backgroundProcess()
7+
activate StandardEngine
8+
StandardEngine -> Cluster: backgroundProcess()
9+
StandardEngine -> Realm : backgroundProcess();
10+
StandardEngine -> Valve : backgroundProcess();
11+
deactivate StandardEngine
12+
ContainerBackgroundProcessor -> ContainerBackgroundProcessor : processChildren(StandardHost)
13+
activate ContainerBackgroundProcessor
14+
ContainerBackgroundProcessor -> StandardHost : backgroundProcess()
15+
activate StandardHost
16+
StandardHost -> HostConfig : fireLifecycleEvent(PERIODIC_EVENT)
17+
activate HostConfig
18+
HostConfig -> HostConfig : check()
19+
activate HostConfig
20+
HostConfig -> HostConfig : deployApps()[Hotdeploy application]
21+
deactivate HostConfig
22+
deactivate HostConfig
23+
deactivate StandardHost
24+
ContainerBackgroundProcessor -> ContainerBackgroundProcessor : processChildren(StandardContext)
25+
activate StandardContext
26+
ContainerBackgroundProcessor -> StandardContext : backgroundProcess()
27+
StandardContext -> WebappLoader : backgroundProcess();
28+
activate WebappLoader
29+
WebappLoader -> StandardContext : reload()
30+
activate StandardContext
31+
StandardContext -> StandardContext : setPaused(true);
32+
StandardContext -> StandardContext : stop();
33+
StandardContext -> StandardContext : start();
34+
StandardContext -> StandardContext : setPaused(false);
35+
deactivate StandardContext
36+
deactivate WebappLoader
37+
deactivate StandardContext
38+
deactivate ContainerBackgroundProcessor
39+
deactivate ContainerBackgroundProcessor
40+
deactivate ContainerBackgroundProcessor
41+
42+
footer
43+
图 5 background thread
44+
endfooter
45+
46+
@enduml

res/tomcat-context-start.png

141 KB
Loading

res/tomcat-context-start.pu

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
@startuml
2+
3+
activate StandardHost
4+
StandardHost -> StandardContext : Start()
5+
activate StandardContext
6+
StandardContext -> StandardContext : init()
7+
activate StandardContext
8+
StandardContext -> StandardContext : reconfigureStartStopExecutor()
9+
deactivate StandardContext
10+
StandardContext -> ContextConfig : lifecycleEvent(BEFORE_START_EVENT)
11+
activate ContextConfig
12+
ContextConfig -> ContextConfig : beforeStart();
13+
activate ContextConfig
14+
deactivate ContextConfig
15+
deactivate ContextConfig
16+
StandardContext -> StandardContext : startInternal()
17+
activate StandardContext
18+
StandardContext -> StandardRoot : setResource(new StandardRoot(this);)
19+
StandardContext -> StandardContext : resourceStart();
20+
activate StandardContext
21+
StandardContext -> StandardRoot : start()
22+
activate StandardRoot
23+
StandardRoot -> StandardRoot : init();
24+
StandardRoot -> StandardRoot : startInternal();
25+
activate StandardRoot
26+
StandardRoot -> StandardRoot : createMainResourceSet()
27+
activate StandardRoot
28+
DirResourceSet --> StandardRoot : new DirResourceSet
29+
deactivate StandardRoot
30+
StandardRoot -> StandardRoot : processWebInfLib();
31+
activate StandardRoot
32+
JarResourceSet --> StandardRoot : new JarResourceSet for *.jar in /WEB-INF/lib/
33+
deactivate StandardRoot
34+
deactivate StandardRoot
35+
deactivate StandardRoot
36+
deactivate StandardContext
37+
StandardContext -> StandardHost : getParentClassLoader()
38+
StandardHost --> StandardContext : sharedClassLoader
39+
StandardContext -> StandardContext : setLoader(new WebappLoader(sharedClassLoader))
40+
StandardContext -> StandardContext : postWorkDirectory();
41+
activate StandardContext
42+
StandardContext -> StandardContext : create work directory[work/<Engine>/<Host>/<Context>/]
43+
StandardContext -> StandardContext : getServletContext()
44+
activate StandardContext
45+
activate ApplicationContext
46+
ApplicationContextFacade --> ApplicationContext : new ApplicationContextFacade
47+
deactivate ApplicationContext
48+
StandardContext <-- ApplicationContext : new ApplicationContext(this)
49+
deactivate StandardContext
50+
deactivate StandardContext
51+
StandardContext -> WebappLoader : start()
52+
activate WebappLoader
53+
WebappLoader -> WebappLoader : createClassLoader()
54+
activate WebappLoader
55+
WebappLoader -> ParallelWebappClassLoader : newInstance();
56+
WebappLoader -> ParallelWebappClassLoader : config
57+
WebappLoader -> ParallelWebappClassLoader : start();
58+
activate ParallelWebappClassLoader
59+
ParallelWebappClassLoader -> ParallelWebappClassLoader : config repository [/WEB-INF/classes/ && /WEB-INF/lib/*.jar]
60+
deactivate ParallelWebappClassLoader
61+
deactivate WebappLoader
62+
deactivate WebappLoader
63+
StandardContext -> ContextConfig : lifecycleEvent(CONFIGURE_START_EVENT)
64+
activate ContextConfig
65+
ContextConfig -> ContextConfig : configureStart();
66+
activate ContextConfig
67+
ContextConfig -> ContextConfig : webConfig();
68+
activate ContextConfig
69+
ContextConfig -> ContextConfig : getDefaultWebXmlFragment()
70+
activate ContextConfig
71+
ContextConfig -> ContextConfig : getGlobalWebXmlSource()[conf/web.xml]
72+
ContextConfig -> ContextConfig : getHostWebXmlSource()[conf/<Engine>/<Host>/web.xml.default]
73+
deactivate ContextConfig
74+
ContextConfig -> WebXml : createWebXml();
75+
ContextConfig -> ContextConfig : getContextWebXmlSource()
76+
activate ContextConfig
77+
ContextConfig -> StandardRoot : getResource()[/WEB-INF/web.xml]
78+
StandardRoot --> ContextConfig : contextWebXml
79+
deactivate ContextConfig
80+
ContextConfig -> WebXmlParser : parseWebXml()[create Servlet,Listener, Filter ,etc,and add to StandardContext]
81+
ContextConfig -> ContextConfig : processAnnotationsWebResource() \n [analysis annotation of classes in /WEB-INF/classes] \n if the class has @WebServlet,@WebFilter,@WebListener annotation,\n then add to WebXml
82+
ContextConfig -> ContextConfig : merge global webxml and context webxml
83+
ContextConfig -> StandardContext : configureContext()
84+
activate ContextConfig
85+
deactivate ContextConfig
86+
ContextConfig -> StandardContext : setConfigured(true)
87+
deactivate ContextConfig
88+
deactivate ContextConfig
89+
deactivate ContextConfig
90+
StandardContext -> StandardWrapper : start();
91+
activate StandardWrapper
92+
deactivate StandardWrapper
93+
StandardContext -> StandardPipeline : start();
94+
activate StandardPipeline
95+
StandardPipeline -> Valve : start()
96+
deactivate StandardPipeline
97+
98+
deactivate StandardContext
99+
deactivate StandardContext
100+
101+
footer
102+
图 4 Tomcat Context start
103+
endfooter
104+
105+
@enduml

res/tomcat-init.png

241 KB
Loading

res/tomcat-init.pu

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
@startuml
2+
3+
actor user
4+
5+
activate CatalinaProperties
6+
activate ClassLoaderFactory
7+
CatalinaProperties -> CatalinaProperties : loadProperties();
8+
9+
user -> Bootstrap : main()
10+
activate Bootstrap
11+
Bootstrap -> Bootstrap : init()
12+
activate Bootstrap
13+
Bootstrap -> Bootstrap : initClassLoaders()
14+
activate Bootstrap
15+
Bootstrap -> Bootstrap: createClassLoader()
16+
activate Bootstrap
17+
Bootstrap -> CatalinaProperties : getProperty("common.loader")
18+
Bootstrap <-- CatalinaProperties : common.loader
19+
Bootstrap -> ClassLoaderFactory : createClassLoader();
20+
Bootstrap <-- ClassLoaderFactory : classLoader
21+
deactivate Bootstrap
22+
deactivate Bootstrap
23+
Bootstrap -> Catalina : startupClass.newInstance();
24+
activate Catalina
25+
Bootstrap <-- Catalina : a instance of Catalina
26+
Bootstrap -> Catalina : setParentClassLoader(sharedLoader)
27+
deactivate Bootstrap
28+
Bootstrap -> Bootstrap : setAwait(true)
29+
activate Bootstrap
30+
Bootstrap -> Catalina : setAwait(true)
31+
deactivate Bootstrap
32+
Bootstrap -> Bootstrap : load();
33+
activate Bootstrap
34+
Bootstrap -> Catalina : load();
35+
Catalina -> Catalina : initDirs();
36+
Catalina -> Catalina : initNaming();
37+
Catalina -> Catalina : createStartDigester();
38+
activate Catalina
39+
Catalina -> Catalina : config digester rule set for server.xml
40+
Catalina -> Digester : parse(); parse from config/server.xml
41+
activate Digester
42+
Digester -> StandardServer : newInstance();
43+
activate StandardServer
44+
StandardServer -> StandardServer : set port and shutdown property
45+
StandardServer -> Catalina : setServer();
46+
Digester -> Listener : newInstance();
47+
activate Listener
48+
Listener -> Listener : set property
49+
Listener -> StandardServer : addLifeCycleListener()
50+
deactivate Listener
51+
Digester -> NamingResourcesImpl : newInstance()
52+
activate NamingResourcesImpl
53+
NamingResourcesImpl -> NamingResourcesImpl : setProperty
54+
NamingResourcesImpl -> StandardServer : setGlobalNamingResources();
55+
deactivate NamingResourcesImpl
56+
Digester -> StandardService : new Instance
57+
activate StandardService
58+
StandardService -> StandardService : set name property;
59+
StandardService -> StandardServer : addService();
60+
activate StandardServer
61+
StandardServer -> StandardService : setServer()
62+
deactivate StandardServer
63+
Digester -> Connector : newInstance()
64+
activate Connector
65+
Connector -> Http11NioProtocol : newInstance()
66+
activate Http11NioProtocol
67+
activate NioEndPoint
68+
activate NioSelectorPool
69+
NioEndPoint -> Http11NioProtocol : new NioEndPoint();
70+
deactivate Http11NioProtocol
71+
Connector -> Connector : set port,protocol,etc property
72+
Connector -> StandardService : addConnector()
73+
Digester -> StandardEngine : newInstance();
74+
activate StandardEngine
75+
StandardEngine -> Engine.StandardPipeline : new StandardPipeline(this);
76+
activate Engine.StandardPipeline
77+
Engine.StandardPipeline -> StandardEngine : setContainer()
78+
Engine.StandardPipeline -> StandardEngineValve : setBasic()
79+
activate StandardEngineValve
80+
deactivate StandardEngineValve
81+
deactivate Engine.StandardPipeline
82+
StandardEngine -> StandardEngine : addLifecycleListener(EngineConfig)
83+
StandardEngine -> StandardEngine : setproperty
84+
StandardEngine -> StandardService : setContainer()
85+
86+
Digester -> LockOutRealm : newInstance()
87+
LockOutRealm -> StandardEngine : setRealm();
88+
89+
Digester -> StandardHost : newInstance();
90+
activate StandardHost
91+
StandardHost -> Host.StandardPipeline : new StandardPipeline(this);
92+
activate Host.StandardPipeline
93+
Host.StandardPipeline -> StandardHost : setContainer();
94+
Host.StandardPipeline -> StandardHostValve : setBasic();
95+
activate StandardHostValve
96+
deactivate StandardHostValve
97+
deactivate Host.StandardPipeline
98+
StandardHost -> StandardEngine : getParentClassLoader();
99+
StandardEngine -> StandardService : getParentClassLoader();
100+
StandardService -> StandardServer : getParentClassLoader();
101+
StandardServer -> Catalina : getParentClassLoader();
102+
Catalina --> StandardHost : sharedClassLoader
103+
StandardHost -> StandardHost : setParentClassLoader()
104+
StandardHost -> StandardHost : addLifecycleListener(HostConfig);
105+
StandardHost -> StandardHost : setProperty
106+
StandardHost -> StandardEngine : addChild();
107+
activate StandardEngine
108+
StandardEngine -> StandardHost : setParent();
109+
deactivate StandardEngine
110+
Digester -> AccessLogValve : newInstance();
111+
activate AccessLogValve
112+
AccessLogValve -> AccessLogValve : setProperty
113+
AccessLogValve -> StandardHost : addValve()
114+
deactivate AccessLogValve
115+
deactivate Digester
116+
Catalina -> StandardServer : setCatalina, setCatalinaBase, setCatalinaHome
117+
StandardServer -> StandardServer: initStream();
118+
Catalina -> StandardServer : init();
119+
activate StandardServer
120+
StandardServer -> StandardServer : initInternal()
121+
activate StandardServer
122+
StandardServer -> StandardService : init()
123+
activate StandardService
124+
StandardService -> StandardService : initInternal()
125+
activate StandardService
126+
StandardService -> StandardEngine : init()
127+
activate StandardEngine
128+
StandardEngine -> StandardEngine : reconfigureStartStopExecutor()
129+
deactivate StandardEngine
130+
StandardService -> Connector : init()
131+
activate Connector
132+
Connector -> Connector : new CoyoteAdapter(this);
133+
Connector -> Http11NioProtocol : init();
134+
activate Http11NioProtocol
135+
Http11NioProtocol -> Http11NioProtocol : getName();
136+
Http11NioProtocol -> NioEndPoint : setName()
137+
activate NioEndPoint
138+
NioEndPoint -> NioEndPoint : bind();
139+
activate NioEndPoint
140+
NioEndPoint -> NioEndPoint : initServerSocket();
141+
NioEndPoint -> NioSelectorPool : open();
142+
deactivate NioEndPoint
143+
deactivate NioEndPoint
144+
deactivate Http11NioProtocol
145+
deactivate Connector
146+
deactivate StandardService
147+
deactivate StandardService
148+
deactivate StandardServer
149+
deactivate StandardServer
150+
deactivate Catalina
151+
deactivate Bootstrap
152+
Bootstrap -> Bootstrap : start();
153+
deactivate Bootstrap
154+
155+
footer
156+
图 2 Tomcat init
157+
endfooter
158+
159+
@enduml

res/tomcat-start.png

151 KB
Loading

0 commit comments

Comments
 (0)