Jetty:以编程方式停止导致"1个线程无法停止"

Ond*_*žka 6 java shutdown jetty

我有一个嵌入式Jetty 6.1.26实例.我希望通过发送的HTTP GET将其关闭/shutdown.所以我创建了一个JettyShutdownServlet:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

 resp.setStatus(202, "Shutting down.");
 resp.setContentType("text/plain");
 ServletOutputStream os = resp.getOutputStream();
 os.println("Shutting down.");
 os.close();
 resp.flushBuffer();

 // Stop the server.
 try {
    log.info("Shutting down the server...");
    server.stop();
 } catch (Exception ex) {
    log.error("Error when stopping Jetty server: "+ex.getMessage(), ex);
 }
Run Code Online (Sandbox Code Playgroud)

但是,当我发送请求时,Jetty不会停止 - 一个线程一直挂org.mortbay.thread.QueuedThreadPool在线上this.wait():

   // We are idle
   // wait for a dispatched job
   synchronized (this)
   {
       if (_job==null)
          this.wait(getMaxIdleTimeMs());
       job=_job;
       _job=null;
   }
Run Code Online (Sandbox Code Playgroud)

...

2011-01-10 20:14:20,375 INFO  org.mortbay.log jetty-6.1.26
2011-01-10 20:14:34,756 INFO  org.mortbay.log Started SocketConnector@0.0.0.0:17283
2011-01-10 20:25:40,006 INFO  org.jboss.qa.mavenhoe.MavenHoeApp Shutting down the server...
2011-01-10 20:25:40,006 INFO  org.mortbay.log Graceful shutdown SocketConnector@0.0.0.0:17283
2011-01-10 20:25:40,006 INFO  org.mortbay.log Graceful shutdown org.mortbay.jetty.servlet.Context@1672bbb{/,null}
2011-01-10 20:25:40,006 INFO  org.mortbay.log Graceful shutdown org.mortbay.jetty.webapp.WebAppContext@18d30fb{/jsp,file:/home/ondra/work/Mavenhoe/trunk/target/classes/org/jboss/qa/mavenhoe/web/jsp}
2011-01-10 20:25:43,007 INFO  org.mortbay.log Stopped SocketConnector@0.0.0.0:17283
2011-01-10 20:25:43,009 WARN  org.mortbay.log 1 threads could not be stopped
2011-01-10 20:26:43,010 INFO  org.mortbay.log Shutdown hook executing
2011-01-10 20:26:43,011 INFO  org.mortbay.log Shutdown hook complete
Run Code Online (Sandbox Code Playgroud)

它会阻塞一分钟,然后关闭.我添加了Graceful shutdown,这应该允许我从servlet关闭服务器; 但是,它从日志中看不到.

我这样解决了:

Server server = new Server( PORT );
server.setGracefulShutdown( 3000 );
server.setStopAtShutdown(true);
...
server.start();

if( server.getThreadPool() instanceof QueuedThreadPool ){
   ((QueuedThreadPool) server.getThreadPool()).setMaxIdleTimeMs( 2000 );
}
Run Code Online (Sandbox Code Playgroud)

setMaxIdleTimeMs()需要在之后调用,因为start()创建了threadPool start().但是,线程已经创建并等待,因此它仅在所有线程至少使用一次后才适用.

我不知道还有什么可做的,除了一些可怕的事情,比如打断所有线程或者System.exit().

有任何想法吗?有好办法吗?

Tim*_*Tim 11

优美没有做什么你认为它-它允许服务器安全关机,但它并没有让你关闭从一个servlet中.

问题是您链接到的邮件列表帖子中描述的 - 您正在尝试停止服务器,而您仍在处理服务器内部的连接.

您应该尝试将servlet的实现更改为:

// Stop the server.
new Thread()
{
   public void run() {
     try {
        log.info("Shutting down the server...");
        server.stop();
        log.info("Server has stopped.");
     } catch (Exception ex) {
        log.error("Error when stopping Jetty server: "+ex.getMessage(), ex);
     }
   }
}.start();
Run Code Online (Sandbox Code Playgroud)

这样,servlet可以在服务器关闭时完成处理,并且不会阻止关闭过程.