A command-line Boot process can return a nonzero exit code for a failed business run, allowing its scheduler to distinguish failure from a clean stop.
Spring Boot process exit codes: make job failure visible to the scheduler
Map outcome to process status
A nightly receipt settlement may finish its Java main method even though the imported job failed. A zero process exit then tells the scheduler the run succeeded. Implement ExitCodeGenerator for the final business outcome and pass SpringApplication.exit’s result to System.exit in a one-shot process. A long-running HTTP service usually should not exit after a single request failure. Batch flow status] determines which job result should be mapped.
Avoid a false success
If a runner throws before normal shutdown, a domain exception can also carry an exit code. Test both a normal completed job and a failed job after the context closes. Several generators may exist; establish their ordering or consolidate the policy so a later nonzero code does not surprise operators. Keep the mapping small and documented in the scheduler configuration. Runner failures] are startup failures, not ordinary request errors.
Exercise the real shell contract
Launch the packaged artifact as a subprocess against a disposable input manifest. Check its numeric exit status and durable result rows. A mocked ExitCodeGenerator test can prove one method returns 47, but not that main actually passes the value to the operating system. For a retryable failure, reconcile committed chunks before the scheduler launches the same job identity again.
Implementation contract
final class SettlementExitCode implements ExitCodeGenerator {
private final SettlementResult result;
SettlementExitCode(SettlementResult result) { this.result = result; }
@Override public int getExitCode() {
return result.failed() ? 47 : 0;
}
}
// In the one-shot main method:
System.exit(SpringApplication.exit(SpringApplication.run(ReceiptBatchApp.class, args)));Cost and verification
Exit-code calculation is negligible. A wrong mapping can cause an unattended scheduler to skip a failed run or retry an already completed one.
Common Mistakes
- Do not return zero after a failed batch execution.
- Do not terminate a long-running web server for one request error.
- Do not test only the generator while leaving the main method’s process exit unverified.
Read next
Spring Batch flow status: distinguish step failure from job restartability, Spring Boot runner failure: reject startup instead of accepting unsafe settings, Spring Batch job identity: a manifest ID defines restart versus a new run, Spring Batch orphaned STARTED execution: inspect before relaunch, Spring Boot FailureAnalyzer: turn one startup exception into an actionable failure.
