KingTravisG
KingTravisG

Reputation: 1336

Shutdown Tomcat instance using Java

I am running an application using tomcat as the container - at start up, several files need to be found and loaded. However, if one of these files doesn't exist or can't be read, I want to log the exception and exit the app, which I am currently doing using System.exit(1)... however, is there a better way of doing this?

Any help is much appreciated!

Upvotes: 2

Views: 2947

Answers (2)

Stefan
Stefan

Reputation: 12453

I dont know if this fits your needs but it actually worked for my app. The listener is called at application start, if it is declared in your web.xml:

<listener>
    <listener-class>your.package.TestServletListener</listener-class>
</listener>

There you can do testing and call the ShutdownThread if one fails. It will connect to Tomcats shutdown port and send the shutdown command as a String:

public class TestServletListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent arg0) {
    System.out.println("Starting app, running 5 tests ...");

    // do tests ...
    for (int i = 0; i < 5; i++) {
        System.out.println("testing ... " + i);
        waitFor(1000);
    }
    // If a test failed call:
    System.out.println("test failed!");
    new ShutdownTask().start();
}

@Override
public void contextDestroyed(ServletContextEvent arg0) {
    System.out.print("Stopping app, cleaning up (takes 3 sec) ... ");
    waitFor(3000);
    System.out.println("done");
}

private void waitFor(int i) {
    try {
        Thread.sleep(i);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

class ShutdownTask extends Thread {

    @Override
    public void run() {
        try {
            Socket s = new Socket("127.0.0.1", 8015);
            PrintStream os = new PrintStream(s.getOutputStream());
            os.println("shutdown");
            s.close();
            System.out.println("Shutting down server ...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

You need to make sure that the shutdown port and shutdown command are in sync with your Tomcats server.xml:

...
<Server port="8015" shutdown="shutdown">
...

For example, you could pass them as context parameters in your web.xml. Like System.exit(...) this is not going to work (without further config) if Tomcat is running with a SecurityManager.

Upvotes: 3

Cebence
Cebence

Reputation: 2416

You should consider embedding Tomcat, i.e. have your AppStarter class perform those checks and then start Tomcat:

public class AppStarter {
    public static void main(String[] args) {
        // Check if everything is ready...
        if (file1.exists() && file2.exists() && condition3) {
            // Start Tomcat here.
        }
        else {
            System.out.println("Invalid configuration.");
        }
    }
}

You can find how to embed Tomcat tutorials on the Internet.

Upvotes: 1

Related Questions