优雅的添加程序结束清理逻辑

  今天在看一些老的开源代码的时候看到了一段没有见过的代码。

scheduledExecutor.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
        try {
            writeLog();
        } catch (Throwable e) {
            log.error("write log error:", e);
        }
    }
}, STATISTIC_PERIOD, STATISTIC_PERIOD, TimeUnit.SECONDS);
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
    @Override
    public void run() {
        if (!scheduledExecutor.isShutdown()) {
            scheduledExecutor.shutdown();
        }
    }
}));

主要是我没有搞懂addShutdownHook()方法。这段代码出现在加载配置和打印日志之后。
后来上网搜索一番之后大概了解了这个方法的功能。

jvm中增加一个关闭的钩子,当jvm关闭的时候,会执行系统中已经设置的所有通过方法addShutdownHook添加的钩子,当系统执行完这些钩子后,jvm才会关闭。所以这些钩子可以在jvm关闭的时候进行内存清理、对象销毁等操作。

我们的java程序运行在JVM上,有很多情况可能会突然崩溃掉,比如OOM、用户强制退出、业务其他报错。。。等一系列的问题可能导致我们的进程挂掉。如果我们的进程在运行一些很重要的内容,比如事务操作之类的,很有可能导致事务的不一致性问题。所以,实现应用的优雅关闭还是蛮重要的,起码我们可以在关闭之前做一些记录补救操作。

但是多数时候我作为一个后端开发多数时候我用Spring开发,我多数时候需要通过Spring的方法来处理我的程序,那么如何在Spring里面处理异常关闭的处理逻辑:
比如监听一些生命周期的事件:
以下内容均来自Spring官网
2.1. ContextRefreshedEvent

On either initializing or refreshing the ApplicationContext, Spring
raises the ContextRefreshedEvent. Typically a refresh can get
triggered multiple times as long as the context has not been closed.

Notice that, we can also have the event triggered manually by calling
the refresh() method on the ConfigurableApplicationContext interface.

2.2. ContextStartedEvent

By calling the start() method on the ConfigurableApplicationContext,
we trigger this event and start the ApplicationContext. As a matter of
fact, the method is typically used to restart beans after an explicit
stop. We can also use the method to deal components with no
configuration for autostart.

Here, it’s important to note that the call to start() is always
explicit as opposed to refresh().

2.3. ContextStoppedEvent

A ContextStoppedEvent is published when the ApplicationContext is
stopped, by invoking the stop() method on the
ConfigurableApplicationContext. As discussed earlier, we can restart a
stopped event by using start() method.

2.4. ContextClosedEvent

This event is published when the ApplicationContext is closed, using
the close() method in ConfigurableApplicationContext. In reality,
after closing a context, we cannot restart it.

A context reaches its end of life on closing it and hence we cannot
restart it like in a ContextStoppedEvent.

通过监听程序关闭事件可以监听到何时关闭程序,具体方法在官网中也给出了方法。
监听一个事件的方法:

@EventListener
public void handleContextRefreshEvent(ContextStartedEvent ctxStartEvt) {
    System.out.println("Context Start Event received.");
}

监听多个事件的方法:

@EventListener(classes = { ContextStartedEvent.class, ContextStoppedEvent.class })
public void handleMultipleEvents() {
    System.out.println("Multi-event listener invoked");
}

或者朴素的方法:

@Component
@Slf4j
public class ShutdownHookDemo implements ApplicationListener<ContextClosedEvent> {
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        log.info("shutdown hook, ContextClosedEvent");
    }
}

最后实现DisposableBean对于那些希望在Bean销毁时需要执行资源释放的Bean,可以实现该接口。对每个单独的Bean,BeanFactory会负责调用destory方法。

@Component
public class Bean2 implements DisposableBean {

    @Override
    public void destroy() throws Exception {
        System.out.println(
          "Callback triggered - DisposableBean.");
    }
}

在写到这里的时候,我发现在官网上还有很多方便的实现方式。比如可以在注册的Bean里直接用注解实现。好吧,算我菜了。还是得充电啊!!!

  • 13
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值