Javalin: Add documentation or example of how to do a clean shutdown when started from main()

Created on 28 Apr 2019  路  4Comments  路  Source: tipsy/javalin

Starting Javalin from main() is simple enough. But from there, how do I shut down the server with stop(), so I can take advantage of the shutdown lifecycle event, as opposed to simply killing the VM?

If I run e.g. Tomcat, I can do a clean stop with the shutdown command, or directly from the IDE if I add the server to the IDE.

I looked at the documentation but couldn't find anything about this; I think it would be good to explain how it how you are supposed to invoke stop() from outside the program (i.e. the equivalent of stopping a "regular" web server.

INFO

Most helpful comment

Cool, I didn't know about Signal, thanks! :+1:

Unfortunately it doesn't quite work for me as I'm running jdk11, and I think sun.misc.* is "unsafe" now, so that doesn't work out of the box anymore.

However, in the process of investigating the issue I came across a different solution that seems to work perfectly (that I didn't know about either):

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    app.stop();
}));

I even tried it with a 5 sec sleep() in JavalinEvent.SERVER_STOPPING and it's all good, JavalinEvent.SERVER_STOPPED is also called afterwards.

So your suggestion was equally valuable in the end :)

Either way, I definitely think this should be added to the documentation page.

All 4 comments

I did something along those lines in my project at https://github.com/ExploratoryEngineering/tkaq

I simply listen to a Signal

    Signal.handle(Signal("INT")) {
        app.stop()
    }

then I have

    app.event(JavalinEvent.SERVER_STOPPING) {
        // Cleanup
    }

and finally

    app.event(JavalinEvent.SERVER_STOPPED) {
        exitProcess(0)
    }

Source

Cool, I didn't know about Signal, thanks! :+1:

Unfortunately it doesn't quite work for me as I'm running jdk11, and I think sun.misc.* is "unsafe" now, so that doesn't work out of the box anymore.

However, in the process of investigating the issue I came across a different solution that seems to work perfectly (that I didn't know about either):

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    app.stop();
}));

I even tried it with a 5 sec sleep() in JavalinEvent.SERVER_STOPPING and it's all good, JavalinEvent.SERVER_STOPPED is also called afterwards.

So your suggestion was equally valuable in the end :)

Either way, I definitely think this should be added to the documentation page.

Either way, I definitely think this should be added to the documentation page.

Would you like to create a PR for the docs? The "stopping" section is here: https://github.com/javalin/javalin.github.io/blob/master/pages/docs-present.md#starting-and-stopping

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bvicenzo picture bvicenzo  路  3Comments

mkpaz picture mkpaz  路  4Comments

maxemann96 picture maxemann96  路  5Comments

gane5h picture gane5h  路  3Comments

mikexliu picture mikexliu  路  3Comments