Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically restart Spring Boot application

I'm using Spring Boot and I've got a use case where user can upload a file which should cause a restart of application (since user's upload is used during creation of multiple beans). I know I can avoid restarting the application, but at the moment - this is what I want.

I've found RestartEndpoint in Spring-Cloud project, but it doesn't seem like ApplicationPreparedEvent is fired. Is there any other way I can programmatically restart Spring Boot application?

like image 931
Matija Folnovic Avatar asked Mar 18 '15 08:03

Matija Folnovic


People also ask

How do I restart my spring boot app?

The very basic way to restart the Spring Boot Application is by sending a POST request to the /restart endpoint.

How do I refresh application context in spring boot?

You can use: POST to /actuator/env to update the Environment and rebind @ConfigurationProperties and log levels. /actuator/refresh to re-load the boot strap context and refresh the @RefreshScope beans. /actuator/restart to close the ApplicationContext and restart it (disabled by default).

How do I reload my changes on spring boot without having to restart server?

In Spring Boot this can be achieved by adding a DevTools module, just add the following dependency in your Spring Boots pom. xml and build it. Spring Boot DevTools module does exactly what developers needed, this eliminates the process of manually deploying the changes.


2 Answers

The simplest way to do this by calling the refresh() method on the Spring ApplicationContext. This will kill and reload all of your beans, so you should be certain that this occurs only when it is safe for your application to do so.

like image 148
adam p Avatar answered Oct 11 '22 01:10

adam p


I have a special case in which my Spring Boot application, which runs on Linux, is required to run as root.

Since I run the application as systemd service, I can restart it like this:

Runtime.getRuntime().exec(new String[] { "/bin/systemctl", "restart", "foo" });

If your application is (hopefully) not required to run as root, a setuid wrapper-script could be used, or better, use sudo.

Check this answer on how to do that:

https://superuser.com/questions/440363/can-i-make-a-script-always-execute-as-root

like image 23
yglodt Avatar answered Oct 11 '22 03:10

yglodt