|
| 1 | +package com.baeldung.shutdown; |
| 2 | + |
| 3 | +import com.baeldung.autoconfiguration.MySQLAutoconfiguration; |
| 4 | +import org.springframework.boot.ExitCodeGenerator; |
| 5 | +import org.springframework.boot.SpringApplication; |
| 6 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 7 | +import org.springframework.boot.builder.SpringApplicationBuilder; |
| 8 | +import org.springframework.boot.system.ApplicationPidFileWriter; |
| 9 | +import org.springframework.context.ConfigurableApplicationContext; |
| 10 | + |
| 11 | +import javax.annotation.security.RolesAllowed; |
| 12 | + |
| 13 | +@SpringBootApplication(exclude = MySQLAutoconfiguration.class) |
| 14 | +public class Application { |
| 15 | + |
| 16 | + public static void main(String[] args) { |
| 17 | + |
| 18 | + SpringApplication.run(Application.class, args); |
| 19 | +// closeApplication(); |
| 20 | +// exitApplication(); |
| 21 | +// writePID(); |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + private static void closeApplication() { |
| 26 | + |
| 27 | + ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class).web(false).run(); |
| 28 | + System.out.println("Spring Boot application started"); |
| 29 | + ctx.getBean(TerminateBean.class); |
| 30 | + ctx.close(); |
| 31 | + } |
| 32 | + |
| 33 | + private static void exitApplication() { |
| 34 | + |
| 35 | + ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class).web(false).run(); |
| 36 | + |
| 37 | + int exitCode = SpringApplication.exit(ctx, new ExitCodeGenerator() { |
| 38 | + @Override |
| 39 | + public int getExitCode() { |
| 40 | + // return the error code |
| 41 | + return 0; |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + System.out.println("Exit Spring Boot"); |
| 46 | + |
| 47 | + System.exit(exitCode); |
| 48 | + } |
| 49 | + |
| 50 | + private static void writePID() { |
| 51 | + SpringApplicationBuilder app = new SpringApplicationBuilder(Application.class).web(false); |
| 52 | + app.build().addListeners(new ApplicationPidFileWriter("./bin/shutdown.pid")); |
| 53 | + app.run(); |
| 54 | + } |
| 55 | +} |
0 commit comments