SpringBootExitCodes
In this quick tutorial, we"re going to find out how we can return exit codes from a Spring Boot application.
2. Spring Boot and Exit Codes
A Spring Boot application will exit with the code 1 if an exception occurs at startup. Otherwise, on a clean exit, it provides 0 as the exit code.
Spring registers shutdown hooks with the JVM to ensure the ApplicationContext closes gracefully on exit. In addition to that, Spring also provides the interface org.springframework.boot.ExitCodeGenerator. This interface can return the specific code when System.exit() is called.
3. Implementing Exit Codes
Boot provides three methods that allow us to work with exit codes.
The ExitCodeGenerator interface and ExitCodeExceptionMapper allow us to specify custom exit codes while the ExitCodeEvent allows us to read the exit code on exit.
3.1. ExitCodeGenerator
Let"s create a class that implements the ExitCodeGenerator interface. We have to implement the method getExitCode() which returns an integer value:
1
2
3
4
5
6
7
8
9
10
11
12
13
@SpringBootApplication
public
class
DemoApplication
implements
ExitCodeGenerator {
public
static
void
main(String[] args) {
System.exit(SpringApplication
.exit(SpringApplication.run(DemoApplication.
class
, args)));
}
@Override
public
int
getExitCode() {
return
42
;
}
}
Here, the DemoApplication class implements the ExitCodeGenerator interface. Also, we wrapped the call to SpringApplication.run() with SpringApplication.exit().
On exit, the exit code will now be 42.
3.2. ExitCodeExceptionMapper
Now let"s find out how we can return an exit code based on a runtime exception. For this, we implement a CommandLineRunner which always throws a NumberFormatException and then register a bean of type ExitCodeExceptionMapper:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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
;
};
}
Within the ExitCodeExceptionMapper, we simply map the exception to a certain exit code.
3.3. ExitCodeEvent
Next, we"ll capture an ExitCodeEvent to read the exit code of our application. For this, we simply register an event listener which subscribes to ExitCodeEvents (named DemoListener in this example):
1
2
3
4
5
6
7
8
9
10
11
@Bean
DemoListener demoListenerBean() {
return
new
DemoListener();
}
private
static
class
DemoListener {
@EventListener
public
void
exitEvent(ExitCodeEvent event) {
System.out.println(
"Exit code: "
+ event.getExitCode());
}
}
Now, when the application exits, the method exitEvent() will be invoked and we can read the exit code from the event.
4. Conclusion
In this article, we"ve gone through multiple options provided by Spring Boot to work with exit codes.
It"s very important for any application to return the right error code while exiting. The exit code determines the state of the application when the exit happened. In addition to that, it helps in troubleshooting.
Code samples can be found over on GitHub.
以上是 SpringBootExitCodes 的全部内容, 来源链接: utcz.com/z/513036.html