• 聊聊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。

  • 相关阅读:
    [LNOI2022] 吃(数学+贪心)
    数据库的基本操作(2)
    LeetCode 第 307 场周赛 复盘
    Android基础篇 Android 数据存储与性能
    C#常用多线程(线程同步,事件触发,信号量,互斥锁,共享内存,消息队列)
    点击化学 PEG 试剂:1802907-92-1,Mtz-PEG4-NHS,甲基四嗪-四聚乙醇-活性酯
    独立产品灵感周刊 DecoHack #026 - 在哪里推广你的新产品
    js将两张图片合并成一张图片
    Kamiya丨Kamiya艾美捷小鼠血清淀粉样蛋白A ELISA说明书
    mindspore 执行模型转换为310的mindir文件显示无LRN算子
  • 原文地址:https://blog.csdn.net/hello_ejb3/article/details/134300987