程序化Jetty关闭 [英] Programmatic Jetty shutdown

查看:167
本文介绍了程序化Jetty关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式关闭嵌入式jetty服务器?

How to programmatically shutdown embedded jetty server?

我像这样启动jetty服务器:

I start jetty server like this:

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

现在,我想从请求中关闭它,例如 http://127.0.0.1:8090/shutdown
如何干净利落地完成?

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?

通常建议的解决方案是创建一个线程并从该线程调用server.stop()。
但是我可能需要调用Thread.sleep()以确保servlet已经完成处理关闭请求。

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.

推荐答案

我找到了一个非常干净整洁的方法这里

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.

这篇关于程序化Jetty关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆