日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring boot学习(5): 进程exit code自定义

發布時間:2023/12/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot学习(5): 进程exit code自定义 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在線上環境中,應用可能因為一些異常而終止,我們如果需要及時找到原因,根據 exit code 來定位,是個很好的途徑。 spring boot 為開發者提供了相關的接口,方便開發者通過異常類型來定義自己的 exit code:ExitCodeGenerator 和 ExitCodeExceptionMapper.

1. ExitCodeGenerator:

用于主動退出應用,在 SpringApplication.exit(org.springframework.context.ApplicationContext,ExitCodeGenerator..)中用到,該方法會尋找所有的 ExitCodeGenerator 的 Bean, 也會用到方法中的入參對象。

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{@Beanpublic ExitCoder getTestExitCoder(){return new ExitCoder();}@Beanpublic 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 {@Overridepublic int getExitCode() {System.out.println("get exit code from class: ExitCoder");return 222;} } 復制代碼import org.springframework.boot.ExitCodeGenerator;public class ExitCoder1 implements ExitCodeGenerator {@Overridepublic 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:

用于應用發生不可調整的異常,導致應用退出的情況。聲明如下:

@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 使用方式

通過 Bean 來注冊,當應用發生異常時,會調用每個ExitCodeExceptionMapper 的實現類。這里,我們可以根據異常類型來設置自己的 exit code:

@SpringBootApplication public class DemoApplication{@Beanpublic DemoExitCodeExceptionMapper getExitCodeMapper(){return new DemoExitCodeExceptionMapper();}public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}@BeanCommandLineRunner 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}.*/@Overridepublic int getExitCode(Throwable exception){System.out.println("exit cod xxxx" + exception.getMessage());if(exception.getCause().getMessage().equals("sdf")){return 254;}return 243;} } 復制代碼

運行輸出為:

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 failedjava.lang.IllegalStateException: Failed to execute CommandLineRunnerat 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: xxxxxat 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 omittedDisconnected 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; } 復制代碼
  • 第一個: 直接取第一個的值。
  • 2....n-1,n: n 和n-1 正負不同,取 n, 相同,取絕對值大的。
  • 4.總結:

    • 主動調用SpringApplication.exit 方法使用ExitCodeGenerator ,可以通過 Bean注冊,也可通過傳值。
    • 應用異常退出使用 ExitCodeExceptionMapper, 只能通過 Bean 注冊使用。
    • 取值: 前后正負不同,取最新, 相同,取絕對值大的。

    轉載于:https://juejin.im/post/5c93a576e51d4574d019cf7d

    總結

    以上是生活随笔為你收集整理的spring boot学习(5): 进程exit code自定义的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。