我正在使用泽西服务器并希望确保当服务器终止时(通常通过SIGTERM),所有当前正在运行的请求都会正常完成。如果音量足够高,如果我不这样做,可能会有数据丢失。
所以我试图从运行时关闭钩子中调用HttpServer。shutdown()。
我认为它正常工作,除了一个问题。使用InterruptedException调用CompletionHandler。failed()。 shutdownNow在堆栈跟踪中,因此在关闭本身完成后似乎只发生了一些逻辑错误:
java.lang.InterruptedException
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1302)
at org.glassfish.grizzly.impl.SafeFutureImpl$Sync.innerGet(SafeFutureImpl.java:354)
at org.glassfish.grizzly.impl.SafeFutureImpl.get(SafeFutureImpl.java:265)
at org.glassfish.grizzly.impl.SafeFutureImpl.notifyCompletionHandlers(SafeFutureImpl.java:181)
at org.glassfish.grizzly.impl.SafeFutureImpl.done(SafeFutureImpl.java:287)
at org.glassfish.grizzly.impl.SafeFutureImpl$Sync.innerSet(SafeFutureImpl.java:383)
at org.glassfish.grizzly.impl.SafeFutureImpl.result(SafeFutureImpl.java:112)
at org.glassfish.grizzly.http.server.HttpServer.shutdownNow(HttpServer.java:458)
at org.glassfish.grizzly.http.server.HttpServer$1.completed(HttpServer.java:384)
at org.glassfish.grizzly.http.server.HttpServer$1.completed(HttpServer.java:376)
at org.glassfish.grizzly.impl.SafeFutureImpl.notifyCompletionHandlers(SafeFutureImpl.java:199)
at org.glassfish.grizzly.impl.SafeFutureImpl.done(SafeFutureImpl.java:287)
at org.glassfish.grizzly.impl.SafeFutureImpl$Sync.innerSet(SafeFutureImpl.java:383)
at org.glassfish.grizzly.impl.SafeFutureImpl.result(SafeFutureImpl.java:112)
at org.glassfish.grizzly.http.server.NetworkListener$1$1.completed(NetworkListener.java:698)
at org.glassfish.grizzly.http.server.NetworkListener$1$1.completed(NetworkListener.java:693)
at org.glassfish.grizzly.http.server.HttpServerFilter.prepareForShutdown(HttpServerFilter.java:328)
at org.glassfish.grizzly.http.server.NetworkListener$1.shutdownRequested(NetworkListener.java:704)
at org.glassfish.grizzly.nio.GracefulShutdownRunner.run(GracefulShutdownRunner.java:93)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
我把它归结为这个不使用平针织物或关闭钩子的测试用例。它非常简单并且发生了同样的异常(实际上,上面是从该程序的输出中复制的):
public class Server {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.createSimpleServer();
server.start();
try { Thread.sleep(5000); } catch (InterruptedException ex) {}
shutdown(server);
}
public static void shutdown (HttpServer server) {
final boolean [] done = {false};
server.shutdown().addCompletionHandler(new EmptyCompletionHandler<HttpServer>() {
public void completed (HttpServer arg) {
System.out.println("Shutdown completed");
done[0] = true;
}
public void failed (Throwable error) {
System.out.println("Shutdown failed");
error.printStackTrace(System.out);
done[0] = true;
}
});
while (!done[0]) {
try { Thread.sleep(100); } catch (InterruptedException ex) {}
}
System.out.println("Goodbye");
}
}
哪里有错误?