How tomcat works 讀書筆記十四 服務器組件和服務組件
來源:程序員人生 發布時間:2015-01-06 09:04:19 閱讀次數:3285次
之前的項目還是有些問題的,例如
1 只能有1個連接器,只能處理http要求,沒法添加另外1個連接器用來處理https。
2 對容器的關閉只能是粗魯的關閉Bootstrap。
org.apache.catalina.Server接口的實例用來表示Catalina的全部servlet引擎。
我們使用Server就是由于,它用1種優雅的方式來啟動/關閉全部系統。
下面是啟動和停止機制是如何工作的。當
服務器啟動的時候,它啟動它內部的所有組件。然后無窮期的等待關閉命令,如果你想要關閉系統,發送1個關閉命令道指定端口便可。當
服務器收到正確的關閉指令后,它停止所有組件的服務。
服務器還使用了另外1個組件,即服務組件,它用來持有組件,例如容器或1個多個的連接器。服務組件將在本章的 service 小節中介紹。(注意有
服務器組件,也有服務組件,這兩個是不1樣的。)
Server接口中屬性 shutdown 用來持有1個停止服務的指令。屬性 port 則是
服務器等待關閉命令的端口??梢哉{用
服務器的 addService 方法將服務添加到
服務器。使用removeService 方法將服務刪除。findServices 返回所有
服務器中所有的服務。
StandardServer類
StandardServer類是Server接口的標準實現。在這里,我們主要介紹它的4個方法。initialize(),start(),stop(),await();
我們1個1個來講。
initialize方法
public void initialize() throws LifecycleException {
if (initialized)
throw new LifecycleException (
sm.getString("standardServer.initialize.initialized"));
initialized = true;
// Initialize our defined Services
for (int i = 0; i < services.length; i++) {
services[i].initialize();
}
}
我們能看到這里使用了1個initialized來標識
服務器是不是已初始化,同時初始化它所包括的服務組件。
另外在stop中,initialized并沒有更改,因此如果
服務器先初始化,再關閉,等再次初始化的時候會拋出異常。
start方法
public void start() throws LifecycleException {
// Validate and update our current component state
if (started)
throw new LifecycleException
(sm.getString("standardServer.start.started"));
// Notify our interested LifecycleListeners
...
started = true;
// Start our defined Services
synchronized (services) {
for (int i = 0; i < services.length; i++) {
if (services[i] instanceof Lifecycle)
((Lifecycle) services[i]).start();
}
}
// Notify our interested LifecycleListeners
...
}
和初始化干的事情差不多,循環啟動它所關聯的服務組件。另外在stop方法中,started會被置為false。
stop方法
public void stop() throws LifecycleException {
// Validate and update our current component state
if (!started)
throw new LifecycleException
(sm.getString("standardServer.stop.notStarted"));
// Notify our interested LifecycleListeners
...
started = false;
// Stop our defined Services
for (int i = 0; i < services.length; i++) {
if (services[i] instanceof Lifecycle)
((Lifecycle) services[i]).stop();
}
// Notify our interested LifecycleListeners
...
}
關閉所有的服務組件。
await方法
這里不再貼出代碼,await做的事就是監聽8005端口,如果用戶給8005端口發來"SHUTDOWN"就關閉serverSocket,然后await方法就履行完了。
現在再說說initialize方法,start方法,await方法,stop4者者都是在Bootstrap里調用的。
當await方法履行終了后,就會調用
服務器的stop方法。
Service接口
org.apache.catalina.Service 接口用于表示服務。1個服務可以有1個容器和多個連接器。你可以添加多個連接器 ,并將它們跟容器相干聯。
StandardService類
StandardService是Service接口的標準實現。
容器與連接器
1個 StandardService 實例包括兩種組件:1個容器和多個連接器。多個連接器可使得 Tomcat 能服務于多個協議。1個協議用途處理 HTTP 要求,另外一個用于處理 HTTPS 要求。
StandardService 類用 container 變量來持有容器實例,用 connectors 數組來持有所有的連接器
private Container container = null;
private Connector connectors[] = new Connector[0];
要將1個容器跟1個服務相干聯,可使用它的 setContainer 方法,
在setContainer中,會把container傳遞給每個connector。
synchronized (connectors) {
for (int i = 0; i < connectors.length; i++)
connectors[i].setContainer(this.container);
}
要給1個服務添加連接器,可使用 addConnector 方法。在添加連接器的同時也會初始化他們。
要刪除1個連接器,可使用 removeConnector 方法。
與生命周期相干的方法
這里主要包括3個方法,initialize,start,stop。
initialize功能就是調用所以連接器的initialize方法。
start功能就是啟動容器和若干個連接器。
stop功能就是關閉容器和若干個連接器。
服務組件的這3個方法都會在
服務器組件里相應的方法里被調用,而
服務器組件的這3個方法是在Bootstrap里調用的。
利用程序
這里主要是兩個類,1個是 Bootstrap 啟動程序,另外一個是 Stopper 類用來停止它。
Bootstrap類
public static void main(String args[]){
......
Service service = new StandardService();
service.setName("Stand-alone Service");
Server server = new StandardServer();
server.addService(service); //把服務組件添加到
服務器組件里
service.addConnector(connector); //把連接器關聯到服務組件里
//StandardService class's setContainer will call all its connector's setContainer method
service.setContainer(engine); //容器并沒有直接和連接器關聯,而是和服務組件關聯
// Start the new server
if (server instanceof Lifecycle) {
try {
server.initialize(); //調用initialize,start,await方法
((Lifecycle) server).start();//這里會調用服務組件的start,來調用連接器與容器的start
//連接器會在8080上等待消息....
server.await(); //除非在8005端口收到了SHUTDOWN信息 這里會1直循環
// the program waits until the await method returns,
// i.e. until a shutdown command is received.
}
catch (LifecycleException e) {
e.printStackTrace(System.out);
}
}
// Shut down the server
if (server instanceof Lifecycle) {
try {
((Lifecycle) server).stop(); //收到了SHUTDOWM信息,關閉
服務器組件
}
catch (LifecycleException e) {
e.printStackTrace(System.out);
}
}
}
Stopper類
public class Stopper {
public static void main(String[] args) {
// the following code is taken from the Stop method of
// the org.apache.catalina.startup.Catalina class
int port = 8005;
try {
Socket socket = new Socket("127.0.0.1", port);
OutputStream stream = socket.getOutputStream();
String shutdown = "SHUTDOWN";
for (int i = 0; i < shutdown.length(); i++)
stream.write(shutdown.charAt(i));
stream.flush();
stream.close();
socket.close();
System.out.println("The server was successfully shut down.");
}
catch (IOException e) {
System.out.println("Error. The server has not been started.");
}
}
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈