Merge pull request #5893 from mprevisic/master

BAEL-2351 programmatically restart spring boot application
This commit is contained in:
Eric Martin 2018-12-14 19:04:29 -06:00 committed by GitHub
commit c7da8f3183
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 127 additions and 2 deletions

View File

@ -86,6 +86,12 @@
<version>${jquery.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>${springcloud.version}</version>
</dependency>
</dependencies>
<build>
@ -153,6 +159,7 @@
<jpa.version>2.2</jpa.version>
<guava.version>18.0</guava.version>
<subethasmtp.version>3.1.7</subethasmtp.version>
<springcloud.version>2.0.2.RELEASE</springcloud.version>
</properties>
</project>

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,17 @@
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();
}
}

View File

@ -1,3 +1,7 @@
management.endpoints.web.exposure.include=*
management.metrics.enable.root=true
management.metrics.enable.jvm=true
management.metrics.enable.jvm=true
management.endpoint.restart.enabled=true
spring.datasource.jmx-enabled=false
spring.main.allow-bean-definition-overriding=true
management.endpoint.shutdown.enabled=true

View File

@ -0,0 +1,39 @@
package com.baeldung.restart;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import java.time.Duration;
public class RestartApplicationIntegrationTest {
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());
}
}

View File

@ -4,4 +4,9 @@ spring.mail.properties.mail.smtp.auth=false
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
endpoints.shutdown.enabled=true
endpoints.shutdown.enabled=true
management.endpoint.restart.enabled=true
spring.main.allow-bean-definition-overriding=true
spring.jmx.unique-names=true