【tomcat啟動(dòng)類(lèi)Bootstrap】
t1.tomcat的人口函數,啟動(dòng)類(lèi)
org.apache.catalina.startup. Bootstrap.java Main函數
t2.Bootstrap類(lèi)
初始化 ClassLoader, 然后利用 Java Reflection 調用 Catalina 類(lèi)來(lái)啟動(dòng) tomcat server
【tomcat擴展-日志】
a1.private static Log log = LogFactory.getLog(Bootstrap.class);
日志可以定義為private 和static
a2.只在本類(lèi)中使用的方法,使用private,降低訪(fǎng)問(wèn)權限,需要的時(shí)候,再考慮重構或者提高訪(fǎng)問(wèn)權限public
a.日志打印前加if(log.isInfoEnabled())
如果代碼中含有logger.debug(“string”);此類(lèi)語(yǔ)句,盡管在log4j.xml配置文件中對該類(lèi)logger的level為 ERROR,但是仍然會(huì )產(chǎn)生較大的內存開(kāi)銷(xiāo),通常是所要輸出的string的幾十倍甚至上百倍大小,對內存開(kāi)銷(xiāo)非常大。優(yōu)化方法為:使用logger進(jìn)行 判斷。
2010-3-30
【tomcat擴展-classloader】
a3.如果兩個(gè)類(lèi)屬于同一個(gè)包下,但是由不同的classloader加載,那么他們也不能互訪(fǎng)default類(lèi)型方法,屬性
a4.classloader:與C或C++編寫(xiě)的程序不同,Java程序并不是一個(gè)可執行文件,而是由許多獨立的類(lèi)文件組成,每一個(gè)文件基本上 對應于一個(gè)類(lèi)。此外,這些類(lèi)文件并非立即全部都裝入內存,而是根據程序需要裝入內存。ClassLoader是JVM中將類(lèi)裝入內存的那部分
a5.定制的ClassLoader應用:
1.在執行非置信代碼之前,自動(dòng)驗證數字簽名
2.使用用戶(hù)提供的密碼透明地解密代碼
3.動(dòng)態(tài)地創(chuàng )建符合用戶(hù)特定需要的定制化構建類(lèi)
4.任何您認為可以生成Java字節碼的內容都可以集成到應用程序中
a6.findClass方法是創(chuàng )建定制的ClassLoader時(shí)唯一需要覆蓋的方法。
ClassLoader loadClass方法
Class c = findLoadedClass(name);
if (c == null) {
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClass0(name);
}
} catch (ClassNotFoundException e) {
// If still not found, then invoke findClass in order
// to find the class.
c = findClass(name);
}
}
a7.ClassLoader(CCL)的任務(wù)是確保代碼被編譯和更新。
下面描述了它的工作方式:1、當請求一個(gè)類(lèi)時(shí),先查看它是否在磁盤(pán)的當前目錄或相應的子目錄。
2、如果該類(lèi)不存在,但源碼中有,那么調用Java編譯器來(lái)生成類(lèi)文件。
3、如果該類(lèi)已存在,檢查它是否比源碼舊。如果是,調用Java編譯器來(lái)重新生成類(lèi)文件。
4、如果編譯失敗,或者由于其它原因不能從現有的源碼中生成類(lèi)文件,返回ClassNotFoundException。
5、如果仍然沒(méi)有該類(lèi),也許它在其它庫中,所以調用findSystemClass來(lái)尋找該類(lèi)。
6、如果還是沒(méi)有,則返回ClassNotFoundException。
7、否則,返回該類(lèi)。
8、調用findLoadedClass來(lái)查看是否存在已裝入的類(lèi)。
9、如果沒(méi)有,那么采用那種特殊的神奇方式來(lái)獲取原始字節。
10、如果已有原始字節,調用defineClass將它們轉換成Class對象。
11、如果沒(méi)有原始字節,然后調用findSystemClass查看是否從本地文件系統獲取類(lèi)。
12、如果resolve參數是true,那么調用resolveClass解析Class對象。
13、如果還沒(méi)有類(lèi),返回ClassNotFoundException。
14、否則,將類(lèi)返回給調用程序。
【tomcat啟動(dòng)類(lèi)classloader】
t3.tomcat自定義了三個(gè)類(lèi),catalinaLoader commonLoader,sharedLoader
Common - 載入$CATALINA_HOME/common/...它們對TOMCAT和所有的WEB APP都可見(jiàn)
Catalina - 載入$CATALINA_HOME/server/..它們僅對TOMCAT可見(jiàn),對所有的WEB APP都不可見(jiàn)
Shared-載入$CATALINA_HOME/shared/它們僅對所有WEB APP可見(jiàn),對TOMCAT不可見(jiàn)(也不必見(jiàn))
t4.Bootstrap通過(guò)反射初始化Catalina類(lèi),
反射調用Catalina方法setParentClassLoader,傳遞SharedClassloader
反射call Catalina方法load 利用server.xml中的配置初始化Service,Server,Engine,Host
反射call Catalina方法start Start the new server 該server是通過(guò) 解析xml文件生成的org.apache.catalina.core.StandardServer類(lèi)
【tomcat-xml解析】
1.Tomcat取了Digester中的interface和幾個(gè)Rule,并且自己實(shí)現了一些 Rule 來(lái)解析xml.
2.tomcat解析xml創(chuàng )建以下幾個(gè)類(lèi)
Server:
org.apache.catalina.core.StandardServer
Resources:
org.apache.catalina.deploy.NamingResources
Server's Listener:( 監聽(tīng)server事件)
org.apache.catalina.core.AprLifecycleListener
org.apache.catalina.core.JasperListener
org.apache.catalina.mbeans.ServerLifecycleListener
org.apache.catalina.mbeans.GlobalResourcesLifecycleListener
Service:
org.apache.catalina.core.StandardService
Executor:
org.apache.catalina.core.StandardThreadExecutor
Engine:
org.apache.catalina.core.StandardEngine
Connector:
org.apache.catalina.connector.Connector
【tomcat-流程】
3.StandardServer啟動(dòng)StandardService,StandardService啟動(dòng)Connector,
Connector啟動(dòng)Http11Protocol,Http11Protocol啟動(dòng)JIoEndpoint,
JioEndpoint啟動(dòng)server Socket,listern 8080端口,處理http請求
4.Http11Processor
Processes HTTP requests.
由http11ConnectionHandler調用,Http11ConnectionHandler由JioEndpoint中的Work 調用
5.A connector passes the request and reponse objects to the Container by calling the Container interface's invoke method
public void invoke(Request request, Response response)
throws IOException, ServletException;
inside the invoke method ,the container loads the servlet class,call its sevice method ,manage sessions,etc.
6.Connector 方法initialize中
// Initializa adapter
adapter = new CoyoteAdapter(this);
protocolHandler.setAdapter(adapter);
adapter通過(guò)protocolHandler(Http11Protocol)傳給Http11Processor,
Http11Processor解析,create request和response,通過(guò)adapter傳送給Container
7.Tomcat使用Pipeline模式在各層容器間傳遞請求,將請求通過(guò)管道依次通過(guò)Engine,Host,Context和 Wrapper。另外,每一個(gè)容器
都可以設置一系列的Valve去對請求進(jìn)行攔 截,就像管道中的閥一樣對請求的行為進(jìn)行一些干涉。
2010-3-31
【tomcat-流程】
1.tomcat的pipeline/valve是標準的責任鏈模式,每個(gè)級別的容器中pipeline所有的valve都完成動(dòng)作后會(huì )將 request/response傳到下一個(gè)容器的pipeline中的valve,
這樣一直傳遞下去直到Wrapper的BaseValve.
Ps:每個(gè)容器的BaseValve會(huì )調用下個(gè)容器的起始valve
2.StandardEngine
屬性Pipeline pipeline = new StandardPipeline(this);
構造函數里會(huì )設置最底層的閥門(mén)
pipeline.setBasic(new StandardEngineValve());
如果需要設置新閥門(mén)處理需求,只需要調用 pipeline.addValve(Valve valve);
3.CoyoteAdapter中會(huì )執行
connector.getContainer().getPipeline().getFirst().invoke(request, response);
該行代碼會(huì )一層一層調用添加的閥門(mén),處理下去.
2010-4-1
【tomcat-流程】
1.jk插件負責tomcat和其它http容器進(jìn)行通信
2.連接器協(xié)議AJP/1.3是tomcat用來(lái)與其它http容器進(jìn)行連接的協(xié)議
3.把指定Context的classloader付給當前線(xiàn)程。
Thread.currentThread().setContextClassLoader(context.getLoader().getClassLoader()); 這樣request就只看見(jiàn)指定的context下面的classes和jar包,而看不見(jiàn)tomcat本身的類(lèi)。
2010-4-7
【tomcat-socke與worker線(xiàn)程】
/**
* Process an incoming TCP/IP connection on the specified socket. Any
* exception that occurs during processing must be logged and swallowed.
* <b>NOTE</b>: This method is called from our Connector's thread. We
* must assign it to our own thread so that multiple simultaneous
* requests can be handled.
* @param socket TCP socket to process
*/
synchronized void assign(Socket socket) {
// Wait for the Processor to get the previous Socket
while (available) {
try {
wait();
} catch (InterruptedException e) {
}
}
// Store the newly available Socket and notify our thread
this.socket = socket;
available = true;
notifyAll();
}
/**
* Await a newly assigned Socket from our Connector, or <code>null</code
* if we are supposed to shut down.
*/
private synchronized Socket await() {
// Wait for the Connector to provide a new Socket
while (!available) {
try {
wait();
} catch (InterruptedException e) {
}
}
// Notify the Connector that we have received this Socket
Socket socket = this.socket;
available = false;
notifyAll();
return (socket);
}
連接器線(xiàn)程調用worker類(lèi)的assign類(lèi),worker類(lèi)的執行線(xiàn)程run方法會(huì )調用await方法獲取socket,通過(guò) available變量的設置和wait/notify方法來(lái)協(xié)調彼此的操作。當連接器線(xiàn)程未傳輸socket,worker類(lèi)線(xiàn)程就執行wait等待,
當worker類(lèi)執行線(xiàn)程在處理忙的時(shí)候,連接器線(xiàn)程wait。
| 一直以來(lái)都是用tomcat做web服務(wù)器進(jìn)行開(kāi)發(fā),很想知道其內部的一些原理和實(shí)現(雖然很多人說(shuō)知道原理又不能當飯吃)。今天跟蹤調試了源碼的啟動(dòng)程序(關(guān)于源碼的eclipse的導入以及ant的構建,網(wǎng)上有大把的資料,google一下就ok了),總算知道點(diǎn)大致的啟動(dòng)步驟,有些淺顯的領(lǐng)悟分享一下: org.apache.catalina.startup.Bootstrap作為啟動(dòng)入口,此類(lèi)的main函數: 1.首先創(chuàng )建一個(gè)自身的實(shí)例(如果此類(lèi)的實(shí)例變量:private static Bootstrap daemon為空的話(huà));
2.然后調用init()方法,此方法的主要工作就是反射得到org.apache.catalina.startup.Catalina的實(shí)例,同時(shí)把classLoader通過(guò)Catalina的setParentClassLoader方法賦值。
3.接著(zhù)調用load(args)方法,此方法反射調用Catalina的load(args),根據傳遞的參數設置部分屬性值之后,會(huì )接著(zhù)執行Catalina的load()方法。 在這個(gè)無(wú)參的方法里通過(guò)Digester組件(Jakarta Commons Digester是apache的一個(gè)解析處理xml的開(kāi)源項目,開(kāi)始我還以為巧合,因為struts的ActionServlet也是使用此組件解析,后來(lái)才知道tomcat的一個(gè)主力也是struts的創(chuàng )造者之一)解析conf/server.xml等xml文件,初始化一系列的參數。
4.接著(zhù)Bootstrap的start()方法反射調用Catalina的start()方法,此方法通過(guò)調用xml解析處理后得到的server:org.apache.catalina.core.StandardServer的start()方法進(jìn)入其所擁有的Lifecycle service:org.apache.catalina.core.StandardService的start()方法,接著(zhù)調用Lifecycle Container:org.apache.catalina.core.StandardEngine的start()方法,初始化一堆東西之后到達旅途的終點(diǎn),StandardEngine的父類(lèi):org.apache.catalina.core.ContainerBase的start(),這個(gè)方法是通過(guò)自身的threadStart()方法啟動(dòng)了一個(gè)容器線(xiàn)程: thread = new Thread(new ContainerBackgroundProcessor(), threadName); thread.setDaemon(true); thread.start();
5.上面的繁文的簡(jiǎn)化大致是:Bootstrap(init,load,start)--> Catalina(start)--> StandardServer(start)--> StandardService(start)--> StandardEngine(start)--> ContainerBase(start, threadStart)
至此終于看到雙擊.bat或者執行.sh背后的一點(diǎn)點(diǎn)東西了(我是跟蹤的6.0.20的源碼,可能之前的版本的有所不同)。 |