参考 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; } }
标签:SmartLifecycle,stop,介绍,running,springframework,使用,Override,org,public From: https://www.cnblogs.com/wuyicode/p/17848427.html