Call Spring actuator /restart endpoint from Spring boot using a java function

社会主义新天地 提交于 2019-12-02 13:20:16

问题


I'm looking to restart the spring boot app, so using Spring Actuator /restart endpoint is working using curl, but i'm looking to call the same function using java code from inside the app, i've tried this code, but it's not working:

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        RestartEndpoint p = new RestartEndpoint();
        p.invoke();
    }
});
thread.setDaemon(false);
thread.start();

回答1:


You need to inject the RestartEndPoint:

@Autowired
private RestartEndpoint restartEndpoint;

...

Thread restartThread = new Thread(() -> restartEndpoint.restart());
restartThread.setDaemon(false);
restartThread.start();

It works, even though it will throw an exception to inform you that this may lead to memory leaks:

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:

  • Note to future reader of this question/answer, RestartEndPoint is NOT included in spring-boot-actuator, you need to add spring-cloud-context dependency.


来源:https://stackoverflow.com/questions/38850762/call-spring-actuator-restart-endpoint-from-spring-boot-using-a-java-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!