no.skatteetaten.aurora.GracefulShutdown Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aurora-spring-boot-starter Show documentation
Show all versions of aurora-spring-boot-starter Show documentation
Starter for an aurora spring-boot app
The newest version!
package no.skatteetaten.aurora;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.catalina.connector.Connector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
public class GracefulShutdown implements TomcatConnectorCustomizer, ApplicationListener {
public static final int TIMEOUT_VALUE = 10;
private static Logger logger = LoggerFactory.getLogger(GracefulShutdown.class);
private Connector connector;
@Override
public void customize(Connector c) {
this.connector = c;
}
@Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
if (connector == null) {
// We have seen cases where connector is null in some unit test environments.
return;
}
this.connector.pause();
Executor executor = this.connector.getProtocolHandler().getExecutor();
if (!(executor instanceof ThreadPoolExecutor)) {
return;
}
ThreadPoolExecutor tpe = (ThreadPoolExecutor) executor;
try {
tpe.shutdown();
if (!tpe.awaitTermination(TIMEOUT_VALUE, TimeUnit.SECONDS)) {
logger.warn(
"Tomcat thread pool did not shut down gracefully within $shutdownTimeout $unit. Proceeding with "
+ "forceful shutdown");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}