使用Java函数从Spring Boot调用Spring致动器/restart端点 [英] Call Spring actuator /restart endpoint from Spring boot using a java function

查看:362
本文介绍了使用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:

Web应用程序[xyx]似乎已经启动了一个名为 [Thread-6],但未能停止.这很有可能会创建一个 内存泄漏.线程的堆栈跟踪:

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依赖项.
    • 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端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆