使用Java函数从Spring引导中调用Spring执行器/重启端点
作者:互联网
我想重新启动Spring Boot应用程序,因此使用Spring Actuator / restart端点可以使用curl进行工作,但是我想使用该应用程序内的Java代码来调用相同的函数,我已经尝试过此代码,但是不起作用:
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
RestartEndpoint p = new RestartEndpoint();
p.invoke();
}
});
thread.setDaemon(false);
thread.start();
解决方法:
您需要注入RestartEndPoint:
@Autowired
private RestartEndpoint restartEndpoint;
...
Thread restartThread = new Thread(() -> restartEndpoint.restart());
restartThread.setDaemon(false);
restartThread.start();
即使它会引发异常以通知您这可能会导致内存泄漏,它仍然可以工作:
The web application [xyx] appears to have started a thread named
[Thread-6] but has failed to stop it. This is very likely to create a
memory leak. Stack trace of thread:
>给这个问题/答案的未来读者注意,spring-boot-actuator中不包括RestartEndPoint,您需要添加spring-cloud-context依赖项.
标签:spring-boot-actuator,java,spring,spring-boot,spring-cloud 来源: https://codeday.me/bug/20191014/1912310.html