Tomcat - Tomcat 8.5.55 启动过程源码分析阶段三_start阶段
文章目錄
- 啟動流程分析
- Pre
- Star階段
- start總覽
- start源碼分析
- StandardServer Start
- StandardService Start
- StandardEngine Start
- Connector Start
- 小結(jié)
啟動流程分析
Pre
Tomcat - Tomcat 8.5.55 啟動過程源碼分析階段二_load加載初始化
說完了load階段,這里我們繼續(xù)來看下最后一個start階段
Star階段
start總覽
你會發(fā)現(xiàn)和 load階段非常相似
這里我們就不展開的這么詳細(xì)了,梳理核心脈絡(luò)~
start源碼分析
Bootstrap#main -----> daemon.start(); -------反射調(diào)用-------> Catalina # start -------------> getServer().start(); -------模板方法LifeCycleBase--------> startInternal();
還是模板模式
StandardServer Start
這里主要是繼續(xù)啟動Service
StandardService Start
還是會走到生命周期里 ,統(tǒng)一收到LifeCycle接口定義的生命周期管理 抽象類LifeCycleBase ,子類重寫startInternal()
一樣的套路
精簡后的核心代碼如下:
@Overrideprotected void startInternal() throws LifecycleException {// Start our defined Container firstif (engine != null) {synchronized (engine) {engine.start();}}// Start our defined Connectors secondsynchronized (connectorsLock) {for (Connector connector: connectors) {connector.start();}}}分兩大塊
- engine.start()
- connector.start()
接下來逐一分析
StandardEngine Start
大致流程一覽
StandardEngine # startInternal ---------> 調(diào)用父類ContainerBase#startInternal ---------> startStopExecutor 將子容器Host提交到具體的StartChild線程類并行執(zhí)行 —> … 使用事件驅(qū)動 初始化 Servlet
仔細(xì)看看吧
繼續(xù) LifeCycleBase # startInternal();
調(diào)用 StandardEngine
@Overrideprotected synchronized void startInternal() throws LifecycleException {// Standard container startupsuper.startInternal();}
父類 ContainerBase # startInternal 的方法
看下調(diào)用棧 也能看出
跟進(jìn)去 關(guān)鍵代碼
結(jié)合Server.xml配置文件 Engine節(jié)點(diǎn)信息
// 查找子容器,啟動子容器 // Start our child containers, if anyContainer children[] = findChildren();List<Future<Void>> results = new ArrayList<>();for (Container child : children) {results.add(startStopExecutor.submit(new StartChild(child)));}
children事實(shí)上就是Host的集合, 然后 startStopExecutor 提交啟動任務(wù) ,這個startStopExecutor就是在load節(jié)點(diǎn)初始化好的,這里來使用。
那就看 StartChild 線程唄
繼續(xù)走
跟進(jìn)去 還是 LifyCycleBean --> StandardHost # startInternal -----> super.startInternal(); ----> setState(LifecycleState.STARTING);
設(shè)置生命周期 觸發(fā)實(shí)例化Context —> LifyCycleBean # setStateInternal -----> fireLifecycleEvent(lifecycleEvent, data);
觸發(fā)Host的生命周期事件后,將后續(xù)工作交給生命周期監(jiān)聽器HostConfig來進(jìn)行,Hostconfig#lifecycleEvent方法,捕獲start事件,執(zhí)行start方法
緊接著就是下面的流程了
-------> Hostconfig#deployApps--------------> Hostconfig#deployDirectories ,以線程方式并行處理多個項(xiàng)目 ---------> DeployDirectory 線程類 ------>Hostconfig#deployDirectory-----------------------> 通過xml解析對象進(jìn)行分析,設(shè)置一些context應(yīng)用的必要屬性 ,在addChild方法中完善context的過程 -------> ContainerBase#addChild方法--------------> ContainerBase#addChildInternal---------> StandardContext#startInternal方法 ------------> 給每個應(yīng)用設(shè)置類加載器 ,把具體每個應(yīng)用的處理交給了ContextConfig . loadOnStartup方法根據(jù)web.xml中配置servlet的load-on-startup來進(jìn)行創(chuàng)建實(shí)例化對應(yīng)servlet。執(zhí)行之后,instance就有具體對象了。 ------> StandardContext#loadOnStartup方法 ------> StandardWrapper#load方法 ---------------------> StandardWrapper#loadServlet方法
Connector Start
主要流程
Connector# startInternal ------> AbstractProtocol# start -----------> AbstractEndpoint # start ----------> NioEndpoint# startInternal ----------> AbstractEndpoint#startAcceptorThreads ------> NioEndpoint# createAcceptor -------> Acceptor線程#run方法 Socket.Accept
小結(jié)
總結(jié)
以上是生活随笔為你收集整理的Tomcat - Tomcat 8.5.55 启动过程源码分析阶段三_start阶段的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tomcat - Tomcat 8.5.
- 下一篇: Tomcat - 源码分析Tomcat是