Tomcat architecture and lifecycle and event listening

Talk about the architecture, life cycle and event monitoring mechanism of tomcat, because there are many things, which w...
Tomcat structure
Tomcat lifecycle
LifecycleState
Life cycle flow
Tomcat event listening

Talk about the architecture, life cycle and event monitoring mechanism of tomcat, because there are many things, which will be described in the later Tomcat source code;

Tomcat structure


Tomcat lifecycle

tomcat architecture is a tree hierarchical management structure. Components can have their own parent nodes or their own child nodes. Each node is a component and each component has a life cycle. For management convenience, the life cycle of child nodes is managed by the parent node.

The lifecycle management of each component is mainly represented by the interface org.apache.catalina.Lifecycle and an enumeration org.apache.catalina.LifecycleState.

Lifecycle
org.apache.catalina.Lifecycle interface defines all the actions executed by the component. The core includes three:

1.init(),Component initialization 2.start(),Start component 3.stop(),Stop component 4.destroy(),Component destruction 5.getState(),Get current status of component

Life cycle nodes include:

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";

LifecycleState

public enum LifecycleState { NEW(false, null), INITIALIZING(false, Lifecycle.BEFORE_INIT_EVENT), INITIALIZED(false, Lifecycle.AFTER_INIT_EVENT), STARTING_PREP(false, Lifecycle.BEFORE_START_EVENT), STARTING(true, Lifecycle.START_EVENT), STARTED(true, Lifecycle.AFTER_START_EVENT), STOPPING_PREP(true, Lifecycle.BEFORE_STOP_EVENT), STOPPING(false, Lifecycle.STOP_EVENT), STOPPED(false, Lifecycle.AFTER_STOP_EVENT), DESTROYING(false, Lifecycle.BEFORE_DESTROY_EVENT), DESTROYED(false, Lifecycle.AFTER_DESTROY_EVENT), FAILED(false, null) }

Enumeration values represent the state
The first parameter indicates whether the component is available in the current state;
The second parameter indicates that the corresponding event is sent when it changes to the current state;

Life cycle flow


1. All States can be changed to failed;
2. A component is starting_ Calling the start() method in the prep, starting and started States will not have an impact;
3. When a component calls the start method in the new state, it will call the init() method first;
4. A component is stopping_ Calling the stop method in the status of prep, stopping and stopped will not have an impact;
5. When a component calls the stop() method in the new state, it will directly change the state to stopped. When the component fails to start itself, it is necessary to stop the sub components, even though some sub components have not been started;
6. Exceptions will be thrown when other states change each other;
7. When a legal state transition occurs, the corresponding lifecycleEvent event will start, and an illegal transition will not trigger the event.

Tomcat event listening

Event trigger
The state of each component of tomcat will change, and some events will be thrown when it changes. tomcat supports defining event listeners to listen and consume these events.
Event execution
The class of event listening function is org.apache.catalina.util.LifecycleBase. Each component will integrate this class. This class has an attribute: List lifecycleListeners; This attribute is used to save event listeners, that is, each component has a list of event listeners.
Methods of this class:

protected void fireLifecycleEvent(String type, Object data) { LifecycleEvent event = new LifecycleEvent(this, type, data); for (LifecycleListener listener : lifecycleListeners) { listener.lifecycleEvent(event); } }

When the component state changes, fireLifecycleEvent will be called to trigger event execution. For example, when the subclass StandardServer of server starts, fireLifecycleEvent will be called

@Override protected void startInternal() throws LifecycleException { fireLifecycleEvent(CONFIGURE_START_EVENT, null); setState(LifecycleState.STARTING); globalNamingResources.start(); // Start our defined Services synchronized (servicesLock) { for (Service service : services) { service.start(); } } }

Event listener
Programmers can customize event listeners by implementing the lifecycleListener interface, such as:

class NamingListener implements lifecycleListener() class FrameListener implements lifecyclelistener()

Summary: the event listener here is not triggered asynchronously, but actively invokes the event listener, which is different from the event trigger of nacos.

4 September 2021, 16:12 | Views: 5300

Add new comment

For adding a comment, please log in
or create account

0 comments