File tree Expand file tree Collapse file tree
java/com/baeldung/restart
java/com/baeldung/restart Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 >
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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11management.endpoints.web.exposure.include =*
22management.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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -4,4 +4,9 @@ spring.mail.properties.mail.smtp.auth=false
44
55management.endpoints.web.exposure.include =*
66management.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
You can’t perform that action at this time.
0 commit comments