Reputation: 21
The docs for spark-java 2.6.0 mention that "Embedded Jetty is now fully configurable".
How do I go about configuring a shutdown hook via Spark 2.6.0?
I've overridden Jetty's Handler.doStop() method in the past, but I'm unclear how something equivalent is accomplished through the Spark framework.
Upvotes: 2
Views: 1356
Reputation: 3909
For people who are searching for an answer for this for newer versions of Spark, here is a reference: http://sparkjava.com/documentation#stopping-the-server
Since URLs on the internet do not live for ever here is the qoute:
By calling the stop() method the server is stopped and all routes are cleared.
And and example of how I use the code:
public static void stopService(){
stop();
awaitStop();
}
What is the usage of this? For example when you want to do a repeated test on the service clearing it and reinitializing it then this comes in handy,
Upvotes: 0
Reputation: 2694
here is how you override Handler.doStop()
in sparkjava:
public class Main {
public static void main(String ...args) throws Exception {
EmbeddedServers.add(EmbeddedServers.Identifiers.JETTY, (Routes routeMatcher, StaticFilesConfiguration staticFilesConfiguration, boolean hasMultipleHandler) -> {
MatcherFilter matcherFilter = new MatcherFilter(routeMatcher, staticFilesConfiguration, false, hasMultipleHandler);
matcherFilter.init(null);
JettyHandler handler = new MyJettyHandler(matcherFilter);
return new EmbeddedJettyServer((int maxThreads, int minThreads, int threadTimeoutMillis) -> new Server(), handler);
});
get("/hello", (req, res) -> {
req.session(true);
return "Hello World";
});
}
}
class MyJettyHandler extends JettyHandler {
public MyJettyHandler(Filter filter) {
super(filter);
}
@Override
protected void doStop() throws Exception {
super.doStop();
// your magic happens here.
}
}
the code where i initialize the embedded jetty is taken from sparkjava itself, so i do not alter the behavior of the framework.
Upvotes: 1