简体   繁体   中英

How to stop another tomcat from Java code (JSP/Servlet)

I have two tomcats Installed as below:

  1. Tomcat-A {Path: C:\TomcatA, Ports: Connector port="8080" redirectPort="8443" shutdown port as -1}
  2. Tomcat-B {Path: C:\TomcatB, Ports: Connector port="8090" redirectPort="8444" shutdown port as 8015}

I have a JSP/Servlet Applications which runs on Tomcat-A. My requirement is to shutdown Tomcat-B and delete/undeploy application hosted on Tomcat-B. Below is my Java code.

private void StopTomcat() throws Exception {
    if(logger.isDebugEnabled()) logger.debug("StopTomcat");

    try {

        if (logger.isTraceEnabled())logger.trace("Shutting down tomcat: ");

        String Directory = "C:\TomcatB\bin\";

        Process process = Runtime.getRuntime().exec(Directory + "shutdown.bat");

        if (logger.isTraceEnabled())logger.trace("Executed shutdown.bat at " + Directory );
        
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

        if (logger.isTraceEnabled())logger.trace("Printing shutdown process logs" );
        String s = null;
        while ((s = stdInput.readLine()) != null) {
            if (logger.isTraceEnabled())
                logger.trace(s);
        }
        String Error = "";
        while ((s = stdError.readLine()) != null) {
            Error = Error + s;
        }

        if (!Error.equals("")) {
            logger.error(Error);
            throw new Exception("Tomcat Shutdown Failed with error: " + Error);
        }
        
    } catch (Exception e) {
        e.printStackTrace();
        logger.error( Arrays.toString( e.getStackTrace() ));
        throw new Exception(e.getMessage());
    }
}

And in the logs I get:

2020-08-04 21:11:00 TRACE MigrateApp:55 - Executed shutdown.bat at C:\TomcatB\bin\
2020-08-04 21:11:00 TRACE MigrateApp:60 - Printing shutdown process logs
2020-08-04 21:11:00 TRACE MigrateApp:64 - Using CATALINA_BASE:   "TomcatA"
2020-08-04 21:11:00 TRACE MigrateApp:64 - Using CATALINA_HOME:   "TomcatA"
2020-08-04 21:11:00 TRACE MigrateApp:64 - Using CATALINA_TMPDIR: "TomcatA\temp"
2020-08-04 21:11:00 TRACE MigrateApp:64 - Using JRE_HOME:        "C:\Program Files\Java\jre1.8.0_251"
2020-08-04 21:11:00 TRACE MigrateApp:64 - Using CLASSPATH:       "TomcatA\bin\bootstrap.jar;TomcatA\bin\tomcat-juli.jar"
2020-08-04 21:11:00 ERROR MigrateApp:72 - 04-Aug-2020 21:11:00.668 SEVERE [main] org.apache.catalina.startup.Catalina.stopServer No shutdown port configured. Shut down server through OS signal. Server not shut down.
2020-08-04 21:11:00 ERROR MigrateApp:78 - [sailpoint.support.applicationdeployer.util.MigrateApp.StopTomcat(MigrateApp.java:73), sailpoint.support.applicationdeployer.util.MigrateApp.run(MigrateApp.java:36)]
2020-08-04 21:11:00 ERROR MigrateApp:79 - Tomcat Shutdown Failed with error: 04-Aug-2020 21:11:00.668 SEVERE [main] org.apache.catalina.startup.Catalina.stopServer No shutdown port configured. Shut down server through OS signal. Server not shut down.
2020-08-04 21:11:00 ERROR MigrateApp:39 - Tomcat Shutdown Failed with error: 04-Aug-2020 21:11:00.668 SEVERE [main] org.apache.catalina.startup.Catalina.stopServer No shutdown port configured. Shut down server through OS signal. Server not shut down.

I am not able to understand why my code is trying to shutdown Tomcat-A. Can anyone please help me on the same.

If you haven't edited your Tomcat B shutdown.bat script from the default you will see that C:\TomcatB\bin\shutdown.bat determines CATALINA_HOME and executes:

%CATALINA_HOME%\bin\catalina.bat

It looks like your TomcatA VM is passing on its own CATALINA_HOME to Tomcat B shutdown.bat which overrides the default used by C:\TomcatB\bin. You could either make a new script shutdown2.bat which does not assume CATALINA_HOME environment variable, or switch to ProcessBuilder and set CATALINA_HOME in the ProcessBuilder launch command something like this:

File exe = new File(Directory, "shutdown.bat");
String [] cmd = new String[] { exe.getAbsolutePath() };
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.environment().put("CATALINA_HOME", exe.getParentFile().getParentFile().getAbsolutePath());
Process process = pb.start();

If the process locks you may need to consume output/error streams in background thread or redirect them to file by calling before start():

pb.redirectOutput(outfile);
pb.redirectError(errfile);

I am able to achieve it by modifying the exec parameters as below. It's now working correctly.

String Directory = "C:\TomcatB\bin\";
File workingDirecotry = new File(Directory);
Process process = Runtime.getRuntime().exec(Directory + "shutdown.bat", null, workingDirecotry);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM