Spring Boot正常关机

Spring Boot正常关机,第1张

Spring Boot正常关机

我最终得到了:

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<ContextClosedEvent> {  private static final Logger log = LoggerFactory.getLogger(GracefulShutdown.class);  private volatile Connector connector;  @Override  public void customize(Connector connector) {    this.connector = connector;  }  @Override  public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {    log.info("Protocol handler is shutting down");    this.connector.pause();    Executor executor = this.connector.getProtocolHandler().getExecutor();    if (executor instanceof ThreadPoolExecutor) {      try {        ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;        threadPoolExecutor.shutdown();        if (!threadPoolExecutor.awaitTermination(30, TimeUnit.SECONDS))          log.warn("Tomcat thread pool did not shut down gracefully within 30 seconds. Proceeding with forceful shutdown");        else          log.info("Protocol handler shut down");      } catch (InterruptedException e) {        Thread.currentThread().interrupt();      }    }  }}

一些其他的bean:

import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;...  @Bean  public GracefulShutdown gracefulShutdown() {    return new GracefulShutdown();  }  @Bean  public EmbeddedServletContainerFactory servletContainer(final GracefulShutdown gracefulShutdown) {    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();    factory.addConnectorCustomizers(gracefulShutdown);    return factory;  }...


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5621746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存