Friday, 27 July 2018

Spring boot: Registering shutdown hook with the JVM

By implementing 'org.springframework.boot.ExitCodeGenerator' interface, you can return a specific exit code when SpringApplication.exit() is called.

Applicaton.java
package com.sample.myApp;

import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application implements ExitCodeGenerator {

 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  System.out.println("Closing Application Context");

  int exitCode = SpringApplication.exit(applicationContext);

  System.out.printf("Application Context is exited with : %d", exitCode);

 }

 @Override
 public int getExitCode() {
  System.out.println("Returning exit code");
  return 10;
 }
}

When you ran above application, you can able to see below message in the console.

Closing Application Context
Returning exit code
Application Context is exited with : 10

You can also attach exit code generator using @Bean annotation like below.

Application.java
package com.sample.myApp;

import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  System.out.println("Closing Application Context");

  int exitCode = SpringApplication.exit(applicationContext);

  System.out.printf("Application Context is exited with : %d", exitCode);

 }

 @Bean
 public ExitCodeGenerator exitCodeGenerator() {
  return new ExitCodeGenerator() {

   @Override
   public int getExitCode() {
    System.out.println("Returning exit code");
    return 10;
   }

  };
 }

}


You can rewrite the same using lambda expression like below.
package com.sample.myApp;

import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  System.out.println("Closing Application Context");

  int exitCode = SpringApplication.exit(applicationContext);

  System.out.printf("Application Context is exited with : %d", exitCode);

 }

 @Bean
 public ExitCodeGenerator exitCodeGenerator() {
  return () -> {
   System.out.println("Returning exit code");
   return 10;
  };

 }

}

How the application behaves on multiple exit code generators?
In the case of multiple exit codes,  all the exit code generators will be executed, but the highest value will be used (or if all values are negative, the lowest value will be used).
Application.java
package com.sample.myApp;

import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  System.out.println("Closing Application Context");

  int exitCode = SpringApplication.exit(applicationContext);

  System.out.printf("Application Context is exited with : %d", exitCode);

 }

 @Bean
 public ExitCodeGenerator exitCodeGenerator10() {
  return () -> {
   System.out.println("Returning exit code 10");
   return 10;
  };

 }

 @Bean
 public ExitCodeGenerator exitCodeGenerator5() {
  return () -> {
   System.out.println("Returning exit code 5");
   return 5;
  };

 }

 @Bean
 public ExitCodeGenerator exitCodeGenerator35() {
  return () -> {
   System.out.println("Returning exit code 35");
   return 35;
  };

 }
}


Previous                                                 Next                                                 Home

No comments:

Post a Comment