[JAVA-27376] Moved article "Programmatically Restarting a Spring Boot… (#15412)
* [JAVA-27376] Moved article "Programmatically Restarting a Spring Boot Application" to spring-boot-runtime-2 module * [JAVA-27367]
This commit is contained in:
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.restart;
|
||||
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application extends SpringBootServletInitializer {
|
||||
|
||||
private static ConfigurableApplicationContext context;
|
||||
|
||||
public static void main(String[] args) {
|
||||
context = SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
public static void restart() {
|
||||
ApplicationArguments args = context.getBean(ApplicationArguments.class);
|
||||
|
||||
Thread thread = new Thread(() -> {
|
||||
context.close();
|
||||
context = SpringApplication.run(Application.class, args.getSourceArgs());
|
||||
});
|
||||
|
||||
thread.setDaemon(false);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.restart;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class RestartController {
|
||||
|
||||
@Autowired
|
||||
private RestartService restartService;
|
||||
|
||||
@PostMapping("/restart")
|
||||
public void restart() {
|
||||
Application.restart();
|
||||
}
|
||||
|
||||
@PostMapping("/restartApp")
|
||||
public void restartUsingActuator() {
|
||||
restartService.restartApp();
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.restart;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.context.restart.RestartEndpoint;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class RestartService {
|
||||
|
||||
@Autowired
|
||||
private RestartEndpoint restartEndpoint;
|
||||
|
||||
public void restartApp() {
|
||||
restartEndpoint.restart();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
management.endpoints.web.exposure.include=*
|
||||
management.metrics.enable.root=true
|
||||
management.metrics.enable.jvm=true
|
||||
management.endpoint.restart.enabled=true
|
||||
spring.datasource.jmx-enabled=false
|
||||
spring.jmx.enabled=true
|
||||
management.endpoint.shutdown.enabled=true
|
||||
Reference in New Issue
Block a user