Tomcat lifecycle management-Lifecycle

Source: Internet
Author: User

We know that the architecture design of Tomcat is clear and modular, and it has many components. If we want to start Tomcat, we can start the components one by one, but it has many disadvantages, which is not only troublesome, in addition, it is easy to miss the component startup, and it will also cause trouble for subsequent dynamic component expansion. Is it hard for us to start them one by one? In fact, it is not necessary that Tomcat designers provide a solution: Use lifecycle to manage start, stop, and close.

From the structural diagram of section 1, we can see that each core component has a relationship between inclusion and inclusion, for example, server <-service <-iner and connector. The largest component is server, which is contained Layer 1. In fact, Tomcat organizes the entire system architecture as a container, reflecting that the data structure is a tree. The root node of the tree has no parent node, and other nodes have only one parent node, each parent node has zero or multiple sub-nodes, and the root container and other containers have the same characteristics. In view of this, you can use the parent container to start its sub-container, so that all other containers can be started as long as the root container is started, to achieve unified start, stop, and close effect.

As a unified interface, lifecycle organizes all startup, stop, close, and lifecycle related methods to easily manage the lifecycle of Tomcat container components. The following is a detailed definition of the lifecycle interface:

Public interface lifecycle {

Public static final string before_init_event = "before_init ";

Public static final string after_init_event = "after_init ";

Public static final string start_event = "start ";

Public static final string before_start_event = "before_start ";

Public static final string after_start_event = "after_start ";

Public static final string stop_event = "stop ";

Public static final string before_stop_event = "before_stop ";

Public static final string after_stop_event = "after_stop ";

Public static final string after_destroy_event = "after_destroy ";

Public static final string before_destroy_event = "before_destroy ";

Public static final string periodic_event = "periodic ";

Public static final string configure_start_event = "configure_start ";

Public static final string configure_stop_event = "configure_stop ";

Public void addlifecyclelistener (lifecyclelistener listener );

Public lifecyclelistener [] findlifecyclelisteners ();

Public void removelifecyclelistener (lifecyclelistener listener );

Public void Init () throws lifecycleexception;

Public void start () throws lifecycleexception;

Public void stop () throws lifecycleexception;

Public lifecyclestate getstate ();

Public String getstatename ();

}

From the above we can see that lifecycle actually defines some state constants and several methods. Here we mainly look at the init, start, and stop methods, all containers that need to be managed by the lifecycle must implement this interface and each container is called by the corresponding method of the parent container. For example, in the initialization phase, the root container server calls the init method, in the init method, the init method of its sub-container service is called, and so on.

In actual use, a unified call can be implemented only after the lifecycle interface is uniformly implemented. For example, the init method of the container is called as follows: (lifecycle) container). INIT (). Next, let's take a look at how the Tomcat source code server calls init.

 

PublicFinalSynchronizedVoidInit ()ThrowsLifecycleexception {

......

For(IntI = 0; I <services. length; I ++ ){

Services [I]. INIT ();

}

......

}

 

There are several service instances in the server container. You only need to use a for loop to call the init method for each service. Here, services [I] does not explicitly convert the type (lifecycle), because service is also an interface, and this interface has inherited the lifecycle interface.

Similarly, the steps for starting and stopping are similar calls to achieve unified startup and unified shutdown. So far, we have a clear understanding of the uniform initialization, startup, and shutdown mechanisms of Tomcat's lifecycle.

Tomcat lifecycle management-Lifecycle

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.