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 is the start-up process of Tomcat". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what the startup process of Tomcat is like.
1.Tomcat analysis
Ah Fan knows that as a senior Java developer, he is all too familiar with Tomcat. Bin directory, conf directory and webapps directory are simply not familiar with these directories. A word of disagreement is a shutdown.sh, or a shutdown.bat, but do you know what your startup process is for startup.bat and startup.sh? Next, let's get into the analysis.
The overall structure diagram of 2.Tomcat
This overall structure map is not what we think of the directory structure diagram, the directory structure map A Fan will not show you, open your Tomcat yourself, there is you want to see the directory structure map, then what does the overall structure diagram look like?
Explain the meaning of this picture to everyone.
Server: the entire server.
Service: specific services.
Connector: provides a connection between Socket and request,response.
Container: used to encapsulate and manage Servlet, as well as specific processing of requests.
In this picture, the inclusion relationship is clearly stated, why do you say so? Because there can be multiple services in a Server, that is, multiple Service, and there can be more than one Connector in a Service, but only one Container, is it very clear?
The startup process of 3.Tomcat
Next let's take a look at the startup process in the source code, the startup process in the Bootstrap class.
The location of this class is in the catalina package of tomcat. Take a look at the main method, that is, the so-called main method.
Public static void main (String [] args) {/ / object initialization if (daemon = = null) {Bootstrap bootstrap = new Bootstrap (); try {bootstrap.init ();} catch (Throwable var3) {handleThrowable (var3); var3.printStackTrace (); return } daemon = bootstrap;} else {Thread.currentThread () .setContextClassLoader (daemon.catalinaLoader);} try {String command = "start"; if (args.length > 0) {command = args [args.length-1] } if (command.equals ("startd")) {args [args.length-1] = "start"; / / load daemon.load (args); / / start daemon.start () } else if (command.equals ("stopd")) {args [args.length-1] = "stop"; / / stop daemon.stop ();} else if (command.equals ("start")) {daemon.setAwait (true); / / load and start daemon.load (args) Daemon.start (); if (null = = daemon.getServer ()) {System.exit (1);}} else if (command.equals ("stop")) {daemon.stopServer (args);} else if (command.equals ("configtest")) {daemon.load (args) If (null = = daemon.getServer ()) {System.exit (1);} System.exit (0);} else {log.warn ("Bootstrap: command\"+ command +"\ "does not exist.");} catch (Throwable var4) {Throwable t = var4 If (var4 instanceof InvocationTargetException & & var4.getCause ()! = null) {t = var4.getCause ();} handleThrowable (t); t.printStackTrace (); System.exit (1);}}
The existence of the main method is also very simple, first performing the operation of init, and then performing start, that is to say, during the startup process, initialization is performed first, followed by startup, and the final stage is stop. This is considered complete.
Load method: to put it bluntly, the load method is to create a Server based on the server.xml file and call the init method of Server for initialization.
Start method: the start method is straightforward, start the server.
The stop method: the stop method, again, stops the server.
Here, the start method and the stop method call the start and stop methods inside the Server, and these three methods are all based on the hierarchical structure in the diagram, first from the load,start,stop of the Server, and then the start of the Server calls the start of the Service, and the start of the Service calls the start methods of Connector and Container, which starts from the outside to the inside, and the Tomcat can be started completely.
Next, we will continue to analyze the wave from the outside to the inside.
3.1 Catalina startup process
We have successfully found the above startup entry, so let's move on. After the object initialization is completed, we execute the init method.
Bootstrap bootstrap = new Bootstrap (); try {bootstrap.init ();} catch (Throwable var3) {
This is the one above. If the argument is empty, then you start calling start. What is the start method?
Public void start () throws Exception {if (this.catalinaDaemon = = null) {this.init ();} Method method = this.catalinaDaemon.getClass (). GetMethod ("start", (Class []) null); method.invoke (this.catalinaDaemon, (Object []) null);}
The above start method is directly mapped to catalinaDaemon using the invoke method, that is, to the start method of Catalina.
And the startup of this Catalina is nothing more than calling the same method, setAwait method, load method, start method.
SetAwait method: used to set whether to enter wait when Server startup is complete, if it is true, then wait, if not false, then do not enter wait.
Load method: create and initialize Server
Start method: start the server as well
The same setAwait method is relatively few, so A Fan will not show it to everyone. It is nothing more than a judgment, while the load method must be seen.
If (! this.loaded) {this.loaded = true; long T1 = System.nanoTime (); try {inputSource.setByteStream ((InputStream) inputStream); digester.push (this); digester.parse (inputSource); break label242 } catch (SAXParseException var28) {log.warn ("Catalina.start using" + this.getConfigFile () + ":" + var28.getMessage ()); return;} catch (Exception var29) {log.warn ("Catalina.start using" + this.getConfigFile () + ":", var29) }} finally {if (inputStream! = null) {try {(InputStream) inputStream). Close ();} catch (IOException var23) { } return;} try {/ / the init method of Server also called here, this.getServer () .init () } catch (LifecycleException var24) {if (Boolean.getBoolean ("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) {throw new Error (var24);} log.error ("Catalina.start", var24);} long T2 = System.nanoTime () If (log.isInfoEnabled ()) {log.info ("Initialization processed in" + (T2-T1) / 1000000L + "ms");}}
And from here on to the next step, the startup process of Server, because the initialization method of getServer has been found from Catalina, the next step is to initialize Server, then load, and then start the process.
3.2 start-up process of Server
Server is an interface in Tomcat, not a class, so we have to find the subclass that implements it, so we find StandardServer extends LifecycleMBeanBase implements Server.
As soon as Ah Fan saw that there was inheritance and implementation, he first looked at LifecycleMBeanBase, the inherited class, so he went to see it again.
Public abstract class LifecycleMBeanBase extends LifecycleBase implements JmxEnabled {
Yeah? And inheritance? Continue to pull down.
Public abstract class LifecycleBase implements Lifecycle {
Finally found it.
As soon as I saw that the init method and the start method called initInternal () and startInternal (), they looked around and went back, and A Fan also learned from here that the template method has its own subclass concrete implementation.
So I went back to StandardServer's own init and start methods.
Protected void startInternal () throws LifecycleException {this.fireLifecycleEvent ("configure_start", (Object) null); this.setState (LifecycleState.STARTING); this.globalNamingResources.start (); Object var1 = this.servicesLock; synchronized (this.servicesLock) {for (int I = 0; I < this.services.length; + + I) {this.services [I] .start () }}}
All in all, StandardServer inherits from LifecycleMBeanBase, LifecycleMBeanBase inherits from LifecycleBase, and the template methods in the LifecycleBase class let their subclasses do the concrete implementation, but we need to know that they exist in his Tomcat life cycle.
It is all said in the picture that there is Service in Server, so there must be. We have to look for it, so A Fan goes to look for it again and to see what it means.
Public void addService (Service service) {service.setServer (this); Object var2 = this.servicesLock; synchronized (this.servicesLock) {Service [] results = new Service [this.services.length + 1]; System.arraycopy (this.services, 0, results, 0, this.services.length); results [this.services.length] = service; this.services = results If (this.getState (). IsAvailable ()) {try {service.start ();} catch (LifecycleException var6) {;}} this.support.firePropertyChange ("service", (Object) null, service);}}
The location is that there are methods to add and delete Service in the interface of Server, and the init method and start method of Server loop to call the init method and start method of each Service.
Next, let's look at the specific implementation of Service and find StandardService:
Protected void initInternal () throws LifecycleException {super.initInternal (); if (this.engine! = null) {this.engine.init ();} Executor [] arr$ = this.findExecutors (); int len$ = arr$.length; int len$; for (len$ = 0; len$ < len$; + + len$) {Executor executor = arr$ [len$] If (executor instanceof JmxEnabled) {((JmxEnabled) executor) .setDomain (this.getDomain ());} executor.init ();} this.mapperListener.init (); Object var11 = this.connectorsLock; synchronized (this.connectorsLock) {Connector [] arr$ = this.connectors; len$ = arr$.length For (int i$ = 0; i$ < len$; + + i$) {Connector connector = arr$ [i$]; try {connector.init ();} catch (Exception var9) {String message = sm.getString ("standardService.connector.initFailed", new Object [] {connector}); log.error (message, var9) If (Boolean.getBoolean ("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) {throw new LifecycleException (message);}}
In the method, the init method of Executor,mapperListener,executor is mainly called.
Connector has existed before, and this mapperListener is the listener of Mapper, which is used to strengthen the changes of the container container.
At this point, I believe you have a deeper understanding of "what is the start-up process of Tomcat". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.