Friday 27 July 2018

Spring boot: ExitCodeGenerator and Exceptons

Exception classes can implement ExitCodeGenerator interface.

When such an exception is encountered, Spring Boot returns the exit code provided by the implemented getExitCode() method. This only works when this exception is thrown from ‘run’ method of  ApplicationRunner/CommandLineRunner.

Find the below working example.

CustomSpringBootException.java
package com.sample.myApp.exceptions;

import org.springframework.boot.ExitCodeGenerator;

public class CustomSpringBootException extends RuntimeException implements ExitCodeGenerator {

 private static final long serialVersionUID = 1L;

 @Override
 public int getExitCode() {
  System.out.println("Inside \"getExitCode\" method of CustomSpringBootException");
  return 23;
 }

 public CustomSpringBootException(String message) {
  super(message);
 }

}

MyCommandLineRunner.java
package com.sample.myApp.runners;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.sample.myApp.exceptions.CustomSpringBootException;;

@Component
public class MyCommandLineRunner implements CommandLineRunner {

 @Override
 public void run(String... args) throws Exception {

  System.out.println("************************");
  System.out.println("Inside \"MyCommandLineRunner\" and going to throw \"CustomSpringBootException\"");
  System.out.println("************************");

  throw new CustomSpringBootException("Exception thrown from \"MyCommandLineRunner\"");
 }

}

Application.java
package com.sample.myApp;

import org.springframework.boot.ExitCodeEvent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;

@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 ExitCodeEventModel exitCodeEventModelIntance() {
  return new ExitCodeEventModel();
 }

 private static class ExitCodeEventModel {

  public ExitCodeEventModel() {
   System.out.println("Instantiating ExitCodeEventModel object");
  }

  @EventListener
  public void exitCodeEvent(ExitCodeEvent event) {
   System.out.println("*************************************");
   System.out.println("exit code: " + event.getExitCode());
   System.out.println("*************************************");
  }
 }
}


When you ran Application.java, you can able to see below messages in console.
************************
Inside "MyCommandLineRunner" and going to throw "CustomSpringBootException"
************************
Inside "getExitCode" method of CustomSpringBootException
*************************************
exit code: 23
*************************************
2018-05-13 21:03:01.254  INFO 7860 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-05-13 21:03:01.261 ERROR 7860 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
 at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
 at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
 at com.sample.myApp.Application.main(Application.java:14) [classes/:na]
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
 at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_161]
 at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.1.RELEASE.jar:2.0.1.RELEASE]
Caused by: com.sample.myApp.exceptions.CustomSpringBootException: Exception thrown from "MyCommandLineRunner"
 at com.sample.myApp.runners.MyCommandLineRunner.run(MyCommandLineRunner.java:18) ~[classes/:na]
 at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
 ... 10 common frames omitted

2018-05-13 21:03:01.262  INFO 7860 --- [  restartedMain] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6b0b9195: startup date [Sun May 13 21:02:56 IST 2018]; root of context hierarchy
2018-05-13 21:03:01.265  INFO 7860 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

Project structure looks like below.




Previous                                                 Next                                                 Home

No comments:

Post a Comment