5

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();
Andrew Lygin
  • 6,077
  • 1
  • 32
  • 37
geogeek
  • 1,274
  • 3
  • 25
  • 42

2 Answers2

17

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.
Eugene
  • 117,005
  • 15
  • 201
  • 306
alexbt
  • 16,415
  • 6
  • 78
  • 87
  • 1+, as far as I see from the source code, you also need `"management.endpoint.restart.enabled=true", "management.endpoints.web.exposure.include=restart` – Eugene Jun 14 '22 at 12:18
1
  1. Get the json here
@Autowired
private HealthEndpoint healthEndpoint;

public Health getAlive() {
    return healthEndpoint.health();
}
  1. Add custom logic
Andrea Ciccotta
  • 598
  • 6
  • 16