* 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:
Mher Baghinyan 2018-04-02 09:56:55 +04:00 committed by Grzegorz Piwowarek
parent b34473ebbb
commit 19fb7ef5a2
7 changed files with 133 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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!");
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.shutdown.shutdown;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ShutdownController {
}

View File

@ -30,6 +30,7 @@ info.java-vendor = ${java.specification.vendor}
security.user.name=admin1
security.user.password=secret1
management.security.role=SUPERUSER
management.endpoint.shutdown.enabled=true
logging.level.org.springframework=INFO

View File

@ -0,0 +1 @@
kill $(cat ./bin/shutdown.pid)

View File

@ -0,0 +1,43 @@
package com.baeldung.shutdown;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
@AutoConfigureMockMvc
public class ShutdownApplicationTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
@Ignore
public void givenBootApp_whenShutdownEndpoint_thenExit() throws Exception {
mockMvc.perform(
post("/shutdown"))
.andExpect(status().isOk());
}
}