简体   繁体   中英

stop embeddable Tomcat from java code

This might be a newbie question, but still, I need help!!

I have a java class that succesfully starts embeddable Tomcat like this:

    public class Main {

    private static Tomcat tomcat;

    public static void main(String[] args) throws Exception {

        String webappDirLocation = "webapps/";
        tomcat = new Tomcat();

        String webPort = System.getenv("PORT");
        if(webPort == null || webPort.isEmpty()) {
            webPort = "8080";
        }

        tomcat.setPort(Integer.valueOf(webPort));
        StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());

        tomcat.start();
        tomcat.getServer().await();
    }
}

My question is, ¿how can I stop it?

I tried to add another function for this purpose like this:

    public static void stopTomcat() throws Exception {

    if (tomcat.getServer() != null) {
        tomcat.stop();
        tomcat.destroy();
    }
}

calling it from the main method like this:

        if (args.length > 0 && args[0].equals("stopTomcat")) {
             stopTomcat();
             return;
        }

However, it doesn't work. I'm getting a thread exception:

Exception in thread "main" java.lang.NullPointerException

How can I get the current Tomcat server's variable that is running in order to stop tomcat?

I just need to be able to run a java class from command line in order to stop Tomcat, and I believe it is posible. Using catalina.bat, or Tomcat.exe is out of the question since this is an embeddable version of tomcat.

Any help will be really really appreciated!!

Regards!!

You are probably running your script twice. Once starting the server and another time stopping it.

If you run it only once, you still have the reference to the Tomcat object, and thus can call stop on it. If not, it will be null, or will be a different instance altogether.

The code below, briefly shows how to stop Tomcat from the same script.

    String webappDirLocation = "webapps/";
    Tomcat tomcat = new Tomcat();

    Runnable task2 = () -> {  try {
        Thread.sleep(5000);
        System.out.println("Stopping Tomcat");
        tomcat.stop();
    } catch (Exception e) {
        // TODO Auto-generated catch block
    } };

    // start the thread
    new Thread(task2).start();

    String webPort = System.getenv("PORT");
    if (webPort == null || webPort.isEmpty()) {
        webPort = "8080";
    }

    tomcat.setPort(Integer.valueOf(webPort));
    tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());

    tomcat.start();
    tomcat.getServer().await();

Apart from a timer, you could also stop the server when pressing a key:

    ... 
    tomcat.start();
    //      tomcat.getServer().await();

    System.out.print("Press key to stop ");     
    if(System.in.read()!=-1) {
        System.out.print("Stopping");
        tomcat.stop();
    }

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