user3732793
user3732793

Reputation: 1959

ShutdownHandler is giving 400 in Embedded Jetty

these versions of libraries are used

    <java.version>1.8</java.version>
    <javax.websockets.version>1.1</javax.websockets.version>
    <jetty.version>9.3.8.v20160314</jetty.version>
    <jersey.version>2.22.2</jersey.version>
    <jgit.version>4.3.0.201604071810-r</jgit.version>
    <json.version>20160212</json.version>
    <junit.version>4.12</junit.version>
    <slf4j.version>1.7.12</slf4j.version>
    <maven.shade.version>2.4.1</maven.shade.version>

embedded Jetty is used like that

    Server server = new Server(Settings.PORT);
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setWelcomeFiles(new String[] { "./html/index.html" });
    resourceHandler.setResourceBase("./ressources/webcontent");

    ShutdownHandler shutdownHandler = new ShutdownHandler("switchoff", true, true);

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resourceHandler, shutdownHandler, new DefaultHandler() });
    server.setHandler(handlers);

This shows the index.html

http://localhost:22279/

but this fails with 400

http://localhost:22279/shutdown?token="switchoff"

any idea why ?

Upvotes: 0

Views: 371

Answers (2)

user3732793
user3732793

Reputation: 1959

solved it by basically copy/pastings the snipped from the documentation...not sure if that is a nice approach as it has a "losed with pending callback" complain but it works

so in a Jersey Context Holder

@GET
@Path("shutdown")
@Produces(MediaType.TEXT_PLAIN)
public String shutdown(){
    new Thread(new ShutDown()).start();                 
    return "Down";
}

and the Shutdown Class looks like that

public class ShutDown implements Runnable{

private static final org.slf4j.Logger log = LoggerFactory.getLogger(ShutDown.class);

private static final String SHUTDOWNCOOKIE = "switchoff";

public void run() {
    try {
        URL url = new URL("http://localhost:8080/shutdown?token=" + SHUTDOWNCOOKIE);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.getResponseCode();
        String attempt = "Shutting down " + url + ": " + connection.getResponseMessage();  
        log.info(attempt);
    } catch (Exception e) {
        String error = "Error " + e.getMessage(); 
        log.debug(error);
    }
}

}

any tailoring comment is greatly appreciated !

Upvotes: 0

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49462

The javadoc for ShutdownHandler says that the shutdown request is a POST request.

The call to http://localhost:22279/shutdown?token=switchoff then needs to be a POST request (also without quotes).

Also note, that the ShutdownHandler has a sendShutdown() method to help you.

And since Jetty is an open source project, you can even look to see how sendShutdown() is implemented.

Upvotes: 0

Related Questions