10

I'm trying to use hyst however when calling the save method, which makes a post with resttemplate, gives the following exception:

com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't found: breaker([class com.wnb.mastercard.domain.enroll.EnrollCommand])

Can someone help me?

@Component
public class EnrollRepositoryRest {

    @Autowired
    private RestTemplate template;

    @Value("${beblue-card-enroll.url}")
    private String url;

    public Enroll getEnrollByCardId(String cardId) {

        Enroll[] enroll = template.getForObject(url + "cardEnroll/enroll/" + cardId, Enroll[].class);

        return enroll[0];
    }

    @HystrixCommand(fallbackMethod = "breaker")
    public void save(EnrollCommand command) {
        template.postForObject(url + "/cardEnroll/enroll", command, EnrollCommand.class);
    }

    public String breaker() {
        System.out.println("HYSTRIX EXECUTADO");
        return "Hystrix is Ok";
    }
}
TheOliverDenis
  • 522
  • 6
  • 18
Tiago Costa
  • 1,004
  • 4
  • 17
  • 31

3 Answers3

24

I think the exception is clearly telling you the issue. The method:

public String breaker(EnrollCommand command) {
    System.out.println("HYSTRIX EXECUTADO");
    return "Hystrix is Ok";
}

Does not exist. (Notice the argument in the signature)

When you define a fallback method with that annotation the fallback method must match the same parameters of the method where you define the Hystrix Command.

Ramon Rius
  • 404
  • 3
  • 7
2

@Ramon Rius

Even the return type has to be same as of the 'save' method, in this case it is void.

Without that am getting this error.

ERROR 10340 --- [nio-8060-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : 
Servlet.service() for servlet [dispatcherServlet] in context with path [] 
threw exception [Request processing failed; nested exception is 
com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: 
Incompatible return types. 
Command method: public long com.brownfield.pss.book.component.BookingComponent.book(com.brownfield.pss.book.entity.BookingRecord,java.lang.String);
Fallback method: public java.lang.String com.brownfield.pss.book.component.BookingComponent.fallByMethod(com.brownfield.pss.book.entity.BookingRecord,java.lang.String);
Hint: Fallback method 'public java.lang.String com.brownfield.pss.book.component.BookingComponent.fallByMethod(com.brownfield.pss.book.entity.BookingRecord,java.lang.String)' must return: long or its subclass] with root cause

Thing is both parameters and return type of the fallbackMethod and the main method should be same. The fallbackMethod may have an extra argument of type Throwable

2

Fallback method must have same definition as original method.

private void breaker(EnrollCommand command) {
    System.out.println("HYSTRIX EXECUTADO");
}
Amol Damodar
  • 359
  • 2
  • 4