From bb301a96f338ec1e2189dfa3f5abb4307d92a114 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Wed, 14 Mar 2018 12:16:30 +0400 Subject: [PATCH 1/7] shutdown boot app --- .../main/java/com/baeldung/shutdown/Application.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 spring-boot/src/main/java/com/baeldung/shutdown/Application.java diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java new file mode 100644 index 000000000000..7b20085dd622 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java @@ -0,0 +1,12 @@ +package com.baeldung.shutdown; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String [] args) { + SpringApplication.run(Application.class, args).close(); + } +} From 80c1e081b844a306e459d241c9aa1d31f177f7f6 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Thu, 15 Mar 2018 15:41:03 +0400 Subject: [PATCH 2/7] test the ctx.close() --- .../com/baeldung/shutdown/Application.java | 21 +++++++++++++++++-- .../com/baeldung/shutdown/ShutdownConfig.java | 15 +++++++++++++ .../com/baeldung/shutdown/TerminateBean.java | 11 ++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 spring-boot/src/main/java/com/baeldung/shutdown/ShutdownConfig.java create mode 100644 spring-boot/src/main/java/com/baeldung/shutdown/TerminateBean.java diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java index 7b20085dd622..5085caffce07 100644 --- a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java +++ b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java @@ -1,12 +1,29 @@ 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.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.ComponentScan; -@SpringBootApplication +@SpringBootApplication(exclude = MySQLAutoconfiguration.class) public class Application { public static void main(String [] args) { - SpringApplication.run(Application.class, args).close(); + + ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class).web(false).run(); + System.out.println("Spring Boot application started"); + ctx.getBean(TerminateBean.class); + ctx.close(); +// SpringApplication.exit(ctx, () -> 0); + System.out.println("Exit Spring Boot"); + +// int exitCode = SpringApplication.exit(ctx, (ExitCodeGenerator) () -> { +// // no errors +// return 0; +// }); +// System.exit(exitCode); } } diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/ShutdownConfig.java b/spring-boot/src/main/java/com/baeldung/shutdown/ShutdownConfig.java new file mode 100644 index 000000000000..2068f45b7dd3 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/shutdown/ShutdownConfig.java @@ -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(); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/TerminateBean.java b/spring-boot/src/main/java/com/baeldung/shutdown/TerminateBean.java new file mode 100644 index 000000000000..4f3b81b9207f --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/shutdown/TerminateBean.java @@ -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!"); + } +} From 40cb8e907a2749add4e032170c37e3b8a8bba92e Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Fri, 16 Mar 2018 14:53:03 +0400 Subject: [PATCH 3/7] application shutdown test --- .../main/java/com/baeldung/shutdown/Application.java | 12 +++++++----- .../baeldung/shutdown/ApplicationShutdownTest.java | 11 +++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 spring-boot/src/test/java/com/baeldung/shutdown/ApplicationShutdownTest.java diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java index 5085caffce07..ab310134db71 100644 --- a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java +++ b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java @@ -20,10 +20,12 @@ public static void main(String [] args) { // SpringApplication.exit(ctx, () -> 0); System.out.println("Exit Spring Boot"); -// int exitCode = SpringApplication.exit(ctx, (ExitCodeGenerator) () -> { -// // no errors -// return 0; -// }); -// System.exit(exitCode); + int exitCode = SpringApplication.exit(ctx, new ExitCodeGenerator() { + @Override + public int getExitCode() { + // return the error code + return 500; + } + }); } } diff --git a/spring-boot/src/test/java/com/baeldung/shutdown/ApplicationShutdownTest.java b/spring-boot/src/test/java/com/baeldung/shutdown/ApplicationShutdownTest.java new file mode 100644 index 000000000000..599aa697db6c --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/shutdown/ApplicationShutdownTest.java @@ -0,0 +1,11 @@ +package com.baeldung.shutdown; + +import org.junit.Test; + +public class ApplicationShutdownTest { + + @Test + public void whenShutDown_thenCloseApplication() { + + } +} From 860feadf3495365cb136b7c3babdee1b75d34f7d Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Sat, 17 Mar 2018 11:22:08 +0400 Subject: [PATCH 4/7] BAEL-1625 --- .../com/baeldung/shutdown/Application.java | 29 ++++++++++++++++--- .../src/main/resources/application.properties | 1 + .../src/main/resources/shutdown/shutdown.bat | 1 + .../shutdown/ApplicationShutdownTest.java | 11 ------- 4 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 spring-boot/src/main/resources/shutdown/shutdown.bat delete mode 100644 spring-boot/src/test/java/com/baeldung/shutdown/ApplicationShutdownTest.java diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java index ab310134db71..a4c31be983fc 100644 --- a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java +++ b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java @@ -5,20 +5,31 @@ 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 org.springframework.context.annotation.ComponentScan; @SpringBootApplication(exclude = MySQLAutoconfiguration.class) public class Application { - public static void main(String [] args) { + public static void main(String[] 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(); -// SpringApplication.exit(ctx, () -> 0); - System.out.println("Exit Spring Boot"); + } + + private static void exitApplication() { + + ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class).web(false).run(); int exitCode = SpringApplication.exit(ctx, new ExitCodeGenerator() { @Override @@ -27,5 +38,15 @@ public int getExitCode() { return 500; } }); + + 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(); } } diff --git a/spring-boot/src/main/resources/application.properties b/spring-boot/src/main/resources/application.properties index 059a6c96bec6..203f0ee3c530 100644 --- a/spring-boot/src/main/resources/application.properties +++ b/spring-boot/src/main/resources/application.properties @@ -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 diff --git a/spring-boot/src/main/resources/shutdown/shutdown.bat b/spring-boot/src/main/resources/shutdown/shutdown.bat new file mode 100644 index 000000000000..2e3bbb50f1d5 --- /dev/null +++ b/spring-boot/src/main/resources/shutdown/shutdown.bat @@ -0,0 +1 @@ +kill $(cat ./bin/shutdown.pid) \ No newline at end of file diff --git a/spring-boot/src/test/java/com/baeldung/shutdown/ApplicationShutdownTest.java b/spring-boot/src/test/java/com/baeldung/shutdown/ApplicationShutdownTest.java deleted file mode 100644 index 599aa697db6c..000000000000 --- a/spring-boot/src/test/java/com/baeldung/shutdown/ApplicationShutdownTest.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.shutdown; - -import org.junit.Test; - -public class ApplicationShutdownTest { - - @Test - public void whenShutDown_thenCloseApplication() { - - } -} From f6a463ae8bcc0211a0bb403f0ab71e588ef311be Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Sun, 18 Mar 2018 13:01:19 +0400 Subject: [PATCH 5/7] /shutdown endpoint test --- .../com/baeldung/shutdown/Application.java | 5 ++- .../shutdown/ShutdownApplicationTest.java | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 spring-boot/src/test/java/com/baeldung/shutdown/ShutdownApplicationTest.java diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java index a4c31be983fc..3da4906836bb 100644 --- a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java +++ b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java @@ -13,7 +13,8 @@ public class Application { public static void main(String[] args) { - closeApplication(); + SpringApplication.run(Application.class, args); +// closeApplication(); // exitApplication(); // writePID(); @@ -35,7 +36,7 @@ private static void exitApplication() { @Override public int getExitCode() { // return the error code - return 500; + return 0; } }); diff --git a/spring-boot/src/test/java/com/baeldung/shutdown/ShutdownApplicationTest.java b/spring-boot/src/test/java/com/baeldung/shutdown/ShutdownApplicationTest.java new file mode 100644 index 000000000000..9462f2026985 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/shutdown/ShutdownApplicationTest.java @@ -0,0 +1,41 @@ +package com.baeldung.shutdown; + +import org.junit.Before; +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.http.MediaType; +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 + public void givenBootApp_whenShutdownEndpoint_thenExit() throws Exception { + + mockMvc.perform( + post("/shutdown")) + .andExpect(status().isOk()); + } +} From 117a751bc7fbbc7431fec2057659fa30d3cb0511 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Sun, 18 Mar 2018 14:13:41 +0400 Subject: [PATCH 6/7] bring back the closeApplication --- .../src/main/java/com/baeldung/shutdown/Application.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java index 3da4906836bb..a75e8074756b 100644 --- a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java +++ b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java @@ -13,8 +13,7 @@ public class Application { public static void main(String[] args) { - SpringApplication.run(Application.class, args); -// closeApplication(); + closeApplication(); // exitApplication(); // writePID(); From 73095bf066882aeacd92ed2fd490cd502d43bd15 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Sun, 18 Mar 2018 14:48:24 +0400 Subject: [PATCH 7/7] @Ignore shutdown endpoint test --- .../src/main/java/com/baeldung/shutdown/Application.java | 5 ++++- .../com/baeldung/shutdown/shutdown/ShutdownController.java | 7 +++++++ .../com/baeldung/shutdown/ShutdownApplicationTest.java | 4 +++- 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 spring-boot/src/main/java/com/baeldung/shutdown/shutdown/ShutdownController.java diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java index a75e8074756b..964e092c10d1 100644 --- a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java +++ b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java @@ -8,12 +8,15 @@ 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) { - closeApplication(); + SpringApplication.run(Application.class, args); +// closeApplication(); // exitApplication(); // writePID(); diff --git a/spring-boot/src/main/java/com/baeldung/shutdown/shutdown/ShutdownController.java b/spring-boot/src/main/java/com/baeldung/shutdown/shutdown/ShutdownController.java new file mode 100644 index 000000000000..b0acc1f422ea --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/shutdown/shutdown/ShutdownController.java @@ -0,0 +1,7 @@ +package com.baeldung.shutdown.shutdown; + +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ShutdownController { +} diff --git a/spring-boot/src/test/java/com/baeldung/shutdown/ShutdownApplicationTest.java b/spring-boot/src/test/java/com/baeldung/shutdown/ShutdownApplicationTest.java index 9462f2026985..7896b9dc096e 100644 --- a/spring-boot/src/test/java/com/baeldung/shutdown/ShutdownApplicationTest.java +++ b/spring-boot/src/test/java/com/baeldung/shutdown/ShutdownApplicationTest.java @@ -1,12 +1,12 @@ 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.http.MediaType; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -15,6 +15,7 @@ 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 @@ -32,6 +33,7 @@ public void setup() { } @Test + @Ignore public void givenBootApp_whenShutdownEndpoint_thenExit() throws Exception { mockMvc.perform(