• 聊聊logback的ShutdownHook


    本文主要研究一下logback的ShutdownHook

    ShutdownHook

    ch/qos/logback/core/hook/ShutdownHook.java

    /**
     * Interface describing a logback shutdown hook implementation
     * 
     * @author Mike Reinhold
     */
    public interface ShutdownHook extends Runnable, ContextAware {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    ShutdownHook接口继承了Runnable、ContextAware接口

    ShutdownHookBase

    ch/qos/logback/core/hook/ShutdownHookBase.java

    /**
     * Base class for classes implementing a Logback ShutdownHook via extension
     *
     * @author Mike Reinhold
     */
    public abstract class ShutdownHookBase extends ContextAwareBase implements ShutdownHook {
    
        public ShutdownHookBase() {
        }
    
        /**
         * Default method for stopping the Logback context
         */
        protected void stop() {
            addInfo("Logback context being closed via shutdown hook");
    
            Context hookContext = getContext();
            if (hookContext instanceof ContextBase) {
                ContextBase context = (ContextBase) hookContext;
                context.stop();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    ShutdownHookBase继承了ContextAwareBase,声明实现ShutdownHook,它提供了一个stop方法,用于关闭ContextBase

    DelayingShutdownHook

    ch/qos/logback/core/hook/DelayingShutdownHook.java

    /**
     * ShutdownHook implementation that stops the Logback context after a specified
     * delay.  The default delay is 0 ms (zero).
     *
     * @author Mike Reinhold
     */
    public class DelayingShutdownHook extends ShutdownHookBase {
        /**
         * The default is no delay before shutdown.
         */
        public static final Duration DEFAULT_DELAY = Duration.buildByMilliseconds(0);
    
        /**
         * The delay in milliseconds before the ShutdownHook stops the logback context
         */
        private Duration delay = DEFAULT_DELAY;
    
        public DelayingShutdownHook() {
        }
    
        public Duration getDelay() {
            return delay;
        }
    
        /**
         * The duration to wait before shutting down the current logback context.
         *
         * @param delay
         */
        public void setDelay(Duration delay) {
            this.delay = delay;
        }
    
        public void run() {
            addInfo("Sleeping for "+delay);
            try {
                Thread.sleep(delay.getMilliseconds());
            } catch (InterruptedException e) {
            }
            super.stop();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    DelayingShutdownHook继承了ShutdownHookBase,其run方法先sleep指定的delay,然后执行stop方法

    示例

    
    
        
            10
        
    
        
    
        
            
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    小结

    logback的ShutdownHook接口继承了Runnable、ContextAware接口,它有一个抽象类ShutdownHookBase提供了一个stop方法,用于关闭ContextBase,它的子类为DelayingShutdownHook,可以延迟指定时间再关闭ContextBase。

  • 相关阅读:
    【后端】Java学习笔记(二周目-1)
    《爱在 ZStack Cube 超融合》三部曲
    Jvm系列第一期——Java类加载机制
    [项目管理-5]:软硬件项目管理 - 项目人力资源管理 (人)
    win10无损升级到win11
    git设置代理
    【LeetCode】只出现一次的数字
    vue重修之Vuex【下部】
    java毕业设计——基于java+Jsoup+HttpClient的网络爬虫技术的网络新闻分析系统设计与实现——网络新闻分析系统
    【vue3】10-vue组件化额外知识补充(下)-动态组件-组件缓存-v-model在组件上的应用
  • 原文地址:https://blog.csdn.net/hello_ejb3/article/details/134300987