JAVA-8358: Move Spring Boot Exit Codes code
This commit is contained in:
@@ -11,4 +11,3 @@ This module contains articles about Spring Boot customization
|
||||
- [Spring Boot: Configuring a Main Class](https://www.baeldung.com/spring-boot-main-class)
|
||||
- [How to Define a Spring Boot Filter?](https://www.baeldung.com/spring-boot-add-filter)
|
||||
- [Guide to the Favicon in Spring Boot](https://www.baeldung.com/spring-boot-favicon)
|
||||
- [Spring Boot Exit Codes](https://www.baeldung.com/spring-boot-exit-codes)
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.baeldung.exitcode.event;
|
||||
|
||||
import org.springframework.boot.ExitCodeEvent;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.event.EventListener;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ExitCodeEventDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.exit(
|
||||
SpringApplication.exit(
|
||||
SpringApplication.run(ExitCodeEventDemoApplication.class, args)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Bean
|
||||
DemoListener demoListenerBean() {
|
||||
return new DemoListener();
|
||||
}
|
||||
|
||||
private static class DemoListener {
|
||||
@EventListener
|
||||
public void exitEvent(ExitCodeEvent event) {
|
||||
System.out.println("Exit code: " + event.getExitCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.baeldung.exitcode.exception;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.ExitCodeExceptionMapper;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ExitCodeExceptionMapperDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ExitCodeExceptionMapperDemoApplication.class, args);
|
||||
}
|
||||
|
||||
@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;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.exitcode.exceptionexitgen;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ExceptionExitCodeGeneratorApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ExceptionExitCodeGeneratorApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
CommandLineRunner failApplication() {
|
||||
return args -> {
|
||||
throw new FailedToStartException();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.exitcode.exceptionexitgen;
|
||||
|
||||
import org.springframework.boot.ExitCodeGenerator;
|
||||
|
||||
public class FailedToStartException extends RuntimeException implements ExitCodeGenerator {
|
||||
|
||||
@Override
|
||||
public int getExitCode() {
|
||||
return 127;
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.exitcode.generator;
|
||||
|
||||
import org.springframework.boot.ExitCodeGenerator;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ExitCodeGeneratorDemoApplication implements ExitCodeGenerator {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.exit(
|
||||
SpringApplication.exit(
|
||||
SpringApplication.run(ExitCodeGeneratorDemoApplication.class, args)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getExitCode() {
|
||||
return 42;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user