Programmatically restart Spring Boot application / Refresh Spring Context

后端 未结 6 1605
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 15:43

I am trying to programmatically restart my Spring Application without having the user to intervene.

Basically, I have a page which allows to switch the mode of the a

6条回答
  •  佛祖请我去吃肉
    2020-12-03 15:51

    You can use the RestartEndPoint (in spring-cloud-context dependency) to restart the Spring Boot application programmatically:

    @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:

    The same answer was provided for this other question (worded differently): Call Spring actuator /restart endpoint from Spring boot using a java function

提交回复
热议问题