Reputation: 2574
There are several questions about shuting down Jetty here on SO but they are almost 10 years old and do not fully cover my question.
I have an embedded Jetty server in my application and want to shutdown the server from inside of the application. So far, I figured out several ways to do this:
ShutdownHandler shutdownHandler = new ShutdownHandler("shutdownToken");
server.setHandler(new HandlerList(shutdownHandler));
shutdownHandler.sendShutdown();
server.stop();
What is the difference between these thow approaches? I could not find a suitable answer in the Jetty documentation.
Upvotes: 1
Views: 1044
Reputation: 49472
ShutdownHandler
exists to allow an HTTP request to shutdown the server if the request contains the correct password/token to initiate the shutdown.
Not a recommended solution for a server that is publicly accessible on the internet.
server.stop()
is a good solution, but you cannot use that method from a thread that the server manages.
That's why ShutdownHandler
schedules the server.stop()
that it uses on another thread that the Server isn't managing.
Recommended Setup
ShutdownMonitor
to listen on localhost for a command to shutdown, this command has a password/token that must be provided that authorizes the shutdown.Server.setStopTimeout(long)
to allow graceful timeout of server components.StatisticsHandler
on the server handler tree (at the start) to track active connections/requests for the graceful shutdown to wait for active requests to finish before stop is initiated.When it's time to shutdown your server, you now have 2 options.
shutdown
command to processHow jetty-home's start.jar works with this recommended setup
The provided bin/jetty.sh
in the jetty-home
distribution will use essentially the following set of commands.
$ cd /path/to/myjettybase
$ java -jar $JETTY_HOME/start.jar --stop
The configured ${jetty.base}
will contain the various STOP configuration needed to know how to stop the server (STOP_HOST, STOP_PORT, STOP_KEY, STOP_WAIT).
Upvotes: 2