According to this question, a listener can be added to a Spark WS so that an initialized but not yet started thread would be run when the thread finishes its execution.
I want to deploy a simple web service that should be run from console as a JRE application (no application container) and has a small catch: On startup it has to initialize resources, and it has to close them when the server stops. Using the above reference, if I add a shutdown hook to the main method, the code would look like this:
public class foo_ws_main{
private ConnectionPool pool;
public static void main(){
pool.initialize();
post("/ws/path/", (request, response) -> { return pool.doSomething() }
Runtime.getRuntime().addShutdownHook(new Thread(new SparkWS_Shutdown(pool)));
}
}
Unfortunately this doesn't behave as expected: the connection pool would be initialized only once, and its resources freed only once, this is, when the virtual machine is terminated gracefully (cases like SIGKILL don't matter at this point).
Can this be done?