In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what the life cycle of Tomcat is like". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what the life cycle of Tomcat is like.
Preface
Tomcat life cycle management, we can not always get that knowledge from the book, but combined with practice, and then synthesize the content of the book, a layer of in-depth analysis, so that our memory and understanding can be more thorough.
At the time of startup
You can find a zip version of Tomcat and start it directly. Let's see what it looks like.
January 11, 2021 10:16:24 morning org.apache.coyote.AbstractProtocol init information: Initializing ProtocolHandler ["http-bio-8080"] January 11, 2021 10:16:24 morning org.apache.coyote.AbstractProtocol init information: Initializing ProtocolHandler ["ajp-bio-8009"] January 11, 2021 10:16:24 morning org.apache.catalina.startup.Catalina load information: Initialization processed in 470 ms January 11, 2021 10:16:24 morning org.apache.catalina.core.StandardService startInternal information: Starting service Catalina January 11 2021 10:16:24 AM org.apache.catalina.core.StandardEngine startInternal Information: Starting Servlet Engine: Apache Tomcat/7.0.88 January 11, 2021 10:16:24 AM org.apache.catalina.startup.HostConfig deployDirectory
After you see this startup process, after thinking about the startup process of Tomcat in the previous article, do you feel a little bit like that again, load, then start, and finally stop.
Highlight 1:Lifecycle
In the previous article, we mentioned Lifecycle, and Tomcat uniformly manages the lifecycle through the Lifecycle interface, and all lifecycle components implement the Lifecycle interface in order to provide a consistent mechanism to start and stop components.
Well, let's analyze what is contained in this Lifecycle interface. You can go directly to the jar of catalina in the tomcat package to find it.
13 variables of type String are defined
Three methods for managing listeners are defined
Four life cycles are defined
Two methods to get the current state are defined.
So let's start with these 13 variables:
String BEFORE_INIT_EVENT = "before_init"; String AFTER_INIT_EVENT = "after_init"; String START_EVENT = "start"; String BEFORE_START_EVENT = "before_start"; String AFTER_START_EVENT = "after_start"; String STOP_EVENT = "stop"; String BEFORE_STOP_EVENT = "before_stop"; String AFTER_STOP_EVENT = "after_stop"; String AFTER_DESTROY_EVENT = "after_destroy" String BEFORE_DESTROY_EVENT = "before_destroy"; String PERIODIC_EVENT = "periodic"; String CONFIGURE_START_EVENT = "configure_start"; String CONFIGURE_STOP_EVENT = "configure_stop"
What do these 13 variables mean? As mentioned in the book Tomcat Schema parsing, this constant information is used in the type property of the LifecycleEvent event to distinguish the state of the LifecycleEvent event emitted by the component (such as pre-initialization, pre-startup, startup medium). This design method allows multiple states to send the same type of time, and then use one of the attribute classes to distinguish between states without defining multiple events.
In fact, you can see by the name of the variable, before initialization, after initialization, before startup, after startup. To put it bluntly, it is just to represent the state of the component when it is emitted.
And what are the three ways to manage surveillance?
Void addLifecycleListener (LifecycleListener var1); LifecycleListener [] findLifecycleListeners (); void removeLifecycleListener (LifecycleListener var1)
These three listeners are also used to add, find and delete listeners of LifecycleListener type. Here are three interfaces. Let's go to the subclass for the specific implementation. Just know that there is such a thing here, and we will analyze it directly below.
4 life cycle
This must be very simple, such as init,start,stop,destroy, initialize, start, stop, destroy, as we all know in the life cycle of Servlet.
Void init () throws LifecycleException; void start () throws LifecycleException; void stop () throws LifecycleException; void destroy () throws LifecycleException
2 ways to get status
LifecycleState getState (); String getStateName ()
After all, this Lifecycle is an interface, it is not a specific implementation class, we want to understand this, then we must go to the specific implementation class to find this content, then what is his implementation class? Here it comes, here it comes, LifecycleBase.
LifecycleBase
LifecycleBase is an abstract class and the base class of all component classes in tomcat. It implements Lifecycle, but many subclasses under Tomcat also inherit it, so it is also very important.
Public abstract class LifecycleBase implements Lifecycle {private LifecycleSupport lifecycle = new LifecycleSupport (this); / / the current state of the source component. Different states trigger different events private volatile LifecycleState state; public LifecycleBase () {this.state = LifecycleState.NEW;}}
Here we also pay attention to this LifecycleSupport class. LifecycleSupport defines an attribute of the LifecycleListener array type to save all listeners, and then defines methods to add, delete, find, and execute listeners respectively. If you don't believe it, let's take a look. After all, it makes sense for this class to be put here to new first.
Public final class LifecycleSupport {private Lifecycle lifecycle = null; private LifecycleListener [] listeners = new LifecycleListener [0]; private final Object listenersLock = new Object (); public LifecycleSupport (Lifecycle lifecycle) {this.lifecycle = lifecycle;} public void addLifecycleListener (LifecycleListener listener) {Object var2 = this.listenersLock; synchronized (this.listenersLock) {LifecycleListener [] results = new LifecycleListener [this.listeners.length + 1] For (int I = 0; I
< this.listeners.length; ++i) { results[i] = this.listeners[i]; } results[this.listeners.length] = listener; this.listeners = results; } } public LifecycleListener[] findLifecycleListeners() { return this.listeners; } public void fireLifecycleEvent(String type, Object data) { LifecycleEvent event = new LifecycleEvent(this.lifecycle, type, data); LifecycleListener[] interested = this.listeners; for(int i = 0; i < interested.length; ++i) { interested[i].lifecycleEvent(event); } } public void removeLifecycleListener(LifecycleListener listener) { Object var2 = this.listenersLock; synchronized(this.listenersLock) { int n = -1; for(int i = 0; i < this.listeners.length; ++i) { if (this.listeners[i] == listener) { n = i; break; } } if (n >= 0) {LifecycleListener [] results = new LifecycleListener [this.listeners.length-1]; int j = 0; for (int I = 0; I < this.listeners.length; + + I) {if (I! = n) {results [jacks +] = this.listeners [I] }} this.listeners = results;}
Needless to say, let's move on, what is its life cycle approach? That's the point of the day. We talked about what the lifecycle contains before, starting with init, then start, then stop, and finally the destroy method, which is incisively and vividly represented in the implementation class.
Init method
Public final synchronized void init () throws LifecycleException {/ / here means that it can only be used in the NEW state, if (! this.state.equals (LifecycleState.NEW)) {this.invalidTransition ("before_init") } / / through different states here, and then to trigger different events, try {/ / sets the life cycle state to INITIALIZING this.setStateInternal (LifecycleState.INITIALIZING, (Object) null, false); / / executes the method this.initInternal () / / set lifecycle state to INITIALIZED this.setStateInternal (LifecycleState.INITIALIZED, (Object) null, false);} catch (Throwable var2) {ExceptionUtils.handleThrowable (var2); this.setStateInternal (LifecycleState.FAILED, (Object) null, false); throw new LifecycleException (sm.getString ("lifecycleBase.initFail", new Object [] {this.toString ()}), var2);}}
Start method
Public final synchronized void start () throws LifecycleException {/ / validates the lifecycle state here, which is the unavailable state STARTING_PREP,STARTING,STARTED if (! LifecycleState.STARTING_PREP.equals (this.state) & &! LifecycleState.STARTING.equals (this.state) & &! LifecycleState.STARTED.equals (this.state)) {/ / if it is NEW state Execute the init method if (this.state.equals (LifecycleState.NEW)) {this.init () / / if the state is FAILED, execute the stop method} else if (this.state.equals (LifecycleState.FAILED)) {this.stop () / / if it is INITIALIZED status, it will tell you that it is an illegal operation} else if (! this.state.equals (LifecycleState.INITIALIZED) & &! this.state.equals (LifecycleState.STOPPED)) {this.invalidTransition ("before_start") } try {/ / sets the startup status to STARTING_PREP this.setStateInternal (LifecycleState.STARTING_PREP, (Object) null, false); this.startInternal () / / it is very rigorous here. After startup, he will continue to see what the status is to ensure that if (this.state.equals (LifecycleState.FAILED)) {this.stop ();} else if (! this.state.equals (LifecycleState.STARTING)) {this.invalidTransition ("after_start") is launched successfully. } else {this.setStateInternal (LifecycleState.STARTED, (Object) null, false);}} catch (Throwable var2) {ExceptionUtils.handleThrowable (var2); this.setStateInternal (LifecycleState.FAILED, (Object) null, false) Throw new LifecycleException (sm.getString ("lifecycleBase.startFail", new Object [] {this.toString ()}), var2);}} else {if (log.isDebugEnabled ()) {Exception e = new LifecycleException (); log.debug (sm.getString ("lifecycleBase.alreadyStarted", new Object [] {this.toString ()}), e) } else if (log.isInfoEnabled ()) {log.info (sm.getString ("lifecycleBase.alreadyStarted", new Object [] {this.toString ()}));}
Stop method
Public final synchronized void stop () throws LifecycleException {/ / same as above, if (! LifecycleState.STOPPING_PREP.equals (this.state) & &! LifecycleState.STOPPING.equals (this.state) & &! LifecycleState.STOPPED.equals (this.state)) {/ / if it is NEW status The status is directly modified to STOPPED if (this.state.equals (LifecycleState.NEW)) {this.state = LifecycleState.STOPPED } else {/ / if it is not in these 2 states, then directly exception if (! this.state.equals (LifecycleState.STARTED) & &! this.state.equals (LifecycleState.FAILED)) {this.invalidTransition ("before_stop") } try {if (this.state.equals (LifecycleState.FAILED)) {this.fireLifecycleEvent ("before_stop", (Object) null);} else {this.setStateInternal (LifecycleState.STOPPING_PREP, (Object) null, false) } this.stopInternal (); if (! this.state.equals (LifecycleState.STOPPING) & &! this.state.equals (LifecycleState.FAILED)) {this.invalidTransition ("after_stop");} this.setStateInternal (LifecycleState.STOPPED, (Object) null, false) } catch (Throwable var5) {...} finally {...} else {...}}
Destroy method
Public final synchronized void destroy () throws LifecycleException {/ / if the state fails to start, that is, FAILED, then the stop method is called directly, if (LifecycleState.FAILED.equals (this.state)) {try {this.stop () } catch (LifecycleException var3) {log.warn (sm.getString ("lifecycleBase.destroyStopFail", new Object [] {this.toString ()}), var3) }} / / if the two states are DESTROYING and DESTROYED, then the execution will no longer be carried out. Directly perform return if (! LifecycleState.DESTROYING.equals (this.state) & &! LifecycleState.DESTROYED.equals (this.state)) {if (! this.state.equals (LifecycleState.STOPPED) & &! this.state.equals (LifecycleState.FAILED) & &! this.state.equals (LifecycleState.NEW) & &! this.state.equals (LifecycleState.INITIALIZED)) {this.invalidTransition ("before_destroy") } try {this.setStateInternal (LifecycleState.DESTROYING, (Object) null, false); this.destroyInternal (); this.setStateInternal (LifecycleState.DESTROYED, (Object) null, false);} catch (Throwable var2) {ExceptionUtils.handleThrowable (var2) This.setStateInternal (LifecycleState.FAILED, (Object) null, false); throw new LifecycleException (sm.getString ("lifecycleBase.destroyFail", new Object [] {this.toString ()}), var2) }} else {.} Thank you for your reading. The above is the content of "what is the life cycle of Tomcat?" after the study of this article, I believe you have a deeper understanding of what the life cycle of Tomcat looks like, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.