Java Spring引导以编程方式重新启动应用程序
我正在开发一个SpringBoot应用程序,在其中我需要编写Java代码来关闭应用程序本身(包括其线程),然后重新启动它。我尝试了以下两种方法,第一种是用Java发出Linux命令,如下所示:Java Spring引导以编程方式重新启动应用程序,java,spring,spring-boot,Java,Spring,Spring Boot,我正在开发一个SpringBoot应用程序,在其中我需要编写Java代码来关闭应用程序本身(包括其线程),然后重新启动它。我尝试了以下两种方法,第一种是用Java发出Linux命令,如下所示: Process p = Runtime.getRuntime().exec("kill -SIGINT " + pid); 其中pid是应用程序本身的pid。这与按ctrl+c键的效果相同。但是,通过这种方式成功地关闭了应用程序,我不知道如何重新启动它。我尝试使用相同的方法发出另一个命令(如“mvn s
Process p = Runtime.getRuntime().exec("kill -SIGINT " + pid);
其中pid是应用程序本身的pid。这与按ctrl+c键的效果相同。但是,通过这种方式成功地关闭了应用程序,我不知道如何重新启动它。我尝试使用相同的方法发出另一个命令(如“mvn spring boot:run
”,这是我启动应用程序的方式),但由于应用程序已经关闭,因此无法运行
其次,我还尝试调用AbstractApplicationContext的refresh()
方法,如下所示:
AbstractApplicationContext appContext = new AnnotationConfigApplicationContext();
appContext.registerShutdownHook();
appContext.refresh();
但我不认为这种上下文刷新和重新启动是一样的
那么,用Java代码重新启动Spring Boot应用程序的正确方法是什么呢?感谢您的帮助,谢谢 不要只执行kill命令,而是执行一个已经准备好的脚本/程序,终止调用进程并启动应用程序的新实例。不要只执行kill命令,执行已准备好的脚本/程序,终止调用进程并启动应用程序的新实例。DevTools依赖于应用程序上下文的关闭挂钩在重新启动期间关闭它。如果您通过
SpringApplication.setRegisterShutdownHook(false)
禁用了关闭挂钩,它将无法正常工作
另请参见:.DevTools依赖于应用程序上下文的关闭挂钩在重新启动期间关闭它。如果您通过
SpringApplication.setRegisterShutdownHook(false)
禁用了关闭挂钩,它将无法正常工作
另请参见:.您需要将
spring boot starter actuator
和spring cloud
依赖项添加到应用程序中,并使用/restart
端点重新启动应用程序。文件是否:
对于弹簧启动执行器应用,有一些
其他管理端点:
- 发布到/env以更新环境并重新绑定 @配置属性和日志级别
- /刷新以重新加载引导上下文并刷新 @咖啡豆
- /重新启动以关闭ApplicationContext并重新启动它 (默认情况下禁用)
- /暂停和/或继续调用生命周期方法(stop()和 应用程序上下文上的start())
完成后,可以使用
restapi
调用重新启动应用程序(通过restemplate
或curl
)。这将是一种比终止并重新运行应用程序更干净的重新启动应用程序的方法。您需要向应用程序添加spring boot starter actuator
和spring cloud
依赖项,并使用/restart
端点重新启动应用程序。文件是否:
对于弹簧启动执行器应用,有一些
其他管理端点:
- 发布到/env以更新环境并重新绑定 @配置属性和日志级别
- /刷新以重新加载引导上下文并刷新 @咖啡豆
- /重新启动以关闭ApplicationContext并重新启动它 (默认情况下禁用)
- /暂停和/或继续调用生命周期方法(stop()和 应用程序上下文上的start())
restapi
调用重新启动应用程序(通过restemplate
或curl
)。这将是一种比终止并重新运行应用程序更干净的重新启动应用程序的方法。答案补充:
就我而言,我做到了:
1.增加依赖项:
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.cloud:spring-cloud-starter:1.2.4.RELEASE")
import org.springframework.cloud.context.restart.RestartEndpoint;
....
@Autowired
private RestartEndpoint restartEndpoint;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
restartEndpoint.invoke();
}
});
thread.setDaemon(false);
thread.start();
2.自动连线重启点:
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.cloud:spring-cloud-starter:1.2.4.RELEASE")
import org.springframework.cloud.context.restart.RestartEndpoint;
....
@Autowired
private RestartEndpoint restartEndpoint;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
restartEndpoint.invoke();
}
});
thread.setDaemon(false);
thread.start();
3。并调用:
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.cloud:spring-cloud-starter:1.2.4.RELEASE")
import org.springframework.cloud.context.restart.RestartEndpoint;
....
@Autowired
private RestartEndpoint restartEndpoint;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
restartEndpoint.invoke();
}
});
thread.setDaemon(false);
thread.start();
我需要新的线程,因为spring mvc为每个请求创建守护进程线程,并添加到应答:
就我而言,我做到了:
1.增加依赖项:
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.cloud:spring-cloud-starter:1.2.4.RELEASE")
import org.springframework.cloud.context.restart.RestartEndpoint;
....
@Autowired
private RestartEndpoint restartEndpoint;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
restartEndpoint.invoke();
}
});
thread.setDaemon(false);
thread.start();
2.自动连线重启点:
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.cloud:spring-cloud-starter:1.2.4.RELEASE")
import org.springframework.cloud.context.restart.RestartEndpoint;
....
@Autowired
private RestartEndpoint restartEndpoint;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
restartEndpoint.invoke();
}
});
thread.setDaemon(false);
thread.start();
3。并调用:
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.cloud:spring-cloud-starter:1.2.4.RELEASE")
import org.springframework.cloud.context.restart.RestartEndpoint;
....
@Autowired
private RestartEndpoint restartEndpoint;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
restartEndpoint.invoke();
}
});
thread.setDaemon(false);
thread.start();
我需要新线程,因为spring mvc为每个请求创建守护进程线程gstackoverflow的答案不适合我,我的解决方案: 将依赖项添加到build.gradle
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter', version: '2.0.0.RELEASE'
compile("org.springframework.boot:spring-boot-starter-actuator")
自动连线重启点
@Autowired
private RestartEndpoint restartEndpoint;
将此添加到application.properties
management.endpoint.restart.enabled = true
调用
Thread restartThread = new Thread(() -> restartEndpoint.restart());
restartThread.setDaemon(false);
restartThread.start();
gstackoverflow的答案不适用于我,我的解决方案: 将依赖项添加到build.gradle
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter', version: '2.0.0.RELEASE'
compile("org.springframework.boot:spring-boot-starter-actuator")
自动连线重启点
@Autowired
private RestartEndpoint restartEndpoint;
将此添加到application.properties
management.endpoint.restart.enabled = true
调用
Thread restartThread = new Thread(() -> restartEndpoint.restart());
restartThread.setDaemon(false);
restartThread.start();
我们可以像这样使用Spring开发工具的重启器:
org.springframework.boot.devtools.restart.Restarter.getInstance().restart()
我们可以像这样使用Spring开发工具的重启器:
org.springframework.boot.devtools.restart.Restarter.getInstance().restart()
我认为没有合适的方法用Java代码重新启动SpringBoot应用程序。为什么不改为shell脚本呢。此外,服务器端应用程序的设计应该允许它们长时间运行。我认为没有合适的方法用Java代码重新启动Spring boot应用程序。为什么不改为shell脚本呢。此外,服务器端应用程序的设计应允许它们长时间运行。谢谢。您能否详细说明如何使用RestTemplate向/重新启动发送有效的POST请求?我试图对RestTemplate使用postForObject(“/restart”,null,String.class)方法,但得到了一个“java.lang.IllegalArgumentException:URI不是绝对的”异常。我对春靴还不熟悉。谢谢。
curl-X POSThttp://localhost:8080/project-编码/重新启动
或restTemplate.postForObject(“http://localhost:8080/project-code/restart”,null,String.class)
项目代码
取决于您的配置server.servlet.context path=project code
谢谢。您能否详细说明如何使用RestTemplate向/重新启动发送有效的POST请求?我试着用postForObje