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.
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)
}
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
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):
I even tried it with a 5 sec sleep() in
JavalinEvent.SERVER_STOPPINGand it's all good,JavalinEvent.SERVER_STOPPEDis 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.