简体   繁体   中英

Programmatically shutdown Jetty with xml configured server

I'm using a jetty server that is configured in xml, like this: http://wiki.eclipse.org/Jetty/Reference/jetty.xml

I need to be able to shut this server down using java code, but I don't know how to retrieve a handle to the server. I'm completely lost on how to do this and it's been stumping me for days. Any insight at all, even just some reading material that might be useful, is appreciated.

Please request more information if required.


Update

I've added a ShutdownHandler to my handler list in jetty.xml based on the following code:

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

However, I'm a bit iffy on how I accomplished it and I don't know of any real way of checking if it is incorrect. Am I allowed to reference the Jetty Server in this way from inside the configuration tags?

<Item>
 <New id="ShutdownHandler" class="org.eclipse.jetty.server.handler.ShutdownHandler">
  <Arg><Ref id="Server"/></Arg>
  <Arg type="java.lang.String">password</Arg>
 </New>
</Item>

Also, I am a bit confused about how to retrieve the shutdown cookie in URL url = new URL("http://localhost:" + port + "/shutdown?token=" + shutdownCookie);

Does it have anything to do with the password or key? Because I tried both and both gave me java.net.SocketException: Unexpected end of file from server when connection.getResponseCode(); was called.

This is one approach:

http://www.eclipse.org/jetty/documentation/current/shutdown-handler.html

You could configure the handler in the xml and then have a small java main class to stop it. I don't know that you would want to do a shutdown using strictly jetty.xml like you seem to be originally asking. There is also a STOP key mechanism if you are starting up with the distribution (which you typically are if using jetty.xml) that you can use which I'll link to here:

http://www.eclipse.org/jetty/documentation/current/quickstart-running-jetty.html#quickstart-stopping-jetty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM