|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
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,45 @@ 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 (isActive(context)) { |
64 |
| - if (this.aborted) { |
65 |
| - logger.info("Graceful shutdown aborted with one or more requests still active"); |
66 |
| - callback.shutdownComplete(GracefulShutdownResult.REQUESTS_ACTIVE); |
67 |
| - 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 (isActive(context)) { |
| 74 | + if (this.aborted) { |
| 75 | + logger.info("Graceful shutdown aborted with one or more requests still active"); |
| 76 | + callback.shutdownComplete(GracefulShutdownResult.REQUESTS_ACTIVE); |
| 77 | + return; |
| 78 | + } |
| 79 | + Thread.sleep(50); |
68 | 80 | }
|
69 |
| - Thread.sleep(50); |
70 | 81 | }
|
71 | 82 | }
|
72 |
| - } |
73 | 83 |
|
| 84 | + } |
| 85 | + catch (InterruptedException ex) { |
| 86 | + Thread.currentThread().interrupt(); |
| 87 | + } |
| 88 | + logger.info("Graceful shutdown complete"); |
| 89 | + callback.shutdownComplete(GracefulShutdownResult.IDLE); |
74 | 90 | }
|
75 |
| - catch (InterruptedException ex) { |
76 |
| - Thread.currentThread().interrupt(); |
| 91 | + finally { |
| 92 | + shutdownUnderway.countDown(); |
77 | 93 | }
|
78 |
| - logger.info("Graceful shutdown complete"); |
79 |
| - callback.shutdownComplete(GracefulShutdownResult.IDLE); |
80 | 94 | }
|
81 | 95 |
|
82 | 96 | private List<Connector> getConnectors() {
|
|
0 commit comments