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

How can Tomcat start and stop with one button?

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to start and stop with one click in Tomcat". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to start and stop with one click in Tomcat.

Script analysis

The contents of start.sh and start.bat are the same, so here we will mainly analyze the content of start.sh.

Os400=false case "`uname`" in OS400*) os400=true;; esac # resolve links-$0 may be a softlink # PRG is the script path. If the current script file is soft connection, the path PRG= "$0" while [- h "$PRG"] where the real PRG file is located will be resolved. Do # determine whether it is a soft connection ls= `ls-ld "$PRG" `# if it is a soft connection, the output contains the string link= `expr "$ls":'. *->\ (. *\) $'# pattern matches the path of the source file if expr "$link":'/. *'> / dev/null Then # regular matching /. * here expr will output the number of matches. If it is not 0, $link contains the directory PRG= "$link" else PRG= `dirname "$PRG" `/ "$link" # if it does not contain directories, and the soft connection and the source files are in the same directory fi done # to get the script directory path PRGDIR= `dirname "$PRG" $EXECUTABLE=catalina.sh # Check that target executable exists if $os400. Then #-x will Only work on the os400 if the files are: # 1. Owned by the user # 2. Owned by the PRIMARY group of the user # this will not work if the user belongs in secondary groups eval else if [!-x "$PRGDIR" / "$EXECUTABLE"] Then echo "Cannot find $PRGDIR/$EXECUTABLE" echo "The file is absent or does not have execute permission" echo "This file is needed to run this program" exit 1 fi fi # execute catalina.sh 's start command exec "$PRGDIR" / "$EXECUTABLE" start "$@"

In fact, to put it simply, the above has done two things.

Get the real path to the script

Execute the start command of catalina.sh

The shutdown.sh command is the same as the start.sh command, except that it is followed by the stop command of catalina.sh

Catalina.sh script

The important steps in the script are as follows

1. Set two important environment variables, CATALINA_HOME and CATALINA_BASE

PRGDIR= `dirname "$PRG" `[- z "$CATALINA_HOME"] & & CATALINA_HOME= `cd "$PRGDIR/.." > / dev/null; pwd` [- z "$CATALINA_BASE"] & & CATALINA_BASE= "$CATALINA_HOME"

Set the CLASSPATH variable. Note here that there is no setenv.sh file by default. You can create a new one and add parameters.

CLASSPATH= if [- r "$CATALINA_BASE/bin/setenv.sh"]; then. "$CATALINA_BASE/bin/setenv.sh" elif [- r "$CATALINA_HOME/bin/setenv.sh"]; then. "$CATALINA_HOME/bin/setenv.sh" fi

Pass bootstrap.jar as a CLASSPATH variable

If [!-z "$CLASSPATH"]; then CLASSPATH= "$CLASSPATH": fi CLASSPATH= "$CLASSPATH"$CATALINA_HOME" / bin/bootstrap.jar if [- z "$CATALINA_OUT"]; then CATALINA_OUT= "$CATALINA_BASE" / logs/catalina.out fi

Execute the script parameter, execute the main method in the Bootstrap class in bootstrap.jar, and pass in the parameter start

Hift eval exec "\" $_ RUNJAVA\ "\" $LOGGING_CONFIG\ "" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS\-Dipper JAVA_ENDORSED_DIRS\ "\-classpath"\ "$CLASSPATH\"\-Djava.security.manager\-Djava.security.policy== "\" $CATALINA_BASE/conf/catalina.policy\ "\-Dcatalina.base="\ "$CATALINA_BASE\"\ "Dcatalina.home="\ "$CATALINA_" HOME\ ""\-Djava.io.tmpdir= "\" $CATALINA_TMPDIR\ ""\ org.apache.catalina.startup.Bootstrap "$@" start

In the above script, we can see that the final execution is from Bootstrap's main method as an entry, so we open the Tomcat source code and go into the Bootstrap class to see what it does.

Initiating class analysis

As an entry class for Tomcat, let's first take a look at what is done in Bootstrap. Only the important code in the main method is posted here.

/ / initialize the classloader and load the Catalina file into memory bootstrap.init (); String command = "start"; if (args.length > 0) {command = args [args.length-1];} if (command.equals ("startd")) {args [args.length-1] = "start"; / / call Catalina.java 's load method daemon.load (args); / / call Catalina.java 's start daemon.start () } else if (command.equals ("stopd")) {args [args.length-1] = "stop"; / / call Catalina.java 's stop daemon.stop ();} else if (command.equals ("start")) {daemon.setAwait (true); 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.");}

Here, different methods of Catalina are called depending on the different commands passed in the script. Because we mainly analyze how Tomcat can start and stop with one button, we mainly analyze the start method of Catalina.

We see this sentence in Catalina's satrt method.

GetServer () .start ()

Then after Debug, we all go through the start method of Lifecycle, and we list the methods of Lifecycle.

Public interface Lifecycle {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 void destroy () throws LifecycleException; public LifecycleState getState (); public String getStateName (); public interface SingleUse {}}

Then looking at its implementation class, we find that the components in the overall architecture we mentioned earlier implement this class. And in its subclass LifecycleBase implements start, init, stop and other methods, and there are corresponding calls to startInternal, initInternal, stopInternal methods, here if we know about the design pattern, we should think of here the use of template design patterns, abstract the public code of all subclasses, and then redefine an internal abstract method, and its subclasses implement their own customized operations.

In Server.xml, we found that the first level is also Server, and then the first launch in Catalina's satrt method is also Server.

The above shows the hierarchical structure of all modules in Tomcat. As long as it has a hierarchical structure, we should be able to think of the combinatorial design pattern immediately. From this hierarchical structure, we can get the relationship between modules, big and small, inside and outside.

Big and small: large component management widgets, such as Server management Service,Service management connectors and containers

Inside and outside: the connector controls the external connection, while the outer component calls the inner component to complete the business function. That is, the process of request processing is driven by the outer component.

Well, according to the above two items, we know that there is a small, there is a big, there is an inside, there is an outside. This is the loading order of the entire level, loading the widget first and then the large component, loading the inner component first and then loading the outer component. At this point, we should understand how Tomcat can start and stop with one button. Through the hierarchy, the priority of the load. Iterate layer by layer to start. And stopping is almost the same as starting. It is also a layer of iterations to stop.

Thank you for your reading, the above is the content of "how to achieve one-click start and stop of Tomcat". After the study of this article, I believe you have a deeper understanding of how to do one-click start and stop of Tomcat, 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: 235

*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

Servers

Wechat

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

12
Report