Spring-boot: Improve documentation for exit code generation

Created on 27 Jan 2016  路  3Comments  路  Source: spring-projects/spring-boot

I was using the exit code generator feature and as per the document

In addition, beans may implement the org.springframework.boot.ExitCodeGenerator interface if they wish to return a specific exit code when the application ends.

Does this bean gets called whenever a proper shut down happen, or it require a call to SpringApplication.exit static method?

For me first option is not happening on a spring boot console application i.e. no web dependency. But when I call the SpringApplication.exit static method ExitCodeGenerator.getExitCode is called but still returns exit code 0 and also it forces the tests to fail with a message that

Ensure that the context has not been closed programmatically.

example can be found here boot-exitcode

superseded

Most helpful comment

Does this bean gets called whenever a proper shut down happen, or it require a call to SpringApplication.exit static method?

It's required to call the exit method.

But when I call the SpringApplication.exit static method ExitCodeGenerator.getExitCode is called but still returns exit code 0 and also it forces the tests to fail

You're not doing anything with the application's exit code, i.e. you haven't passed it into a call to System.exit(), so the JVM's default exit code is being produced. As the JVM has exited cleanly, it's zero.

Take a look at SampleBatchApplication to see how we expect the application's exit code to be used:

System.exit(SpringApplication
        .exit(SpringApplication.run(SampleBatchApplication.class, args)));

All 3 comments

Does this bean gets called whenever a proper shut down happen, or it require a call to SpringApplication.exit static method?

It's required to call the exit method.

But when I call the SpringApplication.exit static method ExitCodeGenerator.getExitCode is called but still returns exit code 0 and also it forces the tests to fail

You're not doing anything with the application's exit code, i.e. you haven't passed it into a call to System.exit(), so the JVM's default exit code is being produced. As the JVM has exited cleanly, it's zero.

Take a look at SampleBatchApplication to see how we expect the application's exit code to be used:

System.exit(SpringApplication
        .exit(SpringApplication.run(SampleBatchApplication.class, args)));

OK my bad I should have posted it to SW. But isn't it good to have it documented as well.

Closing in favour of PR #9740

Was this page helpful?
0 / 5 - 0 ratings