How to Shutdown a Spring Boot Application in a Correct Way

Programmatically shut down Spring Boot application

Closing a SpringApplication basically means closing the underlying ApplicationContext. The SpringApplication#run(String...) method gives you that ApplicationContext as a ConfigurableApplicationContext. You can then close() it yourself.

For example,

@SpringBootApplication
public class Example {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Example.class, args);
// ...determine it's time to shut down...
ctx.close();
}
}

Alternatively, you can use the static SpringApplication.exit(ApplicationContext, ExitCodeGenerator...) helper method to do it for you. For example,

@SpringBootApplication
public class Example {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Example.class, args);
// ...determine it's time to stop...
int exitCode = SpringApplication.exit(ctx, new ExitCodeGenerator() {
@Override
public int getExitCode() {
// no errors
return 0;
}
});

// or shortened to
// int exitCode = SpringApplication.exit(ctx, () -> 0);

System.exit(exitCode);
}
}

How to gracefully shutdown a Spring Boot application?

If you have a ConfigurableApplicationContext, you can use the method registerShutdownHook() to register a shutdown hook with the JVM runtime. You can see more here.
http://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html?is-external=true#addShutdownHook-java.lang.Thread-

You use Springapplication.exit(applicationcontext) to close the application context an finish the Spring application.

How to gracefully shutdown the spring boot application

Ultimately the spring boot application spins off a java process which needs to be killed. At present you are killing it manually.

You have a few options:

  1. You can use SpringApplication.exit(ApplicationContext, ExitCodeGenerator...) method.
  2. If your application is not a long running application then do you have some exit point where your application should stop. At that point you can System.exit(0)
  3. You can use external manager tools, for example on unix you can use supervisor, here is a blog you can read about this.
  4. SpringApplication.run gives you ApplicationContext which you can close.

Using the Spring Boot actuator to shutdown a rest server safely?

The Spring Boot shutdown endpoint invokes this class: org.springframework.boot.actuate.endpoint.ShutdownEndpoint which invokes close() on your ApplicationContext. This in turn ...

  • destroys beans
  • closes the bean factory
  • stops the embedded servlet container

If your beans are ordered and have carefully written shutdown methods, then this should be fine. But if not, then at some point in that shutdown cycle "the jobs that the 3 clients are running" are likely to be interrupted. In addition, it is possible that new connections may be made in the small time window between you invoking shutdown and the shutdown cycle kicking in.

Spring provides application events and listener hooks which allow you to participate in the shutdown cycle. The ContextClosedEvent is published before beans are destroyed, the embedded container is shutdown, etc, so you can use this to implement your own shutdown behaviour before the default behaviour kicks in. For example:

public class ShutdownListener implements ApplicationListener<ContextClosedEvent> {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
//
}
}

You could implement this listener such that it

  • Rejects connection requests with a HTTP 503 (or something equivalent to that if you are not dealing with HTTP requests)
  • Pauses to allow any inflight jobs to complete before continuing with the shutdown cycle

You register this listener in the same way as you register any ApplicationListener in Spring Boot e.g.

SpringApplication app = new SpringApplication(MyApplication.class);
app.addListeners(new ShutdownListener());

Is there any way in spring boot to control shutdown mechanism?

You can achievement using Actuator endpoints then you will have to enable some endpoint on your case is shutdown, in addition I can leave you a link that can help you, it has good information about it.
- > Shutdown a Spring Boot Application
Kind regards.



Related Topics



Leave a reply



Submit