您好,登錄后才能下訂單哦!
這篇文章給大家介紹springboot2.0.6中觀察器和啟動的錯報告以及Headless模式相關(guān)分析是怎樣的,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
public ConfigurableApplicationContext run(String... args) { // 構(gòu)造一個任務(wù)執(zhí)行觀察器(Java 查看時間和性能的類) StopWatch stopWatch = new StopWatch(); // 開始執(zhí)行,記錄開始時間 stopWatch.start(); // 定制配置上下文,頂級父類包含Bean Factory ConfigurableApplicationContext context = null; // 定義一個錯誤集合(用來支持報告關(guān)于啟動的錯) Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); //定義一個 awt 的headless(設(shè)置headless模式,名字為 java.awt.headless 的系統(tǒng)屬性設(shè)置為true) configureHeadlessProperty(); ......//省略 }
其內(nèi)部包含一個 list(代表任務(wù)的個數(shù)),當(dāng)開始調(diào)用 start 方法后,然后調(diào)用 stop,都會記錄當(dāng)前時間和 start 時間的耗時,然后封裝成一個任務(wù)對象加入到 StopWatch 內(nèi)部的list中,其中 start 和 stop 方法必須成對出現(xiàn)。
執(zhí)行 getSpringFactoriesInstances 方法從配置文件中獲取具體的實現(xiàn)類,當(dāng) springboot 啟動工程出現(xiàn)異常,就會調(diào)用 handleRunFailure 方法,具體如下:
private void handleRunFailure(ConfigurableApplicationContext context, Throwable exception, Collection<SpringBootExceptionReporter> exceptionReporters, SpringApplicationRunListeners listeners) { try { try { handleExitCode(context, exception); if (listeners != null) { listeners.failed(context, exception); } } finally { reportFailure(exceptionReporters, exception); if (context != null) { context.close(); } } } catch (Exception ex) { logger.warn("Unable to close ApplicationContext", ex); } ReflectionUtils.rethrowRuntimeException(exception); }
首先看handleExitCode(context, exception)
private void handleExitCode(ConfigurableApplicationContext context, Throwable exception) { int exitCode = getExitCodeFromException(context, exception); if (exitCode != 0) { if (context != null) { context.publishEvent(new ExitCodeEvent(context, exitCode)); } SpringBootExceptionHandler handler = getSpringBootExceptionHandler(); if (handler != null) { handler.registerExitCode(exitCode); } } }
上述代碼的意思就是從異常中獲取退出狀態(tài)碼,如果退出狀態(tài)碼不等于0,就通過 ConfigurableApplicationContext 發(fā)布退出事件,在 SpringBootExceptionHandler 中注冊下該狀態(tài)碼。
下面主要看 reportFailure(exceptionReporters, exception);
private void reportFailure(Collection<SpringBootExceptionReporter> exceptionReporters, Throwable failure) { try { for (SpringBootExceptionReporter reporter : exceptionReporters) { if (reporter.reportException(failure)) { registerLoggedException(failure); return; } } } catch (Throwable ex) { // Continue with normal handling of the original failure } if (logger.isErrorEnabled()) { logger.error("Application run failed", failure); registerLoggedException(failure); } }
上述代碼主要是依次調(diào)用 SpringBootExceptionReporter的reportException 方法(該方法主要是調(diào)用 analyzer 的集合去分析異常,如果有 analyzer 能分析成功,就把異常打印出來) 并把該異常注冊到 SpringBootExceptionHandler中 。
Headless模式是在缺少顯示屏、鍵盤或者鼠標(biāo)是的系統(tǒng)配置。在 java.awt.toolkit 和 java.awt.graphicsenvironment 類中有許多方法,除了對字體、圖形和打印的操作外還可以調(diào)用顯示器、鍵盤和鼠標(biāo)的方法。但是有一些類中,比如 Canvas 和 Panel,可以在 headless 模式下執(zhí)行
private void configureHeadlessProperty() { // 此處調(diào)用的是:java.awt.headless // 不提供外部設(shè)備的情況,自行運算。 // this.headless 默認為true System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, System.getProperty( SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless))); } public final class System { public static String setProperty(String key, String value) { // 判斷空操作 checkKey(key); // 獲取安全管理員 默認null SecurityManager sm = getSecurityManager(); if (sm != null) { sm.checkPermission(new PropertyPermission(key, SecurityConstants.PROPERTY_WRITE_ACTION)); } // 返回 return (String) props.setProperty(key, value); } }
系統(tǒng)屬性配置
為了啟用 headless 模式,需要使用 setProperty 方法去設(shè)置相應(yīng)的系統(tǒng)屬性。
System.setProperty("java.awt.headless","true")
如果想在一個相同的程序 中使用 headless 和傳統(tǒng)環(huán)境,你可以使用下面的命令行來完成:java -Djava.awt.headless=true
通過反射設(shè)置 java.awt.GraphicsEnvironment 中這個屬性的值為true
如果名字為 java.awt.headless 的系統(tǒng)屬性被設(shè)置true,那么 headless 工具包就會被使用。應(yīng)用程序可以執(zhí)行如下操作:
創(chuàng)建輕量級組件。
收集關(guān)于可用的字體、字體指標(biāo)和字體設(shè)置的信息。
設(shè)置顏色來渲染準(zhǔn)備圖片。
創(chuàng)造和獲取圖像,為渲染準(zhǔn)備圖片。
使用java.awt.PrintJob,java.awt.print和javax.print類里的打印
關(guān)于springboot2.0.6中觀察器和啟動的錯報告以及Headless模式相關(guān)分析是怎樣的就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。