Boot调用Spring致动器

Boot调用Spring致动器

本文介绍了使用Java函数从Spring Boot调用Spring致动器/restart端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望重新启动Spring Boot应用程序,因此使用Spring Actuator/restart端点可以使用curl进行工作,但是我希望使用该应用程序内部的Java代码调用相同的函数,因此我尝试了此代码,但不起作用:

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

推荐答案

您需要注入RestartEndPoint:

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:

  • 此问题/答案的未来读者请注意,spring-boot-actuator中不包括 RestartEndPoint ,您需要添加spring-cloud-context依赖项.
    • Note to future reader of this question/answer, RestartEndPoint is NOT included in spring-boot-actuator, you need to add spring-cloud-context dependency.
    • 这篇关于使用Java函数从Spring Boot调用Spring致动器/restart端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 11:50