public class ShutdownAction implements ApplicationListener<ContextClosedEvent> { private ClassWithExecutor classWithExecutor; public ShutdownAction(ClassWithExecutor classWithExecutor) { this.classWithExecutor = classWithExecutor; } @Override public void onApplicationEvent(ContextClosedEvent contextClosedEvent) ...
1、springboot版本2.1.3 2、pom.xml加了 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 3、application.properties加了 management.endpoints.shutdown.enabled=true management.endpoints.shutdown.sensitive=false management.endpoints....
在你的Spring Boot项目中,你需要创建一个新的Java类来实现ApplicationListener接口。这个接口用于监听Spring应用的生命周期事件。 importorg.springframework.context.ApplicationListener;importorg.springframework.context.event.ContextClosedEvent;importorg.springframework.stereotype.Component;@ComponentpublicclassShutdownListener...
在springboot 正常情况下 使用 shutdown 关闭进程 是正常的,但是如果 jar包 正在运行线程任务时如果调用 shutdown 进行关闭 则会 抛出io异常并且 进程不会正常退出 解决方法: 创建ShutdownAction类 package com.ecolor.energy.config; import lombok.extern.slf4j.Slf4j; import org.springframework.context.Applicatio...
详见正文:SpringBoot生命周期事件——BAT的乌托邦 正文 本文将以SpringApplication的启动流程/生命周期各时期发出的Event事件为主线,结合每个生命周期内完成的大事记介绍,真正实现一文让你总览Spring Boot的全貌,这对你深入理解Spring Boot,以及整合进Spring Cloud都将非常重要。
在Spring Boot 2.3.0中,优雅停机非常容易实现,并且可以通过在应用程序配置文件中设置两个属性来进行管理。 server.shutdown:此属性可以支持的值有 immediate:这是默认值,将导致服务器立即关闭。 graceful:启用优雅停机,并遵守spring.lifecycle.timeout-per-shutdown-phase属性中给出的超时。
我们项目分两类应用,一种是基于Spring Boot的Web项目,涉及Servlet容器,一种是基于Spring Cloud Stream的Consumer应用,来监听消息队列处理event。下面我针对这两类应用,添加各自的gracefully shutdown机制。 版本说明 Spring Boot 2.0.3.RELEASE Spring Cloud Stream 2.0.0.RELEASE ...
Event注解方式(Spring4.2后版本) 基本使用 事件三件套: publisher , event , listener。 publisher Publisher由下面接口的实现类来承担,在springboot中ApplicationContext继承了这个接口,具体的实现类为AnnotationConfigServletWebServerApplicationContext, 但是这都不重要,重要的是我们只要声明注入这个接口的实现类Springboot就...
在Spring Boot 2.3.0中,优雅停机非常容易实现,并且可以通过在应用程序配置文件中设置两个属性来进行管理。 server.shutdown:此属性可以支持的值有 immediate:这是默认值,将导致服务器立即关闭。 graceful:启用优雅停机,并遵守spring.lifecycle.timeout-per-shutdown-phase属性中给出的超时。
1、JVM自带的shutdownHook Runtime.getRuntime().addShutdownHook(newThread(()->log.info("shutdown hook, jvm demo"))); 特点: jvm自带,使用方便,多个钩子间是并行执行的。 2、监听Spring的ContextClosedEvent 关于ContextClosedEvent等事件描述,可以参照以下示例(内容来自Spring官网) ...