Programmatic Jetty shutdown

纵然是瞬间 提交于 2019-12-04 09:45:05

问题


How to programmatically shutdown embedded jetty server?

I start jetty server like this:

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

Now, I want to shut it down from a request, such as http://127.0.0.1:8090/shutdown How do I do it cleanly?

The commonly proposed solution is to create a thread and call server.stop() from this thread. But I possibly need a call to Thread.sleep() to ensure that the servlet has finished processing the shutdown request.


回答1:


I found a very clean neat method here

The magic code snippet is:-

        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();

Because the shutdown is running from a separate thread, it does not trip up over itself.




回答2:


Try server.setGracefulShutdown(stands_for_milliseconds);.

I think it's similar to thread.join(stands_for_milliseconds);.




回答3:


Having the ability for a Jetty server to be shutdown remotely through a HTTP request is not recommended as it provides as potential security threat. In most cases it should be sufficient to SSH to the hosting server and run an appropriate command there to shutdown a respective instance of a Jetty server.

The basic idea is to start a separate thread as part of Jetty startup code (so there is no need to sleep as required in one of mentioned in the comment answers) that would serve as a service thread to handle shutdown requests. In this thread, a ServerSocket could be bound to localhost and a designated port, and when an expected message is received it would call server.stop().

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



来源:https://stackoverflow.com/questions/5719159/programmatic-jetty-shutdown

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