程序化 Jetty 关闭

发布于 2024-11-02 10:43:12 字数 365 浏览 10 评论 0原文

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

我像这样启动jetty服务器:

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

现在,我想根据请求关闭它,例如http://127.0。 0.1:8090/关机 我该如何干净利落地做呢?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜心 2024-11-09 10:43:12

我发现了一个非常干净整洁的方法这里

神奇的代码片段是:-

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

因为关闭是从一个单独的线程运行的,它不会自行失败。

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.

云淡月浅 2024-11-09 10:43:12

尝试server.setGracefulShutdown(stands_for_milliseconds);

我认为它类似于thread.join(stands_for_milliseconds);

Try server.setGracefulShutdown(stands_for_milliseconds);.

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

贩梦商人 2024-11-09 10:43:12

不建议通过 HTTP 请求远程关闭 Jetty 服务器,因为这会带来潜在的安全威胁。在大多数情况下,通过 SSH 连接到托管服务器并在其中运行适当的命令来关闭 Jetty 服务器的相应实例就足够了。

基本思想是启动一个单独的线程作为 Jetty 启动代码的一部分(因此不需要按照评论答案中提到的其中之一的要求进行睡眠),该线程将作为服务线程来处理关闭请求。在此线程中,ServerSocket 可以绑定到本地主机和指定端口,当收到预期消息时,它将调用 server.stop()

这篇博文提供了使用上述方法进行详细讨论。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文