2 changed files with 0 additions and 52 deletions
			
			
		@ -1,37 +0,0 @@ | 
				
			|||
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