Close

Spring Cloud - Hystrix Circuit Breaker, getting failure exception in fallback method

[Updated: Jul 24, 2019, Created: Jul 24, 2019]

Hystrix provides the ability to get the exception thrown that caused the failure of the service. The fallback method can have an additional last Throwable parameter in order to get the exception.

Let's modify our last example to see how to achieve that.

Example

Using @HystrixCommand and the fallback method

package com.logicbig.example;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

@Service
public class MyService {
  @HystrixCommand(fallbackMethod = "defaultDoSomething")
  public void doSomething(int input) {
      System.out.println("output: " + 10 / input);
  }

  @HystrixCommand(fallbackMethod = "defaultDoSomething")
  public void doSomething2(int input) {
      try {
          TimeUnit.MILLISECONDS.sleep(1500);// timeout scenario
      } catch (InterruptedException e) {
          return;
      }
      System.out.println("output: " + 10 / input);
  }

  public void defaultDoSomething(int input, Throwable throwable) {
      System.out.printf("Default, input=%s, exception=%s%n", input, throwable);
  }
}
package com.logicbig.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
@EnableCircuitBreaker
public class CircuitBreakerMain {
  public static void main(String[] args) {
      ConfigurableApplicationContext ctx = SpringApplication.run(CircuitBreakerMain.class, args);
      MyService myService = ctx.getBean(MyService.class);
      System.out.println("-- calling doSomething(2) --");
      myService.doSomething(2);
      System.out.println("-- calling doSomething(0) --");
      myService.doSomething(0);
      System.out.println("-- calling doSomething(5) --");
      myService.doSomething(5);
      System.out.println("-- calling doSomething2(2) --");
      myService.doSomething2(2);
  }
}
-- calling doSomething(2) --
output: 5
-- calling doSomething(0) --
Default, input=0, exception=java.lang.ArithmeticException: / by zero
-- calling doSomething(5) --
output: 2
-- calling doSomething2(2) --
Default, input=2, exception=com.netflix.hystrix.exception.HystrixTimeoutException

Following is the tripping of the circuit scenario:

@SpringBootApplication
@EnableCircuitBreaker
public class CircuitBreakerMain2 {
  public static void main(String[] args) throws InterruptedException {
      ConfigurableApplicationContext ctx = SpringApplication.run(CircuitBreakerMain2.class, args);
      MyService myService = ctx.getBean(MyService.class);

      System.out.println("-- calling doSomething() 40 times --");
      int n = 40;
      for (int i = 0; i < n; i++) {
          myService.doSomething(i < (n * 0.6) ? 0 : 2);
          TimeUnit.MILLISECONDS.sleep(100);
      }
      TimeUnit.SECONDS.sleep(6);

      System.out.println("-- final call --");
      myService.doSomething(2);
  }
}
-- calling doSomething() 40 times --
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.ArithmeticException: / by zero
Default, input=0, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
Default, input=2, exception=java.lang.RuntimeException: Hystrix circuit short-circuited and is OPEN
-- final call --
output: 5

Example Project

Dependencies and Technologies Used:

  • Spring Boot 2.1.6.RELEASE
    Corresponding Spring Version 5.1.8.RELEASE
  • Spring Cloud Greenwich.SR2
  • spring-boot-starter : Core starter, including auto-configuration support, logging and YAML.
  • spring-cloud-starter-netflix-hystrix 2.1.2.RELEASE: Spring Cloud Starter Netflix Hystrix.
  • JDK 1.8
  • Maven 3.5.4

Hystrix - Getting Failure Exception In Fallback Select All Download
  • circuit-breaker-accessing-exception
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • MyService.java

    See Also