Life cycle processor

AbstractApplicationContext - > refresh () : / / the last step, release corresponding event finishRefresh (); ServletWebServerApplicationContext->finishRefresh(): super.finishRefresh(); WebServer = startWebServer(); if (webServer ! = null) {/ / ServletWebServerInitializedEvent event publishEvent (new ServletWebServerInitializedEvent (webServer, this)); }Copy the code

Super. FinishRefresh () call is AbstractApplicationContext finishRefresh () method.

Initialization initLifecycleProcessor () : the context of the life cycle of the processor, if none of the IoC container, the default for DefaultLifecycleProcessor object

GetLifecycleProcessor (.) onRefresh () : Find the bean in the IoC container that implements Lifecycle and execute the start() method if it is a SmartLifecycle interface implementation class and isAutoStartup() method returns true and isRunning() method returns false.

PublishEvent (new ContextRefreshedEvent(this)) : ContextRefreshedEvent event that starts a scheduled task

Listening to the

Unbind and re-bind service and Connector

Previously: Service and Connector unbinding during Tomcat startup

To unbind, remove the connector from a service and place the relationship between the two in a TomcatWebServer serviceConnectors variable for later use

TomcatWebServer - > start () : / / added to remove the connectors before addPreviouslyRemovedConnectors (); / / - 1... / / check all connector normal boot checkThatConnectorsHaveStarted (); TomcatWebServer->addPreviouslyRemovedConnectors(): Service[] services = this.tomcat.getServer().findServices(); for (Service service : services) { Connector[] connectors = this.serviceConnectors.get(service); if (connectors ! = null) { for (Connector connector : connectors) { service.addConnector(connector); } this.serviceConnectors.remove(service); }} StandardService->addConnector(): // Service and connector are bound...... If (getState().isavailable ()) {connector.start(); }Copy the code

Start connector, http11NioProtocol, nioEndpoint

In connector’s startInternal(), Http11NioProtocol is started, which in turn starts NioEndpoint

AbstractEndpoint->start(): bindWithCleanup(); // --1 startInternal(); // --2 NioEndpoint->bind(): // --1 initServerSocket(); . Selectorpool.open (); // Initialize nioselectorPool.open (); // Initialize nioselectorPool.open (); NioEndpoint->initServerSocket(): serverSock = serversocketChannel.open (); socketProperties.setProperties(serverSock.socket()); InetSocketAddress addr = new InetSocketAddress(getAddress(), getPortWithOffset()); Serversock.socket ().bind(addr,getAcceptCount())); / / request at this time is for the NioEndpoint - > startInternal () : / / - 2... If (getExecutor() == null) {// Create task execution thread pool createExecutor(); pollers = new Poller[getPollerThreadCount()]; For (int I =0; i<pollers.length; i++) { pollers[i] = new Poller(); Thread pollerThread = new Thread(pollers[i], getName() + "-ClientPoller-"+i); pollerThread.setPriority(threadPriority); pollerThread.setDaemon(true); pollerThread.start(); } // Acceptor is used to listen for clients to connect to startAcceptorThreads(); // At this point, the response is complete}Copy the code

Acceptors, pollers, and executors are introduced in the NioEndpoint of the Http11NioProtocol model, in the order from front to back, and in the reverse order of creation.

conclusion

A listener is the startup of connector. SpringIoC instantiates beans related to Web services first, and then instantiates ordinary beans. When Tomcat starts, ordinary beans have not been instantiated, so listening to receive requests cannot be enabled at this time. Therefore, they are unbound first, and then re-bound to enable listening to receive requests and responses after the instantiation of ordinary beans is complete.

The destruction

The entry method for web server destruction is located at TomcatWebServer->stop():stopTomcat()

Destruction is divided into two steps, the first step is to stop, the second step is to destroy.

As with startup, components are performed in sequence. As shown in the figure below, each component has an independent life cycle. Here component 2 is the last component of component 1, component 3 is the last component of component 2, and so on.

The order of each component is and the general function is:

  1. NioEndpoint (turn off listening)
  2. Wrapper, context, host, engine
    • Child components of engine, host, and Conext stop asynchronously, and wrapper synchronization stops
    • Interrupt background thread
  3. NioEndpoint, http11NioProtocol, connector
  4. The service, the server