From ebf0a3b21b0b62fdf4ce3f6824b86fca535bcfaa Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 19 May 2018 21:29:40 +0300 Subject: [PATCH] add custom shutdown endpoint --- .../com/baeldung/shutdown/Application.java | 3 ++- .../shutdown/shutdown/ShutdownController.java | 20 ++++++++++++++++++- .../src/main/resources/application.properties | 3 +-- 3 files changed, 22 insertions(+), 4 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 2225df8b34..03f16789dc 100644 --- a/spring-boot/src/main/java/com/baeldung/shutdown/Application.java +++ b/spring-boot/src/main/java/com/baeldung/shutdown/Application.java @@ -9,7 +9,7 @@ import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class Application { - + public static void main(String[] args) { SpringApplication.run(Application.class, args); @@ -46,4 +46,5 @@ public class Application { app.build().addListeners(new ApplicationPidFileWriter("./bin/shutdown.pid")); app.run(); } + } 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 index b0acc1f422..6ebe053654 100644 --- a/spring-boot/src/main/java/com/baeldung/shutdown/shutdown/ShutdownController.java +++ b/spring-boot/src/main/java/com/baeldung/shutdown/shutdown/ShutdownController.java @@ -1,7 +1,25 @@ package com.baeldung.shutdown.shutdown; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController -public class ShutdownController { +public class ShutdownController implements ApplicationContextAware { + + private ApplicationContext context; + + @PostMapping("/shutdownContext") + public void shutdownContext() { + ((ConfigurableApplicationContext) context).close(); + } + + @Override + public void setApplicationContext(ApplicationContext ctx) throws BeansException { + this.context = ctx; + + } } diff --git a/spring-boot/src/main/resources/application.properties b/spring-boot/src/main/resources/application.properties index 04a4fbf9de..8c02e528ab 100644 --- a/spring-boot/src/main/resources/application.properties +++ b/spring-boot/src/main/resources/application.properties @@ -9,6 +9,7 @@ spring.jpa.hibernate.ddl-auto = update management.endpoints.jmx.domain=Spring Sample Application management.endpoints.jmx.uniqueNames=true +management.endpoints.web.exposure.include=* management.endpoint.shutdown.enabled=true ##jolokia.config.debug=true @@ -27,8 +28,6 @@ info.app.description=This is my first spring boot application G1 info.app.version=1.0.0 info.java-vendor = ${java.specification.vendor} -management.security.role=SUPERUSER - logging.level.org.springframework=INFO #Servlet Configuration