Overview of "TOMCAT9 Source analysis" Lifecycle, start, stop

Source: Internet
Author: User
Tags event listener reflection socket throwable tomcat

Reprint Please specify source: http://blog.csdn.net/linxdcn/article/details/73350812 1 Preface

When Tomcat starts, the Server.xml file is read to create the server, Service, Connector, Engine, Host, Context, wrapper and other components, the introduction of each component can refer to the Tomcat components and framework

After each component is created, Tomcat is responsible for managing their lifecycle, and this article first describes the component lifecycle management in Tomcat and then describes the start and stop of Tomcat. 2 life cycle Management 2.1 Lifecycle

All components in Tomcat inherit the lifecycle interface, and the lifecycle interface defines a set of functions for lifecycle management, from the new, initialized, start, stop, fail, and destroy of components to the same rules, and the state transition diagram for lifecycle components is as follows.



Lifecycle state transition diagram

The main methods of the lifecycle interface are as follows, the normal sequence of calls is init ()->start ()->destroy (), the parent component's init () and start () will trigger the child component init () and start (), So only the Init () and start () of the server component can be called in Tomcat.

Public interface Lifecycle {public

    void init () throws lifecycleexception;

    public void Start () throws lifecycleexception;

    public void Stop () throws lifecycleexception;

    public void Destroy () throws lifecycleexception;

    Public lifecyclestate getState ();

    public void Addlifecyclelistener (Lifecyclelistener listener);

    Public lifecyclelistener[] Findlifecyclelisteners ();

    public void Removelifecyclelistener (Lifecyclelistener listener);

Each implementation component inherits from Lifecyclebase,lifecyclebase implements the lifecycle interface, and when the container state changes, the Firelifecycleevent method is called, generating lifecycleevent, And it is handled by the event listener of this container.

Public abstract class Lifecyclebase implements Lifecycle {

    protected void firelifecycleevent (String type, Object data {
        Lifecycleevent event = new Lifecycleevent (this, type, data);
        for (Lifecyclelistener listener:lifecyclelisteners) {
            listener.lifecycleevent (event);}}}
2.2 Lifecyclelistener and Lifecycleevent

In the lifecycle interface, you also see Lifecyclelistener, which is used to listen for events that occur lifecycle, and the Lifecyclelistener interface defines only one callback function.

Public interface Lifecyclelistener {

    /** * Acknowledge the occurrence of the
     specified event.
     *
     * @param event lifecycleevent that have occurred
     *
    /public void Lifecycleevent (Lifecycleevent event); c14/>}
3 Tomcat Launcher

Here's a look at what happened after executing the tomcat/bin/startup.sh script. Executing the script is equivalent to executing the main method of the Org.apache.catalina.startup.Bootstra class and passing in the start parameter.

package org.apache.catalina.startup; Public final class Bootstrap {public static void main (String args[]) {if (daemon = = null) {Bo
            Otstrap bootstrap = new Bootstrap ();
            try {//1 calls the Init method of the Bootstrap class Bootstrap.init ();
        } catch (Throwable t) {//Omit code} daemon = bootstrap;

            } else {//omit code} try {String command = "Start";

                Omit code if (Command.equals ("start")) {daemon.setawait (true);

                2 Call the Bootstrap class's Load Method Daemon.load (args);
            3 Call the Bootstrap class's Load Method Daemon.start (); }} catch (Throwable t) {//Omit code}}} 

The main steps are as follows: Create a new Bootstrap object daemon and call its init () method
Initializes the Tomcat class loader to instantiate the Org.apache.catalina.startup.Catalina object with reflection Catalinadaemon calls the daemon load method, Essentially called the load method of the Catalinadaemon
Loading and parsing the Server.xml configuration file calls Daemon's Start method, essentially invoking the Catalinadaemon start method
Starting the server component, the startup of the server drives other components to start, such as service, Container, Connector call Catalinadaemon's await method loop waiting for the shutdown command to receive Tomcat 4 Tomcat Stop

The boot section finally describes the Catalinadaemon call await Stop command, which we typically do by executing tomcat/bin/ Shutdown.sh to close Tomcat is equivalent to executing the main method of the Org.apache.catalina.startup.Bootstra class and passing in the stop parameter.

Package org.apache.catalina.startup;

Public final class Bootstrap {public

    static void Main (String args[]) {

        if (daemon = = null) {
            Bootstrap bootst Rap = new Bootstrap ();
            try {
                //1 calls the Init method of the Bootstrap class
                bootstrap.init ();
            } catch (Throwable t) {
                //omit code
            }
            daemon = bootstrap;
        } else {
            //omit code
        }

        try {
            String command = "Start";

            Omit Code

            if (command.equals ("Stop")) {
                //2 calls the Bootsrap class's Stopserver method
                daemon.stopserver (args);} 
        } catch (Throwable t) {
            //omit code
        }
    }
}
Create a new Bootstrap object daemon and call its init () method
Initializes the Tomcat class loader to instantiate the Org.apache.catalina.startup.Catalina object with reflection Catalinadaemon call Daemon's Stopserver method, Essentially called the Stopserver method of the Catalinadaemon
Parse the Server.xml file, construct the server container to get the socket listener port and address of the server, create a socket object to connect the ServerSocket created when you start Tomcat, The last shutdown command that is sent to ServerSocket runs in the server call stop method stops
5 Summary



Tomcat Startup and shutdown process

6 References

Http://tomcat.apache.org/tomcat-9.0-doc/architecture/startup/serverStartup.txt
Http://tomcat.apache.org/tomcat-9.0-doc/architecture/startup/serverStartup.pdf
http://blog.csdn.net/beliefer/article/details/51585006

Reprint Please specify source: http://blog.csdn.net/linxdcn/article/details/73350812

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.