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)文章
springcloud連接遠(yuǎn)程nacos失敗顯示localhost服務(wù)連接失敗的問題解決
這篇文章主要介紹了springcloud連接遠(yuǎn)程nacos失敗顯示localhost服務(wù)連接失敗的問題解決,文中有詳細(xì)的代碼示例供大家參考,對大家解決問題有一定的幫助,需要的朋友可以參考下2024-03-03
IDEA2022.1創(chuàng)建maven項(xiàng)目規(guī)避idea2022新建maven項(xiàng)目卡死無反應(yīng)問題
這篇文章主要介紹了IDEA2022.1創(chuàng)建maven項(xiàng)目規(guī)避idea2022新建maven項(xiàng)目卡死無反應(yīng)問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
idea項(xiàng)目中target文件提示拒絕訪問的解決
這篇文章主要介紹了idea項(xiàng)目中target文件提示拒絕訪問的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringMVC JSON數(shù)據(jù)傳輸參數(shù)超詳細(xì)講解
有時(shí)候參數(shù)的傳遞還需要更多的參數(shù),比如一個(gè)獲取用戶信息的請求中既有用戶ID等基本參數(shù),還要求對查詢結(jié)果進(jìn)行分頁,針對這種場景,一般都會(huì)將分頁參數(shù)封裝成一個(gè)對象,然后將它和基本參數(shù)一起傳給控制器2023-02-02
window?下?win10?jdk8安裝與環(huán)境變量的配置過程
這篇文章主要介紹了window?下?win10?jdk8安裝與環(huán)境變量的配置,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Spring Boot + Thymeleaf + Activiti 快速開發(fā)平臺項(xiàng)目 附源碼
這篇文章主要介紹了Spring Boot + Thymeleaf + Activiti 快速開發(fā)平臺項(xiàng)目附源碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
關(guān)于spring循環(huán)依賴問題及解決方案
這篇文章主要介紹了關(guān)于spring循環(huán)依賴問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Elasticsearch?Analyzer?內(nèi)置分詞器使用示例詳解
這篇文章主要為大家介紹了Elasticsearch?Analyzer?內(nèi)置分詞器使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

