Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Container structure and Lifecycle in Tomcat9

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail about Tomcat9 container structure and life cycle example analysis, Xiaobian think it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

Life cycle of container Structure of container

The above figure shows the class diagram structure of Tomcat container. All containers inherit Lifecycle MBeanBase, while the base class Lifecycle MBeanBase implements two interfaces, Lifecycle and MBeanRegistration. Lifecycle describes the main behavior and life cycle changes of the container, while MBeanRegistration monitors Tomcat through JMX.

org.apache.catalina.Lifecycle

The main methods in Lifecycle are init, start, stop and destruction. Lifecycle contains 12 states, and each operation method corresponds to the change of state, which constitutes the complete life cycle of Tomcat container from initial creation to final destruction. The interface annotation of Lifecycle makes a good explanation for this.

/* start() * ----------------------------- * | | * | init() | * NEW -»-- INITIALIZING | * | | | | ------------------«----------------------- * | | |auto | | | * | | \|/ start() \|/ \|/ auto auto stop() | * | | INITIALIZED --»-- STARTING_PREP --»- STARTING --»- STARTED --»--- | * | | | | | * | |destroy()| | | * | --»-----«-- ------------------------«-------------------------------- ^ * | | | | * | | \|/ auto auto start() | * | | STOPPING_PREP ----»---- STOPPING ------»----- STOPPED -----»----- * | \|/ ^ | ^ * | | stop() | | | * | | -------------------------- | | * | | | | | * | | | destroy() destroy() | | * | | FAILED ----»------ DESTROYING ---«----------------- | * | | ^ | | * | | destroy() | |auto | * | --------»----------------- \|/ | * | DESTROYED | * | | * | stop() | * ----»-----------------------------»------------------------------ */

The following figure is the Tomcat internal container class structure diagram in the previous article. It can be seen that all types of containers implement the Lifecycle interface. Various types of containers are aggregated and combined according to the organization. int(), start() and other methods are called according to the hierarchy.

observer pattern

When the state of the container changes, Tomcat uses observer mode to trigger listening registered on the container. The LifecycleBase.init() method is described below as an example.

org.apache.catalina.util.LifecycleBase

@Override public final synchronized void init() throws LifecycleException { if (! state.equals(LifecycleState.NEW)) { invalidTransition(Lifecycle.BEFORE_INIT_EVENT); } try { setStateInternal(LifecycleState.INITIALIZING, null, false); initInternal(); setStateInternal(LifecycleState.INITIALIZED, null, false); } catch (Throwable t) { handleSubClassException(t, "lifecycleBase.initFail", toString()); } }//private synchronized void setStateInternal(LifecycleState state, Object data, boolean check) throws LifecycleException { ... Omit other codes... this.state = state; String lifecycleEvent = state.getLifecycleEvent(); if (lifecycleEvent != null) { fireLifecycleEvent(lifecycleEvent, data); } } //invoke protected void fireLifecycleEvent(String type, Object data) { LifecycleEvent event = new LifecycleEvent(this, type, data); for (LifecycleListener listener : lifecycleListeners) { listener.lifecycleEvent(event); } }

The init() method triggers a snoop corresponding to the LifecycleState.INITIALIZING state before the actual method executes, and triggers a snoop corresponding to the LifecycleState.INITIALIZED state after the actual method executes

Listeners are registered when the container is initialized, for example, under the server tag in server.xml

Tomcat Operation Timing Diagram

About "Tomcat9 container structure and life cycle sample analysis" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report