允许在飞行中完成tomcat / spring mvc容器关闭之前的请求

问题描述 投票:1回答:1

春季4.2.3在Tomcat 7上运行的Spring Mvc Web应用程序。

关闭tomcat时,我正在寻找一种执行以下操作的方法。

  • 停止接收任何new请求
  • 允许所有当前的运行中 http请求完成,然后spring / tomcat开始关闭所有服务等。

原因是,我有一个应用程序可以处理购买预付费产品的客户。 (通话时间,电费等。)。在关闭之前,必须允许所有进入我的应用程序的请求完成,以确保我的数据和事务本身的完整性。

[我知道我可以在弹跳豆上使用@PreDestroy作为挂钩,以在关机时执行操作。但是我需要一种适用于整个应用程序的方法,该方法可以在春季或容器甚至开始关闭我的服务之前就给我钩住。

我已经搜索了几个小时,但是找不到所需的内容?

spring-mvc tomcat shutdown application-shutdown
1个回答
0
投票
SpringBoot 1.5+  Provides an option to do the graceful shutdown of an application which helps to complete the processing of in flight requests before application shutdown. 

**Flow of an Graceful shutdown of SpringBoot app** :

 1. The JVM receives the SIGTERM signal and starts shutting down the Spring container.
 2. A Spring EventListener listens for a ContextClosedEvent and is invoked once the shutdown is started.
 3. The EventListener updates a Spring Boot HealthIndicator and puts it "out of service".
 4. The context shutdown is delayed using a Thread.sleep to allow the load balancer to see the updated HealthIndicator status and stop forwarding requests to this instance.
 5. When the Thread.sleep is finished, the Tomcat container is gracefully shutdown. First by pausing the connector, no longer accepting new request. Next, by allowing the Tomcat thread pool a configurable amount of time to finish the active threads.
 6. Finally, the Spring context is closed.

Helpful links :

https://github.com/timpeeters/spring-boot-graceful-shutdown
https://github.com/SchweizerischeBundesbahnen/springboot-graceful-shutdown
https://www.baeldung.com/spring-boot-graceful-shutdown
© www.soinside.com 2019 - 2024. All rights reserved.