日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java钩子函数(hook)以spring源码为例

發(fā)布時間:2025/3/15 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java钩子函数(hook)以spring源码为例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、什么是鉤子函數(shù)

請問在Spring中,如果JVM異常終止,Spring是如何保證會釋放掉占用的資源,比如說數(shù)據(jù)庫連接等資源呢?鉤子函數(shù)非常簡單,簡單到只用摘抄一段Spring代碼即可。

二、問題

Spring 容器中 Bean 在什么時候執(zhí)行銷毀方法?我們知道在Spring中定義銷毀方法有兩種方式實現(xiàn)DisposableBean 的 destroy 方法。 使用 @PreDestroy 注解修飾方法

@Component public class DataCollectBean implements DisposableBean {/*** 第一種方法實現(xiàn) DisposableBean#destroy方法** @throws Exception 異常*/@Overridepublic void destroy() throws Exception {System.err.println("執(zhí)行銷毀方法");}/*** 第二種方法使用PreDestroy注解聲明銷毀方法*/@PreDestroypublic void customerDestroy() {System.err.println("執(zhí)行自定義銷毀方法");} }

三、那么在什么時候執(zhí)行銷毀方法?

  • 主動執(zhí)行銷毀bean
  • public static void main(String[] args) {ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);DataCollectBean bean = run.getBean(DataCollectBean.class);//1. 主動銷毀beanrun.getBeanFactory().destroyBean(bean); }
  • JVM關閉時候自動執(zhí)行銷毀方法。
    這里就要用到鉤子函數(shù)了, Spring 的鉤子函數(shù)在 AbstractApplicationContext#shutdownHook屬性,如果我們是SpringBoot項目我們看到在SpringApplication啟動時候會注冊一個鉤子函數(shù)
  • 四、如何定義鉤子函數(shù)?

    簡直太簡單了,沒有任何學習成本。一行代碼就能搞定。

    public class HooksTester {public static void main(String[] args) {//定義鉤子函數(shù)Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {@Overridepublic void run() {System.out.println("鉤子函數(shù)執(zhí)行");}}));//當主動關閉應用while (true);} }

    五、觸發(fā)鉤子函數(shù)的場景

    只要不是機器斷電,強制kill -9 強制殺進程,都會觸發(fā)。

    六、鉤子函數(shù)能做什么?

    正如上圖所示優(yōu)雅停機,在項目將要關閉時候,主動釋放程序占用的資源信息,釋放db連接池的連接等其他占用的資源信息。如果我們是 Spring 項目其實我們不用自己定義鉤子函數(shù),我們只要使用Spring提供的銷毀方法即可。因為Spring定義的鉤子函數(shù)中會執(zhí)行DisposableBean.destory() 和被 PreDestroy 修飾的方法。

    • 源碼
    protected void doClose() {// Check whether an actual close attempt is necessary...if (this.active.get() && this.closed.compareAndSet(false, true)) {if (logger.isDebugEnabled()) {logger.debug("Closing " + this);}LiveBeansView.unregisterApplicationContext(this);try {// Publish shutdown event.publishEvent(new ContextClosedEvent(this));}catch (Throwable ex) {logger.warn("Exception thrown from ApplicationListener handling ContextClosedEvent", ex);}// Stop all Lifecycle beans, to avoid delays during individual destruction.if (this.lifecycleProcessor != null) {try {this.lifecycleProcessor.onClose();}catch (Throwable ex) {logger.warn("Exception thrown from LifecycleProcessor on context close", ex);}}// Destroy all cached singletons in the context's BeanFactory.destroyBeans();// Close the state of this context itself.closeBeanFactory();// Let subclasses do some final clean-up if they wish...onClose();// Reset local application listeners to pre-refresh state.if (this.earlyApplicationListeners != null) {this.applicationListeners.clear();this.applicationListeners.addAll(this.earlyApplicationListeners);}// Switch to inactive.this.active.set(false);}}

    可以看到:doClose()方法會執(zhí)行bean的destroy(),也會執(zhí)行SmartLifeCycle的stop()方法,我們就可以通過重寫這些方法來實現(xiàn)對象的關閉,生命周期的管理,實現(xiàn)平滑shutdown

    文章轉自

    總結

    以上是生活随笔為你收集整理的java钩子函数(hook)以spring源码为例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。