[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
@@ -1,30 +0,0 @@
|
||||
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
@@ -1,23 +0,0 @@
|
||||
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
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.restart;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.context.restart.RestartEndpoint;
|
||||
|
||||
@Service
|
||||
public class RestartService {
|
||||
|
||||
@Autowired
|
||||
private RestartEndpoint restartEndpoint;
|
||||
|
||||
public void restartApp() {
|
||||
restartEndpoint.restart();
|
||||
}
|
||||
|
||||
}
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
package com.baeldung.restart;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
/**
|
||||
* We have to make sure that while running this test, 8080 and 8090 ports are free.
|
||||
* Otherwise it will fail.
|
||||
*/
|
||||
public class RestartApplicationManualTest {
|
||||
|
||||
private TestRestTemplate restTemplate = new TestRestTemplate();
|
||||
|
||||
@Test
|
||||
public void givenBootApp_whenRestart_thenOk() throws Exception {
|
||||
Application.main(new String[0]);
|
||||
|
||||
ResponseEntity response = restTemplate.exchange("http://localhost:8080/restart",
|
||||
HttpMethod.POST, null, Object.class);
|
||||
|
||||
assertEquals(200, response.getStatusCode().value());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBootApp_whenRestartUsingActuator_thenOk() throws Exception {
|
||||
Application.main(new String[] { "--server.port=8090" });
|
||||
|
||||
ResponseEntity response = restTemplate.exchange("http://localhost:8090/restartApp",
|
||||
HttpMethod.POST, null, Object.class);
|
||||
|
||||
assertEquals(200, response.getStatusCode().value());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user