Spring Boot Exit Codes – Spring Boot退出代码

最后修改: 2018年 5月 14日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Every application returns an exit code on exit; this code can be any integer value including negative values.

每个应用程序在退出时都会返回一个退出代码;这个代码可以是任何整数值,包括负值。

In this quick tutorial, we’re going to find out how we can return exit codes from a Spring Boot application.

在这个快速教程中,我们将了解如何从Spring Boot应用程序中返回退出代码。

2. Spring Boot and Exit Codes

2.Spring Boot和退出代码

A Spring Boot application will exit with code 1 if an exception occurs at startup. Otherwise, on a clean exit, it provides 0 as the exit code.

如果启动时出现异常,Spring Boot应用程序将以代码1退出。否则,在干净的退出时,它会提供0作为退出代码。

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.

Spring向JVM注册了关闭钩子,以确保ApplicationContext在退出时优雅地关闭。除此之外,Spring还提供了接口org.springframework.boot.ExitCodeGenerator。这个接口可以在System.exit()被调用时返回具体代码。

3. Implementing Exit Codes

3.实施退出代码

Spring Boot provides four methods that allow us to work with exit codes.

Spring Boot提供了四种方法,使我们能够处理退出代码。

The ExitCodeGenerator interface and ExitCodeExceptionMapper allow us to specify custom exit codes, while the ExitCodeEvent allows us to read the exit code on exit. Moreover, it’s even possible for exceptions to implement the ExitCodeGenerator interface.

ExitCodeGenerator接口和ExitCodeExceptionMapper允许我们指定自定义退出代码,而ExitCodeEvent允许我们读取退出时的退出代码。此外,甚至还可以让异常实现ExitCodeGenerator接口。

3.1. ExitCodeGenerator

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:

让我们创建一个实现ExitCodeGenerator接口的类。我们必须实现getExitCode()方法,它返回一个整数值。

@SpringBootApplication
public class ExitCodeGeneratorDemoApplication 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 ExitCodeGeneratorDemoApplication class implements the ExitCodeGenerator interface. Also, we wrapped the call to SpringApplication.run() with SpringApplication.exit().

这里,ExitCodeGeneratorDemoApplication类实现了ExitCodeGenerator接口。此外,我们用SpringApplication.run()包裹了对SpringApplication.exit()的调用。

On exit, the exit code will now be 42.

退出时,退出代码现在将是42。

3.2. ExitCodeExceptionMapper

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:

现在让我们来看看我们如何能够根据运行时异常返回一个退出代码。为此,我们实现一个CommandLineRunner,它总是抛出一个NumberFormatException,然后注册一个ExitCodeExceptionMapper类型的bean。

@Bean
CommandLineRunner createException() {
    return args -> Integer.parseInt("test") ;
}

@Bean
ExitCodeExceptionMapper exitCodeToexceptionMapper() {
    return exception -> {
        // set exit code based 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.

ExitCodeExceptionMapper中,我们简单地将异常映射到某个退出代码。

3.3. ExitCodeEvent

3.3.ExitCodeEvent[/em]

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):

接下来,我们将捕获一个ExitCodeEvent来读取我们应用程序的退出代码为此,我们只需注册一个事件监听器,该监听器将订阅ExitCodeEvents(在本例中名为DemoListener)。

@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.

现在,当应用程序退出时,方法exitEvent()将被调用,我们可以从该事件中读取退出代码。

3.4. Exception with Exit Code

3.4.有退出代码的异常情况

An exception can also implement the ExitCodeGenerator interface. When throwing such exceptions, Spring Boot returns the exit code provided by the implemented getExitCode() method. For instance:

异常也可以实现ExitCodeGenerator接口。当抛出此类异常时,Spring Boot会返回由实现的getExitCode()方法提供的退出代码。举例来说。

public class FailedToStartException extends RuntimeException implements ExitCodeGenerator {

    @Override
    public int getExitCode() {
        return 42;
    }
}

If we throw an instance of FailedToStartException, Spring Boot will detect this exception as an ExitCodeGenerator and report 42 as the exit code.

如果我们抛出一个FailedToStartException的实例,Spring Boot将检测这个异常作为ExitCodeGenerator并报告42作为退出代码。

4. Conclusion

4.总结

In this article, we’ve gone through multiple options provided by Spring Boot to work with exit codes.

在这篇文章中,我们浏览了Spring Boot提供的多个选项来处理退出代码。

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.

代码样本可以在GitHub上找到over