Shutting Down Jetty

How to programmatically disable the embedded berth server?

I start the console server as follows:

Server server = new Server(8090); ... server.start(); server.join(); 

Now I want to close it from the request, for example http://127.0.0.1:8090/shutdown How can I do this cleanly?

A commonly suggested solution is to create a stream and call server.stop () from that stream. But I may need a call to Thread.sleep () to make sure that the servlet has finished processing the completion request.

+7
source share
3 answers

I found a very clean neat method here

Fragment of the magic code: -

  server.setStopTimeout(10000L);; try { new Thread() { @Override public void run() { try { context.stop(); server.stop(); } catch (Exception ex) { System.out.println("Failed to stop Jetty"); } } }.start(); 

Since shutdown is performed from a separate thread, it does not work on itself.

+6
source

Try server.setGracefulShutdown(stands_for_milliseconds); .

I think it looks like thread.join(stands_for_milliseconds); .

+2
source

Having the remote Jetty server feature remotely using an HTTP request is not recommended because it provides a potential security risk. In most cases, there should be enough SSH for the hosting server and run the appropriate command to shut down the corresponding instance of the Jetty server.

The main idea is to start a separate thread as part of the Jetty startup code (so there is no need to sleep, as required in one of the answers mentioned in the comments), which will serve as a service flow for processing shutdown requests. In this thread, the ServerSocket can be bound to the local host and assigned port, and when the expected message is expected, it will call server.stop() .

This blog post gives a detailed discussion using the above approach.

+1
source

All Articles