2 changed files with 55 additions and 4 deletions
@ -0,0 +1,37 @@ |
|||
package com.epmet.config; |
|||
|
|||
import org.apache.catalina.connector.Connector; |
|||
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; |
|||
import org.springframework.context.ApplicationListener; |
|||
import org.springframework.context.event.ContextClosedEvent; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.concurrent.Executor; |
|||
import java.util.concurrent.ThreadPoolExecutor; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
@Component |
|||
public class GracefulShutdownTomcat implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> { |
|||
private volatile Connector connector; |
|||
private final int waitTime = 30; |
|||
@Override |
|||
public void customize(Connector connector) { |
|||
this.connector = connector; |
|||
} |
|||
@Override |
|||
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) { |
|||
this.connector.pause(); |
|||
Executor executor = this.connector.getProtocolHandler().getExecutor(); |
|||
if (executor instanceof ThreadPoolExecutor) { |
|||
try { |
|||
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; |
|||
threadPoolExecutor.shutdown(); |
|||
if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) { |
|||
System.out.println("Tomcat thread pool did not shut down gracefully within " + waitTime + " seconds. Proceeding with forceful shutdown"); |
|||
} |
|||
} catch (InterruptedException ex) { |
|||
Thread.currentThread().interrupt(); |
|||
} |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue