Spring Boot的exit code
任何应用程序都有exit code,这个code是int值包含负值,在本文中我们将会探讨Spring Boot中的 exit code。
Spring Boot的exit code
Spring Boot如果启动遇到错误,则会返回1.正常退出的话则会返回0.
Spring Boot向JVM注册了shutdown hooks来保证应用程序优雅的退出。Spring Boot还提供了org.springframework.boot.ExitCodeGenerator接口,来方便自定义退出code.
自定义Exit Codes
Spring Boot提供了三种方式来让我们自定义exit code。
ExitCodeGenerator,ExitCodeExceptionMapper和ExitCodeEvent。下面我们分别来讲解。
ExitCodeGenerator
实现ExitCodeGenerator接口,我们需要自己实现getExitCode()方法来自定义返回代码:
@SpringBootApplication
public class ExitCodeApp implements ExitCodeGenerator {
public static void main(String[] args) {
System.exit(SpringApplication.exit(SpringApplication.run(ExitCodeApp.class, args)));
}
@Override
public int getExitCode() {
return 11;
}
}
ExitCodeExceptionMapper
如果我们遇到runtime exception的时候,可以使用ExitCodeExceptionMapper来做错误代码的映射如下:
@Bean
CommandLineRunner createException() {
return args -> Integer.parseInt("test") ;
}
@Bean
ExitCodeExceptionMapper exitCodetoExceptionMapper() {
return exception -> {
// set exit code base on the exception type
if (exception.getCause() instanceof NumberFormatException) {
return 80;
}
return 1;
};
}
上面的例子我们创建了一个CommandLineRunner bean,在实例化的过程中会抛出NumberFormatException,然后在ExitCodeExceptionMapper中,我们会捕捉到这个异常,返回特定的返回值。
ExitCodeEvent
我们还可以使用ExitCodeEvent来捕捉异常事件如下所示:
@Bean
DemoListener demoListenerBean() {
return new DemoListener();
}
private static class DemoListener {
@EventListener
public void exitEvent(ExitCodeEvent event) {
System.out.println("Exit code: " + event.getExitCode());
}
}
当应用程序退出的时候,exitEvent() 方法会被调用。
本文的例子可以参考:https://github.com/ddean2009/learn-springboot2/tree/master/springboot-exitcode
更多教程请参考 flydean的博客
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。