java 嵌入式 Jetty 和优雅关机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25011249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:08:51  来源:igfitidea点击:

Embedded Jetty and graceful shutdown

javajetty

提问by Odinodin

I've dug through the Jetty documentation trying to find out how to properly configure an embedded Jetty to shut down gracefully, but I found it lacking.

我翻阅了 Jetty 文档,试图找出如何正确配置嵌入式 Jetty 以正常关闭,但我发现它缺乏。

The examples in the documentationinconsequently use setStopAtShutdown(true). However, there is no JavaDoc or explanation why this should be done. As far as I can tell, the default value is set to false.

文档中的示例不合时宜地使用setStopAtShutdown(true). 但是,没有 JavaDoc 或解释为什么应该这样做。据我所知,默认值设置为 false。

Additionally, the setGracefulShutdown()method changed to setStopTimeout()it seems, but this is not documented either.

此外,setGracefulShutdown()方法更改为setStopTimeout()它似乎,但这也没有记录。

So these are my questions:

所以这些是我的问题:

  1. Why would you set or not set stop at shutdown?
  2. When would you override the stop timeout (defaults to 30 seconds)?
  3. What other things should be taken into consideration when configuring Jetty for graceful shutdown?
  1. 为什么要设置或不设置关机时停止?
  2. 您何时会覆盖停止超时(默认为 30 秒)?
  3. 在配置 Jetty 以实现正常关机时,还应考虑哪些其他事项?

Edit: After some trial and error; discovered that setStopAtShutdown(true) is required if you want to have Jetty signal a shutdown event to any listeners such as Spring's ContextLoaderListener.

编辑:经过一些试验和错误;发现如果您想让 Jetty 向任何侦听器(例如 Spring 的 ContextLoaderListener)发出关闭事件信号,则需要 setStopAtShutdown(true)。

采纳答案by jbyler

The graceful shutdown procedure requests each worker thread to shut down (i.e. finish processing the current request, then don't accept any new requests and exit). It then waits for a bit (configurable via the timeout), and if any threads remain, it kills them forcefully. This can happen because the thread is taking a long time to process a request, for example, or because of a deadlock. If you keep the default value of 30 seconds, it might take a little more than 30 seconds for the application to exit. Reducing the value of the timeout will give you a faster shutdown time, but at the expense of potentially killing threads that were busy processing legitimate, active requests.

优雅关闭程序要求每个工作线程关闭(即处理完当前请求,然后不接受任何新请求并退出)。然后等待一段时间(可通过超时进行配置),如果还有任何线程存在,它就会强行杀死它们。这可能是因为线程需要很长时间来处理请求,例如,或者因为死锁。如果保持默认值 30 秒,则应用程序退出可能需要 30 秒多一点的时间。减少超时值将使您的关闭时间更快,但代价是可能会杀死忙于处理合法、活动请求的线程。

回答by alexgirao

The default stopAtShutdownseems to be true, see at http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-server/src/main/config/etc/jetty.xml?h=jetty-9.1.x, the timeout is also there, 5 seconds:

默认stopAtShutdown似乎是真的,见http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-server/src/main/config/etc/jetty.xml ?h=jetty-9.1.x,超时也有,5秒:

<Set name="stopAtShutdown">true</Set>
<Set name="stopTimeout">5000</Set>

Additionally, you may configure a LifeCycle.Listener for more specific cases, e.g., I use one to remove a pid file (among other things) when shutting down the server, I attach the listener in the Connector using addLifeCycleListener on the ServerConnector, in jetty xml, it looks like:

此外,您可以为更具体的情况配置 LifeCycle.Listener,例如,我在关闭服务器时使用一个来删除 pid 文件(除其他外),我在码头的 ServerConnector 上使用 addLifeCycleListener 将侦听器附加到连接器中xml,它看起来像:

<Configure id="Server" class="org.eclipse.jetty.server.Server">
  ...
  <Call name="addConnector">
    <Arg>
      <New class="org.eclipse.jetty.server.ServerConnector">
        ...
        <Call name="addLifeCycleListener">
          <Arg>
            <New class="com.acme.jetty.ConnectorListener"/>
          </Arg>
        </Call>

and ConnectorListener.java would begin with something like:

和 ConnectorListener.java 会以类似的内容开头:

package com.acme.jetty;

import org.eclipse.jetty.util.component.LifeCycle;

import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

public class ConnectorListener implements LifeCycle.Listener {
    private static final Logger LOG = Log.getLogger(ConnectorListener.class);
    ...
    public void lifeCycleStopped(LifeCycle event) {
        LOG.debug("lifeCycleStopped()");
    }

I know this does not address your questions directly, but maybe you could provide us more information.

我知道这不能直接解决您的问题,但也许您可以向我们提供更多信息。