parent
1c5858e59f
commit
2581c5c87a
|
@ -13,6 +13,6 @@ kotlinVersion=1.8.22
|
|||
mavenVersion=3.9.4
|
||||
nativeBuildToolsVersion=0.9.28
|
||||
springFrameworkVersion=6.0.17
|
||||
tomcatVersion=10.1.18
|
||||
tomcatVersion=10.1.19
|
||||
|
||||
kotlin.stdlib.default.dependency=false
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -19,6 +19,7 @@ package org.springframework.boot.web.embedded.tomcat;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import org.apache.catalina.Container;
|
||||
import org.apache.catalina.Service;
|
||||
|
@ -51,32 +52,45 @@ final class GracefulShutdown {
|
|||
|
||||
void shutDownGracefully(GracefulShutdownCallback callback) {
|
||||
logger.info("Commencing graceful shutdown. Waiting for active requests to complete");
|
||||
new Thread(() -> doShutdown(callback), "tomcat-shutdown").start();
|
||||
}
|
||||
|
||||
private void doShutdown(GracefulShutdownCallback callback) {
|
||||
List<Connector> connectors = getConnectors();
|
||||
connectors.forEach(this::close);
|
||||
CountDownLatch shutdownUnderway = new CountDownLatch(1);
|
||||
new Thread(() -> doShutdown(callback, shutdownUnderway), "tomcat-shutdown").start();
|
||||
try {
|
||||
for (Container host : this.tomcat.getEngine().findChildren()) {
|
||||
for (Container context : host.findChildren()) {
|
||||
while (isActive(context)) {
|
||||
if (this.aborted) {
|
||||
logger.info("Graceful shutdown aborted with one or more requests still active");
|
||||
callback.shutdownComplete(GracefulShutdownResult.REQUESTS_ACTIVE);
|
||||
return;
|
||||
}
|
||||
Thread.sleep(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shutdownUnderway.await();
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
logger.info("Graceful shutdown complete");
|
||||
callback.shutdownComplete(GracefulShutdownResult.IDLE);
|
||||
}
|
||||
|
||||
private void doShutdown(GracefulShutdownCallback callback, CountDownLatch shutdownUnderway) {
|
||||
try {
|
||||
List<Connector> connectors = getConnectors();
|
||||
connectors.forEach(this::close);
|
||||
shutdownUnderway.countDown();
|
||||
try {
|
||||
for (Container host : this.tomcat.getEngine().findChildren()) {
|
||||
for (Container context : host.findChildren()) {
|
||||
while (isActive(context)) {
|
||||
if (this.aborted) {
|
||||
logger.info("Graceful shutdown aborted with one or more requests still active");
|
||||
callback.shutdownComplete(GracefulShutdownResult.REQUESTS_ACTIVE);
|
||||
return;
|
||||
}
|
||||
Thread.sleep(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
logger.info("Graceful shutdown complete");
|
||||
callback.shutdownComplete(GracefulShutdownResult.IDLE);
|
||||
}
|
||||
finally {
|
||||
shutdownUnderway.countDown();
|
||||
}
|
||||
}
|
||||
|
||||
private List<Connector> getConnectors() {
|
||||
|
|
Loading…
Reference in New Issue