java基礎之ShutdownHook

原文鏈接:https://blog.csdn.net/wins22237/article/details/72758644

一、什麼是ShutdownHook?

在Java程序中可以通過添加關閉鉤子,實現在程序退出時關閉資源、平滑退出的功能。
使用Runtime.addShutdownHook(Thread hook)方法,可以註冊一個JVM關閉的鉤子。
這個鉤子可以在以下幾種場景被調用

  • 程序正常退出
  • 使用System.exit()
  • 終端使用Ctrl+C觸發的中斷
  • 系統關閉
  • 使用Kill pid命令幹掉進程

Runtime中的源碼

public void addShutdownHook(Thread hook) {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkPermission(new RuntimePermission("shutdownHooks"));
        }
        ApplicationShutdownHooks.add(hook);
} 
public boolean removeShutdownHook(Thread hook) {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            sm.checkPermission(new RuntimePermission("shutdownHooks"));
        }
        return ApplicationShutdownHooks.remove(hook);
}

ApplicationShutdownHooks

class ApplicationShutdownHooks {
    /* The set of registered hooks */
    private static IdentityHashMap<Thread, Thread> hooks;
    static {
        try {
            Shutdown.add(1 /* shutdown hook invocation order */,
                false /* not registered if shutdown in progress */,
                new Runnable() {
                    public void run() {
                        runHooks();
                    }
                }
            );
            hooks = new IdentityHashMap<>();
        } catch (IllegalStateException e) {
            // application shutdown hooks cannot be added if
            // shutdown is in progress.
            hooks = null;
        }
    }


    private ApplicationShutdownHooks() {}

    /* Add a new shutdown hook.  Checks the shutdown state and the hook itself,
     * but does not do any security checks.
     */
    static synchronized void add(Thread hook) {
        if(hooks == null)
            throw new IllegalStateException("Shutdown in progress");

        if (hook.isAlive())
            throw new IllegalArgumentException("Hook already running");

        if (hooks.containsKey(hook))
            throw new IllegalArgumentException("Hook previously registered");

        hooks.put(hook, hook);
    }

    /* Remove a previously-registered hook.  Like the add method, this method
     * does not do any security checks.
     */
    static synchronized boolean remove(Thread hook) {
        if(hooks == null)
            throw new IllegalStateException("Shutdown in progress");

        if (hook == null)
            throw new NullPointerException();

        return hooks.remove(hook) != null;
    }

    /* Iterates over all application hooks creating a new thread for each
     * to run in. Hooks are run concurrently and this method waits for
     * them to finish.
     */
    static void runHooks() {
        Collection<Thread> threads;
        synchronized(ApplicationShutdownHooks.class) {
            threads = hooks.keySet();
            hooks = null;
        }

        for (Thread hook : threads) {
            hook.start();
        }
        for (Thread hook : threads) {
            try {
                hook.join();
            } catch (InterruptedException x) { }
        }
    }
}

二、java進程平滑退出的意義

很多時候,我們會有這樣的一些場景,比如說nginx反向代理若干個負載均衡的web容器,又或者微服務架構中存在的若干個服務節點,需要進行無間斷的升級發佈。
在重啓服務的時候,除非我們去變更nginx的配置,否則重啓很可能會導致正在執行的線程突然中斷,本來應該要完成的事情只完成了一半,並且調用方出現錯誤警告。
如果能有一種簡單的方式,能夠讓進程在退出時能執行完當前正在執行的任務,並且讓服務的調用方將新的請求定向到其他負載節點,這將會很有意義。
自己註冊ShutdownHook可以幫助我們實現java進程的平滑退出。

三、java進程平滑退出的思路

  1. 在服務啓動時註冊自己的ShutdownHook
  2. ShutdownHook在被運行時,首先不接收新的請求,或者告訴調用方重定向到其他節點
  3. 等待當前的執行線程運行完畢,如果五秒後仍在運行,則強制退出

四、實現服務的平滑退出

4.1 Http請求

4.2 dubbo請求

嘗試了許多次,看了相關的源碼,dubbo不支持平滑退出;解決方法只有一個,那就是修改dubbo的源碼,以下兩個地址有詳細介紹:
http://frankfan915.iteye.com/blog/2254097
https://my.oschina.net/u/1398931/blog/790709

4.3 RabbitMQ消費

以下是SpringBoot的示例,不使用Spring原理也是一樣的

RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry = applicationContext.getBean(
        RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME,
        RabbitListenerEndpointRegistry.class);
Collection<MessageListenerContainer> containers = rabbitListenerEndpointRegistry.getListenerContainers();
for (MessageListenerContainer messageListenerContainer : containers) {
    messageListenerContainer.stop();
}

五、爲何重啓時有時會有ClassNotFoundException

springboot通過java -jar example.jar的方式啓動項目,在使用腳本restart的時候,首先覆蓋舊的jar包,然後stop舊線程,啓動新線程,這樣就可能會出現此問題。因爲在stop的時候,ShutdownHook線程被喚醒,在其執行過程中,某些類(尤其是匿名類)還未加載,這時候就會通知ClassLoader去加載;ClassLoader持有的是舊jar包的文件句柄,雖然新舊jar包的名字路徑完全一樣,但是ClassLoader仍然是使用open着的舊jar包文件,文件已經找不到了,所以類加載不了就ClassNotFound了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章