Is it possible to restart a springboot application
In this tutorial we are going to learn about the possibilities of restarting a springboot application.
Spring boot doesn’t do any assumptions about the environment it runs in. So when spring boot process gets shut down, re-starting it again is “out of competence” of spring boot infrastructure which is just a bunch of java classes running inside a JVM process.
You can find Here a list of endpoints exposed by the spring boot. There is a “shutdown” method that you’ve mentioned there, but there is no “restart” functionality exposed.
Now there are other techniques that probably can help: If the application gets shut down because of some illegal state of some spring bean, maybe it makes sense to expose some endpoint that will “clean up” the state and make application operational again. If the application has to be restarted due to changes in configuration files or something, then you might want to consider using spring cloud’s Refresh Scope for Beans. It’s kind of hard to provide more information here, because you haven’t mentioned the reason for shutting down the application, but I guess you’ve got the direction.
Having said that, there are probably some different ways to achieve what you want depending on the environment your application runs in:
- If you’re running in AWS for example, you can take advantage of their autoscaling policies, shut down the application remotely and AWS will run another instance for you. I’m not an expert in AWS, but I saw this working in ECS for example.
- If you’re running “java -jar” just on some server and want to make sure that when your process ends (by using ‘shutdown’) it should be started again, its possible to use some kind of wrapper that would wrap the process in service and track the service availability. There are even ready solutions for this, like Tanuki wrapper (I’m not affiliated with this product but used once its free version and it served us great)
- If you’re using Docker infrastructure you can change the policy and restart the container automatically when it gets shut down, I haven’t used this by myself, but according to This excellent blog post is perfectly doable.