|
@ -1,28 +1,26 @@ |
|
|
package com.epmet.commons.tools.config.shutdown; |
|
|
package com.epmet.commons.tools.config.shutdown; |
|
|
|
|
|
|
|
|
import org.apache.catalina.connector.Connector; |
|
|
import com.tongweb.container.connector.Connector; |
|
|
import org.slf4j.Logger; |
|
|
import com.tongweb.springboot.starter.TongWebConnectorCustomizer; |
|
|
import org.slf4j.LoggerFactory; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|
|
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; |
|
|
|
|
|
import org.springframework.context.ApplicationListener; |
|
|
import org.springframework.context.ApplicationListener; |
|
|
import org.springframework.context.event.ContextClosedEvent; |
|
|
import org.springframework.context.event.ContextClosedEvent; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
import java.util.concurrent.Executor; |
|
|
import java.util.concurrent.Executor; |
|
|
import java.util.concurrent.ThreadPoolExecutor; |
|
|
import java.util.concurrent.ThreadPoolExecutor; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@EnableConfigurationProperties(ShutdownProperties.class) |
|
|
@EnableConfigurationProperties(ShutdownProperties.class) |
|
|
@Component |
|
|
@Component |
|
|
@ConditionalOnProperty(prefix = "shutdown.graceful", name = "enable", havingValue = "true", matchIfMissing = false) |
|
|
@ConditionalOnProperty(prefix = "shutdown.graceful", name = "enable", havingValue = "true", matchIfMissing = false) |
|
|
public class GracefulShutdownTomcat implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> { |
|
|
public class GracefulShutdownTongWeb implements TongWebConnectorCustomizer, ApplicationListener<ContextClosedEvent> { |
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(GracefulShutdownTomcat.class); |
|
|
@Resource |
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private ShutdownProperties shutdownProperties; |
|
|
private ShutdownProperties shutdownProperties; |
|
|
|
|
|
|
|
|
private volatile Connector connector; |
|
|
private volatile Connector connector; |
|
@ -31,6 +29,7 @@ public class GracefulShutdownTomcat implements TomcatConnectorCustomizer, Applic |
|
|
public void customize(Connector connector) { |
|
|
public void customize(Connector connector) { |
|
|
this.connector = connector; |
|
|
this.connector = connector; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) { |
|
|
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) { |
|
|
this.connector.pause(); |
|
|
this.connector.pause(); |
|
@ -42,11 +41,11 @@ public class GracefulShutdownTomcat implements TomcatConnectorCustomizer, Applic |
|
|
threadPoolExecutor.shutdown(); |
|
|
threadPoolExecutor.shutdown(); |
|
|
if (!threadPoolExecutor.awaitTermination(waitTimeSecs, TimeUnit.SECONDS)) { |
|
|
if (!threadPoolExecutor.awaitTermination(waitTimeSecs, TimeUnit.SECONDS)) { |
|
|
String msg = String.format("Tomcat在【%s】秒内优雅停机失败,请手动处理", waitTimeSecs); |
|
|
String msg = String.format("Tomcat在【%s】秒内优雅停机失败,请手动处理", waitTimeSecs); |
|
|
logger.error(msg); |
|
|
log.error(msg); |
|
|
} |
|
|
} |
|
|
} catch (InterruptedException ex) { |
|
|
} catch (InterruptedException ex) { |
|
|
Thread.currentThread().interrupt(); |
|
|
Thread.currentThread().interrupt(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |