Tomcat source analysis-specific start-up process analysis

Source: Internet
Author: User

Starting the call stack from Tomcat, it is known that the main method of the Bootstrap class is the entire Tomcat portal, where init initializes the bootstrap class to set Catalina's working path, which is catalina_home information, Catalina.base information, initializes the class loader in the Initclassloaders method, and then initializes the Org.apache.catalina.startup.Catalina as the Catalina daemon through reflection;

First, load

650) this.width=650; "src=" Http://image.mamicode.com/info/201412/20180926205526545995.png "style=" margin:0px; padding:0px;border:0px;width:709px; "/>
bootstrap:

    1. Reflection calls the Catalina Load method.

    2. Calls Catalina Initdirs (), initnaming () to initialize Catalinahome, catalina.usenaming, and so on.

    3. Creates a Digester object, invokes Createstartdigester to create and configure digester information, Load the Server.xml configuration file, use Digester to parse the Server.xml file, and create server, Service, Engine, Host, Valve, Realm, Connector, based on the configuration information of the server file. Listener, resource and other objects.

    4. Sets the Catalina object that is currently Catalina to Standardserver, calling Initstreams.

    5. Invokes the Init method of Standardserver to initialize the server.

Call Bootstrap's Load method to call Catalina's load with reflection, initialize Initdirs () path information in Catalina, invoke Createstartdigester to create and configure digester information, Load the Server.xml configuration file, use Digester to parse the Server.xml file, and create server, Service, Engine, Host, Valve, Realm, Connector, based on the configuration information of the server file. Listener, resource and other objects, and the Association of Objects (Digester the relevant knowledge is not described here). Since the server object was created when the Digester parsing server.xml, the server's Init method is also initialized to the server object, so that the bootstrap execution is complete, and the Catalina load is exactly loaded.

Second, start
The Start method of the bootstrap is then invoked to launch the container, and in the Start method, the Start method of the Catalina is called by reflection, such as:

650) this.width=650; "src=" Http://image.mamicode.com/info/201412/20180926205526860460.png "style=" margin:0px; padding:0px;border:0px;width:709px; "/>

Then start the server by invoking the Start method of the Standardserver object in the Catalina, and in the Start method, as we analyzed in the section of the Tomcat source analysis-component initiation implementation analysis, The launch of service, Connector, Engine, Host, Context, Wrapper, and pipeline through Tomcat's life cycle management observer pattern is not repeated here;

Iii. Registration Closure Hooks (shutdownhook)

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

1 if (Shutdownhook = = null) {2 Shutdownhook = new Catalinashutdownhook (); 3}4 runtime.getruntime (). Addshutdownhook (Shu Tdownhook);

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

call await () and stop (), waiting to close the request with the Stop container
650) this.width=650; "src=" Http://image.mamicode.com/info/201412/20180926205527323369.png "style=" margin:0px; padding:0px;border:0px;width:709px; "/>
Calling Catalina's await actually calls Standardserver's await, waiting for <server port= "8006" shutdown= "Shutdown" > in the Server Shutdown on port 8006 closes the tomcat request.
The Stop method that calls Standardserver after receiving a shutdown request is associated with the Destroy method such as:
650) this.width=650; "src=" Http://image.mamicode.com/info/201412/20180926205527721821.png "style=" margin:0px; padding:0px;border:0px;width:709px; "/>
Lifecycle management Observer pattern for closing and freeing resources for service, Connector, Engine, Host, Context, Wrapper, Pipeline

V. Summary
Tomcat start and close process to this end, the whole process is still relatively clear, as long as the removal of the Tomcat startup process also naturally understood the Tomcat shutdown process, tomcat using the Observer pattern for the life cycle of Tomcat management, Understanding Lifecyclebase, Lifecycle, Lifecyclesupport and other related classes also understand the entire process, the timing diagram of the whole process is as follows:

650) this.width=650; "src=" Http://image.mamicode.com/info/201412/20180926205528151525.png "style=" margin:0px; padding:0px;border:0px;width:709px; "/>

Tomcat start and close timing diagram

Article starting address: Solinx

http://www.solinx.co/archives/121


650) this.width=650, "src=" http://image.mamicode.com/info/201412/20180926205528492359.gif "width="/>

Tomcat source analysis-specific start-up process analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.