在线上环境中,应用可能因为一些异常而终止,我们如果需要及时找到原因,根据 exit code 来定位,是个很好的途径。 spring boot 为开发者提供了相关的接口,方便开发者通过异常类型来定义自己的 exit codeExitCodeGeneratorExitCodeExceptionMapper.

1. ExitCodeGenerator:

用于主动退出应用,在 SpringApplication.exit(org.springframework.context.ApplicationContext,ExitCodeGenerator..)中用到,该方法会寻找所有的 ExitCodeGeneratorBean, 也会用到方法中的入参对象。

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;
}
复制代码
  1. 第一个: 直接取第一个的值。
  2. 2....n-1,n: nn-1 正负不同,取 n, 相同,取绝对值大的。

4.总结:

  • 主动调用SpringApplication.exit 方法使用ExitCodeGenerator ,可以通过 Bean注册,也可通过传值。
  • 应用异常退出使用 ExitCodeExceptionMapper, 只能通过 Bean 注册使用。
  • 取值: 前后正负不同,取最新, 相同,取绝对值大的。

转载于:https://juejin.im/post/5c93a576e51d4574d019cf7d

spring boot学习(5): 进程exit code自定义相关推荐

  1. Spring Boot 学习系列(08)—自定义servlet、filter及listener

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 传统的filter及listener配置 在传统的Java web项目中,servlet.filter和li ...

  2. Spring Boot 学习系列(05)—自定义视图解析规则

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 自定义视图解析 在默认情况下Spring Boot 的MVC框架使用的视图解析ViewResolver类是C ...

  3. Spring Boot 学习系列(09)—自定义Bean的顺序加载

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Bean 的顺序加载 有些场景中,我们希望编写的Bean能够按照指定的顺序进行加载.比如,有UserServ ...

  4. Spring Boot学习笔记-基础(2)

    Spring Boot学习笔记-基础(2) Spring Boot 优点: – 快速创建独立运行的Spring项目以及与主流框架集成 – 使用嵌入式的Servlet容器,应用无需打成WAR包 – st ...

  5. Spring Boot学习笔记-实践建言

    2019独角兽企业重金招聘Python工程师标准>>> 本文延续<Spring Boot学习笔记-快速示例>,从开发指南中摘出一些实践经验可供参考.这也是笔者看到的眼前一 ...

  6. Spring Boot学习笔记-进阶(3)

    文章目录 Spring Boot学习笔记-进阶(3) 一.Spring Boot与缓存 二.Spring Boot与消息 三.Spring Boot与检索 四.Spring Boot与任务 异步任务 ...

  7. Spring Boot学习笔记(1)

    文章目录 Spring Boot学习笔记(1) Spring Boot 整合 JSP Spring Boot HTML Thymeleaf 常用语法 Spring Boot 数据校验 Spring B ...

  8. 2023最新首发,全网最全 Spring Boot 学习宝典(附思维导图)

    作者:bug菌 博客:CSDN.掘金.infoQ.51CTO等 简介:CSDN/阿里云/华为云/51CTO博客专家,博客之星Top30,掘金年度人气作者Top40,51CTO年度博主Top12,掘金/ ...

  9. 八个开源的 Spring Boot 学习资源,你值得拥有

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:什么?你还在使用fastjson,性能太差了个人原创+1博客:点击前往,查看更多 转载自:牧马小子 Spring ...

最新文章

  1. fiddler及postman讲解
  2. atcode062D(预处理&优先队列)
  3. NIPS 2016 | Best Paper, Dual Learning, Review Network, VQA 等论文选介
  4. 利剑无意之JAVA面试题(三)
  5. MSP432 库函数实现 PID 电机调角度、调速
  6. 医疗:ICU(10)
  7. 10个理由告诉你为何要学Java编程?
  8. 飞秋(FeiQ)海量的用户基数决定了这一模式
  9. 计算机游戏无法运行程序包,一体电脑显示或声音不正常,某些软件或游戏无法正常运行怎么办...
  10. 重磅!2021年考研国家线公布!
  11. wake on lan
  12. Web.config中注册用户控件和自定义控件
  13. web 前端签名插件_10款前端开发神器,助你成前端高手?
  14. 项目管理工具project软件学习(四) - 日历保存为模板、日历重命名、删除
  15. 华为ensp模拟器 三层交换机
  16. python selenium无头浏览器
  17. 动态规划(dp)的总结
  18. HTML 的js中手机号,身份证号等正则表达式表示
  19. vue调取电脑摄像头实现拍照功能
  20. lvs集群实现lvs-dr模型和lvs-nat模型

热门文章

  1. x509代码实例java_Java X509AttributeCertificate.getIssuer方法代码示例
  2. cesium three性能比较_mapboxgl + three 动画 — 网格热图
  3. 2019_8_1python
  4. 牛客假日团队赛6 D 迷路的牛 (思维)
  5. python 基础之字符串方法
  6. Vue.js(17)之 插槽
  7. HTML5 file api读取文件的MD5码工具
  8. SSRS: How to Display Checkbox on Report
  9. tomcat 默认站点的配置
  10. CodeSmith基础(二)