|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.Collections;
|
21 | 21 | import java.util.List;
|
| 22 | +import java.util.concurrent.CountDownLatch; |
22 | 23 |
|
23 | 24 | import org.apache.catalina.Container;
|
24 | 25 | import org.apache.catalina.Service;
|
@@ -51,32 +52,44 @@ final class GracefulShutdown {
|
51 | 52 |
|
52 | 53 | void shutDownGracefully(GracefulShutdownCallback callback) {
|
53 | 54 | logger.info("Commencing graceful shutdown. Waiting for active requests to complete");
|
54 |
| - new Thread(() -> doShutdown(callback), "tomcat-shutdown").start(); |
| 55 | + CountDownLatch shutdownUnderway = new CountDownLatch(1); |
| 56 | + new Thread(() -> doShutdown(callback, shutdownUnderway), "tomcat-shutdown").start(); |
| 57 | + try { |
| 58 | + shutdownUnderway.await(); |
| 59 | + } |
| 60 | + catch (InterruptedException ex) { |
| 61 | + Thread.currentThread().interrupt(); |
| 62 | + } |
55 | 63 | }
|
56 | 64 |
|
57 |
| - private void doShutdown(GracefulShutdownCallback callback) { |
58 |
| - List<Connector> connectors = getConnectors(); |
59 |
| - connectors.forEach(this::close); |
| 65 | + private void doShutdown(GracefulShutdownCallback callback, CountDownLatch shutdownUnderway) { |
60 | 66 | try {
|
61 |
| - for (Container host : this.tomcat.getEngine().findChildren()) { |
62 |
| - for (Container context : host.findChildren()) { |
63 |
| - while (!this.aborted && isActive(context)) { |
64 |
| - Thread.sleep(50); |
65 |
| - } |
66 |
| - if (this.aborted) { |
67 |
| - logger.info("Graceful shutdown aborted with one or more requests still active"); |
68 |
| - callback.shutdownComplete(GracefulShutdownResult.REQUESTS_ACTIVE); |
69 |
| - return; |
| 67 | + List<Connector> connectors = getConnectors(); |
| 68 | + connectors.forEach(this::close); |
| 69 | + shutdownUnderway.countDown(); |
| 70 | + try { |
| 71 | + for (Container host : this.tomcat.getEngine().findChildren()) { |
| 72 | + for (Container context : host.findChildren()) { |
| 73 | + while (!this.aborted && isActive(context)) { |
| 74 | + Thread.sleep(50); |
| 75 | + } |
| 76 | + if (this.aborted) { |
| 77 | + logger.info("Graceful shutdown aborted with one or more requests still active"); |
| 78 | + callback.shutdownComplete(GracefulShutdownResult.REQUESTS_ACTIVE); |
| 79 | + return; |
| 80 | + } |
70 | 81 | }
|
71 | 82 | }
|
72 | 83 | }
|
73 |
| - |
| 84 | + catch (InterruptedException ex) { |
| 85 | + Thread.currentThread().interrupt(); |
| 86 | + } |
| 87 | + logger.info("Graceful shutdown complete"); |
| 88 | + callback.shutdownComplete(GracefulShutdownResult.IDLE); |
74 | 89 | }
|
75 |
| - catch (InterruptedException ex) { |
76 |
| - Thread.currentThread().interrupt(); |
| 90 | + finally { |
| 91 | + shutdownUnderway.countDown(); |
77 | 92 | }
|
78 |
| - logger.info("Graceful shutdown complete"); |
79 |
| - callback.shutdownComplete(GracefulShutdownResult.IDLE); |
80 | 93 | }
|
81 | 94 |
|
82 | 95 | private List<Connector> getConnectors() {
|
|
0 commit comments