随笔 - 43  文章 - 0  评论 - 1  阅读 - 80451

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; } }
复制代码

 

说明:

  1. stop(Runnable callback) ,结束应用后,会自己执行。
  2. stop() 不会自己执行,要手动调用。
  3. 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; } }
复制代码

 

posted on   wuyicode  阅读(97)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2019-11-22 记一次linux下安装mysql5.7
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示