//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ========================================================================
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================

[[shutdown-handler]]
=== Shutdown Handler

[[shutdown-handler-metadata]]
==== Info

* Classname: `org.eclipse.jetty.server.handler.ShutdownHandler`
* Maven Artifact: org.eclipse.jetty:jetty-server
* Javadoc: {JDURL}/org/eclipse/jetty/server/handler/ShutdownHandler.html
* Xref: {JXURL}/org/eclipse/jetty/server/handler/ShutdownHandler.html

[[shutdown-handler-usage]]
==== Usage

A handler that shuts the server down on a valid request. 
Used to do "soft" restarts from Java. 
If _exitJvm is set to true a hard System.exit() call is being made.

This is an example of how you can setup this handler directly with the Server, it can be added as a part of handler chain or collection as well.

[source, java, subs="{sub-order}"]
----

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

    
----

And this is an example that you can use to call the shutdown handler from within java.

[source, java, subs="{sub-order}"]
----

   public static void attemptShutdown(int port, String shutdownCookie) {
        try {
            URL url = new URL("http://localhost:" + port + "/shutdown?token=" + shutdownCookie);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("POST");
            connection.getResponseCode();
            logger.info("Shutting down " + url + ": " + connection.getResponseMessage());
        } catch (SocketException e) {
            logger.debug("Not running");
            // Okay - the server is not running
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    
----
