logback的ShutdownHook關(guān)閉原理解析
序
本文主要研究一下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 {
}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();
}
}
}ShutdownHookBase繼承了ContextAwareBase,聲明實(shí)現(xiàn)ShutdownHook,它提供了一個(gè)stop方法,用于關(guān)閉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();
}
}DelayingShutdownHook繼承了ShutdownHookBase,其run方法先sleep指定的delay,然后執(zhí)行stop方法
示例
<configuration debug="false">
<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook">
<delay>10</delay>
</shutdownHook>
<appender name="A" class="ch.qos.logback.core.read.ListAppender"/>
<root level="DEBUG">
<appender-ref ref="A" />
</root>
</configuration>小結(jié)
logback的ShutdownHook接口繼承了Runnable、ContextAware接口,它有一個(gè)抽象類ShutdownHookBase提供了一個(gè)stop方法,用于關(guān)閉ContextBase,它的子類為DelayingShutdownHook,可以延遲指定時(shí)間再關(guān)閉ContextBase。
以上就是logback的ShutdownHook的詳細(xì)內(nèi)容,更多關(guān)于logback ShutdownHook的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之隊(duì)列
這篇文章主要為大家介紹了Java數(shù)據(jù)結(jié)構(gòu)和算法之隊(duì)列,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01
SpringBoot中使用MQTT實(shí)現(xiàn)消息的訂閱和發(fā)布(示例代碼)
這篇文章主要介紹了SpringBoot中使用MQTT實(shí)現(xiàn)消息的訂閱和發(fā)布的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
MyBatis的CRUD中的不同參數(shù)綁定查詢實(shí)現(xiàn)
本文主要介紹了MyBatis的CRUD中的不同參數(shù)綁定查詢實(shí)現(xiàn),主要包括單個(gè)參數(shù)傳遞綁定,序號參數(shù)傳遞綁定,注解參數(shù)傳遞綁定,pojo(對象)參數(shù)傳遞綁定,map參數(shù)傳遞綁定這幾種類型,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
JAVA多線程之實(shí)現(xiàn)用戶任務(wù)排隊(duì)并預(yù)估排隊(duì)時(shí)長
本文主要介紹了Java多線程之實(shí)現(xiàn)用戶任務(wù)排隊(duì)并預(yù)估排隊(duì)時(shí)長的問題,文中的代碼具有一定的學(xué)習(xí)和工作價(jià)值,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2021-12-12
Java?SpringBoot項(xiàng)目如何優(yōu)雅的實(shí)現(xiàn)操作日志記錄
這篇文章主要介紹了Java?SpringBoot項(xiàng)目如何優(yōu)雅的實(shí)現(xiàn)操作日志記錄,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
Java中Lambda表達(dá)式使用場景實(shí)例詳解
本文給大家介紹Java中Lambda表達(dá)式使用及詳解,講解Lambda表達(dá)式的概念、語法及其應(yīng)用場景,并探討如何利用函數(shù)引用進(jìn)一步提高代碼效率,感興趣的朋友一起看看吧2025-08-08

