Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Cloud Hystrix (fallback method wasn't found)

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";
    }
}
like image 754
Tiago Costa Avatar asked Feb 01 '17 11:02

Tiago Costa


3 Answers

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.

like image 97
Ramon Rius Avatar answered Oct 28 '22 17:10

Ramon Rius


Fallback method must have same definition as original method.

private void breaker(EnrollCommand command) {
    System.out.println("HYSTRIX EXECUTADO");
}
like image 34
Amol Damodar Avatar answered Oct 28 '22 17:10

Amol Damodar


@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

like image 31
RBuser2769569 Avatar answered Oct 28 '22 16:10

RBuser2769569