成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

spring boot學(xué)習(xí)(5): 進(jìn)程exit code自定義

張巨偉 / 2483人閱讀

摘要:用于應(yīng)用發(fā)生不可調(diào)整的異常,導(dǎo)致應(yīng)用退出的情況。聲明如下使用方式通過(guò)來(lái)注冊(cè),當(dāng)應(yīng)用發(fā)生異常時(shí),會(huì)調(diào)用每個(gè)的實(shí)現(xiàn)類(lèi)。取值前后正負(fù)不同,取最新相同,取絕對(duì)值大的。

在線上環(huán)境中,應(yīng)用可能因?yàn)橐恍┊惓6K止,我們?nèi)绻枰皶r(shí)找到原因,根據(jù) exit code 來(lái)定位,是個(gè)很好的途徑。 spring boot 為開(kāi)發(fā)者提供了相關(guān)的接口,方便開(kāi)發(fā)者通過(guò)異常類(lèi)型來(lái)定義自己的 exit codeExitCodeGeneratorExitCodeExceptionMapper.

1. ExitCodeGenerator:

用于主動(dòng)退出應(yīng)用,在 SpringApplication.exit(org.springframework.context.ApplicationContext,ExitCodeGenerator..)中用到,該方法會(huì)尋找所有的 ExitCodeGeneratorBean, 也會(huì)用到方法中的入?yún)?duì)象。

ExitCodeGenerator 聲明如下:

@FunctionalInterface
public interface ExitCodeGenerator {

    /**
     * Returns the exit code that should be returned from the application.
     * @return the exit code.
     */
    int getExitCode();

}
1.1 使用:

System.exit(SpringApplication.exit(SpringApplication.run(DemoApplication.class, args)));

@SpringBootApplication
public class DemoApplication{

    @Bean
    public ExitCoder getTestExitCoder(){
        return new ExitCoder();
    }

    @Bean
    public ExitCoder1 getTestExitCoder1(){
        return new ExitCoder1();
    }


    public static void main(String[] args) {
        System.exit(SpringApplication.exit(SpringApplication.run(DemoApplication.class, args)));
    }
}
import org.springframework.boot.ExitCodeGenerator;


public class ExitCoder implements ExitCodeGenerator {
    @Override
    public int getExitCode() {
        System.out.println("get exit code from class: ExitCoder");
        return 222;
    }
}
import org.springframework.boot.ExitCodeGenerator;

public class ExitCoder1 implements ExitCodeGenerator {
    @Override
    public int getExitCode() {
        System.out.println("get exit code from class: ExitCoder1");
        return 221;
    }
}

輸出為

2019-03-21 21:52:55.802  INFO 44627 --- [           main] com.example.exitcode.DemoApplication     : Starting DemoApplication on lei.local with PID 44627 (/Users/lei/own/projects/java_learning/spring_boot_learning/blog/5exitcode/exitcode/out/production/classes started by lei in /Users/lei/own/projects/java_learning/spring_boot_learning/blog/5exitcode/exitcode)
2019-03-21 21:52:55.806  INFO 44627 --- [           main] com.example.exitcode.DemoApplication     : No active profile set, falling back to default profiles: default
2019-03-21 21:52:56.339  INFO 44627 --- [           main] com.example.exitcode.DemoApplication     : Started DemoApplication in 15.901 seconds (JVM running for 21.676)
get exit code from class: ExitCoder
get exit code from class: ExitCoder1
Disconnected from the target VM, address: "127.0.0.1:50873", transport: "socket"

Process finished with exit code 222

從上面可以看到,以 exit code 最大的為最終值。

2. ExitCodeExceptionMapper

用于應(yīng)用發(fā)生不可調(diào)整的異常,導(dǎo)致應(yīng)用退出的情況。聲明如下:

@FunctionalInterface
public interface ExitCodeExceptionMapper {

    /**
     * Returns the exit code that should be returned from the application.
     * @param exception the exception causing the application to exit
     * @return the exit code or {@code 0}.
     */
    int getExitCode(Throwable exception);

}
2.1 使用方式

通過(guò) Bean 來(lái)注冊(cè),當(dāng)應(yīng)用發(fā)生異常時(shí),會(huì)調(diào)用每個(gè)ExitCodeExceptionMapper 的實(shí)現(xiàn)類(lèi)。這里,我們可以根據(jù)異常類(lèi)型來(lái)設(shè)置自己的 exit code:

@SpringBootApplication
public class DemoApplication{
    @Bean
    public DemoExitCodeExceptionMapper getExitCodeMapper(){
        return new DemoExitCodeExceptionMapper();
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }


    @Bean
    CommandLineRunner showUsers() {
        return args -> {throw new Exception("xxxxx");};
    }
}
public class DemoExitCodeExceptionMapper implements ExitCodeExceptionMapper{
    /**
     * Returns the exit code that should be returned from the application.
     * @param exception the exception causing the application to exit
     * @return the exit code or {@code 0}.
     */
    @Override
    public int getExitCode(Throwable exception){
        System.out.println("exit cod xxxx" + exception.getMessage());
        if(exception.getCause().getMessage().equals("sdf")){
            return 254;
        }
        return 243;
    }
}

運(yùn)行輸出為:

2019-03-21 22:13:34.261  INFO 45049 --- [           main] com.example.exitcode.DemoApplication     : Started DemoApplication in 15.816 seconds (JVM running for 21.521)
exit cod xxxxFailed to execute CommandLineRunner
2019-03-21 22:13:38.797  INFO 45049 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with "debug" enabled.
2019-03-21 22:13:38.845 ERROR 45049 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at com.example.exitcode.DemoApplication.main(DemoApplication.java:31) [classes/:na]
Caused by: java.lang.Exception: xxxxx
    at com.example.exitcode.DemoApplication.lambda$showUsers$0(DemoApplication.java:37) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    ... 5 common frames omitted

Disconnected from the target VM, address: "127.0.0.1:51237", transport: "socket"

Process finished with exit code 243
3. 取值邏輯
public int getExitCode() {
    int exitCode = 0;
    for (ExitCodeGenerator generator : this.generators) {
        try {
            int value = generator.getExitCode();
            if (value > 0 && value > exitCode || value < 0 && value < exitCode) {
                exitCode = value;
            }
        }
        catch (Exception ex) {
            exitCode = (exitCode != 0) ? exitCode : 1;
            ex.printStackTrace();
        }
    }
    return exitCode;
}

第一個(gè): 直接取第一個(gè)的值。

2....n-1,n: nn-1 正負(fù)不同,取 n, 相同,取絕對(duì)值大的。

4.總結(jié):

主動(dòng)調(diào)用SpringApplication.exit 方法使用ExitCodeGenerator ,可以通過(guò) Bean注冊(cè),也可通過(guò)傳值。

應(yīng)用異常退出使用 ExitCodeExceptionMapper, 只能通過(guò) Bean 注冊(cè)使用。

取值:
前后正負(fù)不同,取最新, 相同,取絕對(duì)值大的。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/73840.html

相關(guān)文章

  • Spring Boot - 定義啟動(dòng)banner

    摘要:背景這段時(shí)間較忙,有些想念小紅,為了表達(dá)我對(duì)小紅的思念之情,決定將啟動(dòng)的研究一下,看看是否能夠自定義,讓我天天能夠看到她。 背景 這段時(shí)間較忙,有些想念小紅,為了表達(dá)我對(duì)小紅的思念之情,決定將spring boot啟動(dòng)的banner研究一下,看看是否能夠自定義,讓我天天能夠看到她。 展示 經(jīng)過(guò)調(diào)研,發(fā)現(xiàn)自定義banner是一個(gè)輕松愉快的過(guò)程,忍不住讓我多啟動(dòng)幾次,先看看效果:(省略了一...

    CollinPeng 評(píng)論0 收藏0
  • 慕課網(wǎng)_《Kafka流處理平臺(tái)》學(xué)習(xí)總結(jié)

    摘要:慕課網(wǎng)流處理平臺(tái)學(xué)習(xí)總結(jié)時(shí)間年月日星期日說(shuō)明本文部分內(nèi)容均來(lái)自慕課網(wǎng)。 慕課網(wǎng)《Kafka流處理平臺(tái)》學(xué)習(xí)總結(jié) 時(shí)間:2018年09月09日星期日 說(shuō)明:本文部分內(nèi)容均來(lái)自慕課網(wǎng)。@慕課網(wǎng):https://www.imooc.com 教學(xué)源碼:無(wú) 學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:課程介紹 1-1 課程介紹 課程介紹 Kafk...

    Maxiye 評(píng)論0 收藏0
  • 基于Shiro,JWT實(shí)現(xiàn)微信小程序登錄完整例子

    摘要:小程序官方流程圖如下,官方地址如果此圖理解不清楚的地方也可參看我的博客本文是對(duì)接微信小程序自定義登錄的一個(gè)完整例子實(shí)現(xiàn),技術(shù)棧為。調(diào)用微信接口獲取和根據(jù)和自定義登陸態(tài)返回自定義登陸態(tài)給小程序端。 小程序官方流程圖如下,官方地址 : https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login....

    cfanr 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<