Skip to content

Commit c7da8f3

Browse files
authored
Merge pull request eugenp#5893 from mprevisic/master
BAEL-2351 programmatically restart spring boot application
2 parents fd554d0 + 7a23ac4 commit c7da8f3

7 files changed

Lines changed: 127 additions & 2 deletions

File tree

‎spring-boot-ops/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@
8686
<version>${jquery.version}</version>
8787
</dependency>
8888

89+
<dependency>
90+
<groupId>org.springframework.cloud</groupId>
91+
<artifactId>spring-cloud-context</artifactId>
92+
<version>${springcloud.version}</version>
93+
</dependency>
94+
8995
</dependencies>
9096

9197
<build>
@@ -153,6 +159,7 @@
153159
<jpa.version>2.2</jpa.version>
154160
<guava.version>18.0</guava.version>
155161
<subethasmtp.version>3.1.7</subethasmtp.version>
162+
<springcloud.version>2.0.2.RELEASE</springcloud.version>
156163
</properties>
157164

158165
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.restart;
2+
3+
import org.springframework.boot.ApplicationArguments;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
7+
import org.springframework.context.ConfigurableApplicationContext;
8+
9+
@SpringBootApplication
10+
public class Application extends SpringBootServletInitializer {
11+
12+
private static ConfigurableApplicationContext context;
13+
14+
public static void main(String[] args) {
15+
context = SpringApplication.run(Application.class, args);
16+
}
17+
18+
public static void restart() {
19+
ApplicationArguments args = context.getBean(ApplicationArguments.class);
20+
21+
Thread thread = new Thread(() -> {
22+
context.close();
23+
context = SpringApplication.run(Application.class, args.getSourceArgs());
24+
});
25+
26+
thread.setDaemon(false);
27+
thread.start();
28+
}
29+
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.restart;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.web.bind.annotation.PostMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
@RestController
8+
public class RestartController {
9+
10+
@Autowired
11+
private RestartService restartService;
12+
13+
@PostMapping("/restart")
14+
public void restart() {
15+
Application.restart();
16+
}
17+
18+
@PostMapping("/restartApp")
19+
public void restartUsingActuator() {
20+
restartService.restartApp();
21+
}
22+
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.restart;
2+
3+
import org.springframework.stereotype.Service;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.cloud.context.restart.RestartEndpoint;
6+
7+
@Service
8+
public class RestartService {
9+
10+
@Autowired
11+
private RestartEndpoint restartEndpoint;
12+
13+
public void restartApp() {
14+
restartEndpoint.restart();
15+
}
16+
17+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
management.endpoints.web.exposure.include=*
22
management.metrics.enable.root=true
3-
management.metrics.enable.jvm=true
3+
management.metrics.enable.jvm=true
4+
management.endpoint.restart.enabled=true
5+
spring.datasource.jmx-enabled=false
6+
spring.main.allow-bean-definition-overriding=true
7+
management.endpoint.shutdown.enabled=true
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.restart;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.springframework.boot.test.web.client.TestRestTemplate;
9+
import org.springframework.boot.web.client.RestTemplateBuilder;
10+
import org.springframework.http.HttpMethod;
11+
import org.springframework.http.ResponseEntity;
12+
13+
import java.time.Duration;
14+
15+
public class RestartApplicationIntegrationTest {
16+
17+
private TestRestTemplate restTemplate = new TestRestTemplate();
18+
19+
@Test
20+
public void givenBootApp_whenRestart_thenOk() throws Exception {
21+
Application.main(new String[0]);
22+
23+
ResponseEntity response = restTemplate.exchange("http://localhost:8080/restart",
24+
HttpMethod.POST, null, Object.class);
25+
26+
assertEquals(200, response.getStatusCode().value());
27+
}
28+
29+
@Test
30+
public void givenBootApp_whenRestartUsingActuator_thenOk() throws Exception {
31+
Application.main(new String[] { "--server.port=8090" });
32+
33+
ResponseEntity response = restTemplate.exchange("http://localhost:8090/restartApp",
34+
HttpMethod.POST, null, Object.class);
35+
36+
assertEquals(200, response.getStatusCode().value());
37+
}
38+
39+
}

‎spring-boot-ops/src/test/resources/application.properties‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ spring.mail.properties.mail.smtp.auth=false
44

55
management.endpoints.web.exposure.include=*
66
management.endpoint.shutdown.enabled=true
7-
endpoints.shutdown.enabled=true
7+
endpoints.shutdown.enabled=true
8+
9+
management.endpoint.restart.enabled=true
10+
11+
spring.main.allow-bean-definition-overriding=true
12+
spring.jmx.unique-names=true

0 commit comments

Comments
 (0)