Spring Boot正常关机

我正在开发由嵌入式Tomcat支持的Spring Boot" title="Spring Boot">Spring Boot应用程序,并且需要按照以下步骤开发正常关机:

  1. 停止处理新的HTTP请求(停止Web容器)
  2. 处理所有已经接受的请求
  3. 关闭Spring ApplicationContext

*按顺序(依次)执行上述步骤

我该如何实现?

PS Spring Boot 1.5.20.RELEASE,Java 8

回答:

我最终得到了:

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;

}

...

以上是 Spring Boot正常关机 的全部内容, 来源链接: utcz.com/qa/399705.html

回到顶部