SmartLifecycle 使用介绍
参考 https://blog.csdn.net/feiying0canglang/article/details/127156520
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;
@Component
public class MySmartLifecycle implements SmartLifecycle {
private volatile boolean running = false;
@Override
public boolean isAutoStartup() {
// 返回true 才能自动执行 start() 方法
return true;
}
@Override
public void stop(Runnable callback) {
// 结束应用后会调用
System.out.println("stop(callback)");
// stop();
callback.run();
}
@Override
public void start() {
System.out.println("start()");
running = true;
}
@Override
public void stop() {
System.out.println("stop()");
running = false;
}
@Override
public boolean isRunning() {
System.out.println("isRunning " + running);
return running;
}
@Override
public int getPhase() {
// 数字越小,执行顺序越靠前
return 0;
}
}
说明:
- stop(Runnable callback) ,结束应用后,会自己执行。
- stop() 不会自己执行,要手动调用。
- isAutoStartup() 返回 true,才能执行 start() 。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.DefaultLifecycleProcessor;
@Configuration
public class MySmartLifecycleConfig {
// 执行异步 stop(Runnable runnable)关闭的 超时 方法
@Bean
public DefaultLifecycleProcessor defaultLifecycleProcessor(){
// 默认是30秒 private volatile long timeoutPerShutdownPhase = 30000L;
DefaultLifecycleProcessor defaultLifecycleProcessor = new DefaultLifecycleProcessor();
defaultLifecycleProcessor.setTimeoutPerShutdownPhase( 30000L);
return defaultLifecycleProcessor;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2019-11-22 记一次linux下安装mysql5.7