标签:读书笔记 tomcat 服务器组件 服务组件 bootstrap
之前的项目还是有些问题的,例如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(); } }
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。
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 ... }关闭所有的服务组件。
private Container container = null; private Connector connectors[] = new Connector[0];要将一个容器跟一个服务相关联,可以使用它的 setContainer 方法,
synchronized (connectors) { for (int i = 0; i < connectors.length; i++) connectors[i].setContainer(this.container); }要给一个服务添加连接器,可以使用 addConnector 方法。在添加连接器的同时也会初始化他们。
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信息 这里会一直循环 // 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); } } }
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."); } } }
How tomcat works 读书笔记十四 服务器组件和服务组件
标签:读书笔记 tomcat 服务器组件 服务组件 bootstrap
原文地址:http://blog.csdn.net/dlf123321/article/details/41699339