Bael 1625 (#3831)
* shutdown boot app * test the ctx.close() * application shutdown test * BAEL-1625 * /shutdown endpoint test * bring back the closeApplication * @Ignore shutdown endpoint test
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
b34473ebbb
commit
19fb7ef5a2
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.shutdown;
|
||||
|
||||
import com.baeldung.autoconfiguration.MySQLAutoconfiguration;
|
||||
import org.springframework.boot.ExitCodeGenerator;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.system.ApplicationPidFileWriter;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
|
||||
@SpringBootApplication(exclude = MySQLAutoconfiguration.class)
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
SpringApplication.run(Application.class, args);
|
||||
// closeApplication();
|
||||
// exitApplication();
|
||||
// writePID();
|
||||
|
||||
}
|
||||
|
||||
private static void closeApplication() {
|
||||
|
||||
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class).web(false).run();
|
||||
System.out.println("Spring Boot application started");
|
||||
ctx.getBean(TerminateBean.class);
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
private static void exitApplication() {
|
||||
|
||||
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class).web(false).run();
|
||||
|
||||
int exitCode = SpringApplication.exit(ctx, new ExitCodeGenerator() {
|
||||
@Override
|
||||
public int getExitCode() {
|
||||
// return the error code
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("Exit Spring Boot");
|
||||
|
||||
System.exit(exitCode);
|
||||
}
|
||||
|
||||
private static void writePID() {
|
||||
SpringApplicationBuilder app = new SpringApplicationBuilder(Application.class).web(false);
|
||||
app.build().addListeners(new ApplicationPidFileWriter("./bin/shutdown.pid"));
|
||||
app.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.shutdown;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.baeldung.shutdown")
|
||||
public class ShutdownConfig {
|
||||
|
||||
@Bean
|
||||
public TerminateBean getTerminateBean() {
|
||||
return new TerminateBean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.shutdown;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
public class TerminateBean {
|
||||
|
||||
@PreDestroy
|
||||
public void onDestroy() throws Exception {
|
||||
System.out.println("Spring Container is destroyed!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.shutdown.shutdown;
|
||||
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class ShutdownController {
|
||||
}
|
||||
Reference in New Issue
Block a user