Saturday, 28 July 2018

Spring boot: mapping exit codes and exceptions

ExitCodeExceptionMapper is a strategy interface, that can be used to provide a mapping between exceptions and exit codes.

Example
         @Bean
         ExitCodeExceptionMapper exitCodeExceptionMapper() {
                  return exception -> {

                           if (exception.getCause() instanceof CustomSpringBootException) {
                                    System.out.println("I am in Exception mapper");
                                    return 10;
                           } else if (exception.getCause() instanceof ArrayIndexOutOfBoundsException) {
                                    return 11;
                           }
                           return 1;
                  };
         }

Find the below working application.

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

public class CustomSpringBootException extends RuntimeException {

 private static final long serialVersionUID = 1L;

 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.ExitCodeExceptionMapper;
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;

import com.sample.myApp.exceptions.CustomSpringBootException;

@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
 ExitCodeExceptionMapper exitCodeExceptionMapper() {
  return exception -> {

   if (exception.getCause() instanceof CustomSpringBootException) {
    System.out.println("I am in Exception mapper");
    return 10;
   } else if (exception.getCause() instanceof ArrayIndexOutOfBoundsException) {
    return 11;
   }
   return 1;
  };
 }

 @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("*************************************");
  }
 }
} 


Output
************************
Inside "MyCommandLineRunner" and going to throw "CustomSpringBootException"
************************
I am in Exception mapper
*************************************
exit code: 10
*************************************

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-05-14 08:20:52.940 ERROR 10824 --- [  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:17) [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

Project structure looks like below.







Previous                                                 Next                                                 Home

No comments:

Post a Comment