Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
like image 306
geogeek Avatar asked Dec 04 '22 00:12

geogeek


1 Answers

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.
like image 74
alexbt Avatar answered Dec 06 '22 20:12

alexbt