springboot SmartLifeCycle说明


springboot SmartLifeCycle说明

 

Application: Perform custom operations after the spring ioc container is created and before it is closed

 

******************************

Related classes and interfaces

 

LifeCycle

public interface Lifecycle {
    void start();   //容器启动后调用

    void stop();    //容器关闭前调用

    boolean isRunning();  //当前应用是否正在运行
}

 

SmartLifeCycle

public interface SmartLifecycle extends Lifecycle, Phased {
    int DEFAULT_PHASE = 2147483647;

    default boolean isAutoStartup() {  //自动调用start()、stop()方法,默认自动调用
        return true;
    }

    default void stop(Runnable callback) {
        this.stop();    //调用stop()方法
        callback.run(); //如果不调用该方法,等待30s关闭容器;如果调用了该方法,不需要等待就可关闭容器
    }

    default int getPhase() {  //如果有多个继承了SmartLifeCycle接口的类,返回值小的start()方法先调用,stop()方法相反
        return 2147483647;
    }
}

 

DefaultLifecycleProcessor

public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactoryAware {
    private final Log logger = LogFactory.getLog(this.getClass());
    private volatile long timeoutPerShutdownPhase = 30000L;
    private volatile boolean running;
    @Nullable
    private volatile ConfigurableListableBeanFactory beanFactory;

***************
构造方法

    public DefaultLifecycleProcessor() {
    }

***************
相关方法

    public void setTimeoutPerShutdownPhase(long timeoutPerShutdownPhase) {
        this.timeoutPerShutdownPhase = timeoutPerShutdownPhase;
    }//设置容器关闭时等待时间,默认为30s

 

 

******************************

Example: stop (Runnable callback) uses the default method, call callback.run ()

 

CustomSamrtLifeCycle

@Component
public class CustomSmartLifeCycle implements SmartLifecycle {

    private boolean isRunning=false;

    public void setRunning(boolean running) {
        isRunning = running;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public void start() {
        System.out.println("开始执行:"+LocalDateTime.now());
        this.setRunning(true);
    }

    @Override
    public void stop() {
        System.out.println("stop:"+ LocalDateTime.now());
        this.setRunning(false);
    }
}

 

*************************

Console output

 

      

Note: ctrl + c, the application is closed immediately

 

 

******************************

Example: stop (Runnable callback) does not call callback.run ()

 

CustomSmartLifeCycle

@Component
public class CustomSmartLifeCycle implements SmartLifecycle {

    private boolean isRunning=false;

    public void setRunning(boolean running) {
        isRunning = running;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public void start() {
        System.out.println("开始执行:"+LocalDateTime.now());
        this.setRunning(true);
    }

    @Override
    public void stop() {
        System.out.println("stop:"+ LocalDateTime.now());
        this.setRunning(false);
    }

    @Override
    public void stop(Runnable callback) {
        this.stop();
    }
}

 

*************************

Console output

 

      

Note: ctrl + c, the application is closed after 30s

 

 

******************************

Example: Set the default shutdown time

 

CustomSmartLifeCycle

@Component
public class CustomSmartLifeCycle implements SmartLifecycle {

    private boolean isRunning=false;

    public void setRunning(boolean running) {
        isRunning = running;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public void start() {
        System.out.println("开始执行:"+LocalDateTime.now());
        this.setRunning(true);
    }

    @Override
    public void stop() {
        System.out.println("stop:"+ LocalDateTime.now());
        this.setRunning(false);
    }

    @Override
    public void stop(Runnable callback) {
        this.stop();
    }
}

 

WebConfig

@Configuration
public class WebConfig {

    @Bean("lifecycleProcessor")  //需指定bean的id:lifecycleProcessor
    public DefaultLifecycleProcessor initDefaultLifeCycleProcessor(){
        DefaultLifecycleProcessor defaultLifecycleProcessor=new DefaultLifecycleProcessor();
        defaultLifecycleProcessor.setTimeoutPerShutdownPhase(20000L);

        return defaultLifecycleProcessor;
    }
}

 

*************************

Console output

 

     

Note: The default shutdown time is set to 20s

 

 

Published 387 original articles · Like 98 · Visits 30,000+

Guess you like

Origin blog.csdn.net/weixin_43931625/article/details/104971503
Recommended