ShutdownHandler is giving 400 in Embedded Jetty

。_饼干妹妹 提交于 2019-12-12 04:31:54

问题


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 ?


回答1:


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.




回答2:


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 !



来源:https://stackoverflow.com/questions/37188585/shutdownhandler-is-giving-400-in-embedded-jetty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!