How to gracefully shutdown embeded jetty

14,283

Solution 1

There is no predefined solution to shut-down the Jetty server. The only ordered way to shut-down the Jetty server is to call the method stop() on the running server instance. You must implement the way how this method is called yourself.

You could achieve this (for example) by...

  • implementing an RMI server thread and invoke the method from a RMI client
  • implementing a JMX MBean and from a client call a method on that MBean
  • implementing a custom handler like described in the link you have posted

If you only want to find a way which does not depend on additional tools like curl, than you could solve it for example like below (it's your own code with small modifications)

public class MyJetty {

    public static void main(String[] args) throws Exception {
        int PORT = 9103;
        String home = System.getProperty("user.home");
        String logFileDateFormat = "yyyy_MM_dd";

        // execute a request to http://localhost:9103/stop
        // instead of `curl -v http://localhost:9103/stop`
        if (args.length == 1 && "stop".equalsIgnoreCase(args[0])) {
            URL url = new URL("http", "localhost", PORT, "/stop");
            try (InputStream in = url.openStream()) {
                int r;
                while ((r = in.read()) != -1) {
                    System.out.write(r);
                }
                return;
            } catch (IOException ex) {
                System.err.println("stop Jetty failed: " + ex.getMessage());
            }
        }

        Server server = new Server();
        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(PORT);
        server.addConnector(connector);
        HandlerCollection handlers = new HandlerCollection();
        NCSARequestLog requestLog = new NCSARequestLog();
        requestLog.setFilename(home + "/logs/access_" + logFileDateFormat + ".log");
        requestLog.setFilenameDateFormat(logFileDateFormat);
        requestLog.setRetainDays(10);
        requestLog.setAppend(true);
        requestLog.setExtended(false);
        requestLog.setLogCookies(false);
        requestLog.setLogTimeZone(TimeZone.getDefault().getID());
        RequestLogHandler requestLogHandler = new RequestLogHandler();
        requestLogHandler.setRequestLog(requestLog);
        handlers.addHandler(requestLogHandler);

        // the class YourHandler is the one from your link
        handlers.addHandler(new YourHandler(server));

        server.setHandler(handlers);
        server.start();
        server.join();
    }
}
  • start the server with java MyJetty
  • stop the server with java MyJetty stop

Solution 2

Since Jetty 7.5.x you can use org.eclipse.jetty.server.handler.ShutdownHandler in your code:

Server server = new Server(8080);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]
{ someOtherHandler, new ShutdownHandler("secret_password", false, true) });
server.setHandler(handlers);
server.start();

... which will allow you to shut down your jetty by issuing the following http POST request:

curl -X POST http://localhost:8080/shutdown?token=secret_password

Solution 3

You can call setStopTimeout(long timeout) to shutdown Jetty in a relatively graceful way. A statisticsHandler must be configured when calling this method.

Referencing: Jetty Server.class setStopTimeout(long)

e.g.

YourServletHandler servletHandler = new YourServletHandler();
StatisticsHandler statsHandler = new StatisticsHandler();
statsHandler.setHandler(servletHandler);

Server server = new Server(80);
server.setHandler(statsHandler);
server.setStopTimeout(3000L);

//...
server.start();

//...
server.stop();
Share:
14,283
jos
Author by

jos

Updated on June 20, 2022

Comments

  • jos
    jos almost 2 years

    I have an application runs on an embedded jetty server. Now i want to start/stop the server as a service. I use a script to start the server.

    java $JAVA_OPTS -DREQ_JAVA_VERSION=$JAVA_VERSION -jar myjetty.jar
    

    Main Class

    Server server = new Server();
    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(PORT);
    server.addConnector(connector);
    HandlerCollection handlers = new HandlerCollection();
    NCSARequestLog requestLog = new NCSARequestLog();
    requestLog.setFilename(home + "/logs/access_" + logFileDateFormat
                + ".log");
    requestLog.setFilenameDateFormat(logFileDateFormat);
    requestLog.setRetainDays(10);
    requestLog.setAppend(true);
    requestLog.setExtended(false);
    requestLog.setLogCookies(false);
    requestLog.setLogTimeZone(TimeZone.getDefault().getID());
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    requestLogHandler.setRequestLog(requestLog);
    handlers.addHandler(requestLogHandler);
    server.setHandler(handlers);
    server.start();
    server.join();
    

    This starts the server.Stopping and/or Restarting an embedded Jetty instance via web call can be used to stop server but, How to stop the server from the script? and what changes should i make to shout down server in the main class.