Search code examples
javaspringhystrix

Spring Hystrix not triggered on inner methods


I'm trying to include Hystrix circuit breaker in a spring boot application. My Application is a standard spring boot 1.4.1 with spring-cloud-hystrix v 1.2.0 app with a single controller class which calls a service class with an "aggregate" method. This method internally calls two services using inner private methods.

If I annotate the "aggregate" method with @HystrixCommand everithing works fine, but if a annotate the other inner methods Hystrix seems to is not be triggered

This is my service class:

@Service
public class AggregationService {

    @Autowired
    Service1 service1Client;

    @Autowired
    Service2 service2Client;

    private static final String QUERY = "query";
    private static final Logger log = LogManager.getLogger();
    private final ObjectMapper objectMapper = new ObjectMapper();

    //@HystrixCommand(
    //    fallbackMethod = "emptyResult",
    //    groupKey = "aggregation-service",
    //    commandKey = "aggregate")
    public AggregationResponse aggregate(final AggregationParams params)
            throws ApiException, IOException {

        final String query = queryExplain(params);
        final WrapperQueryBuilder wrappedQuery = QueryBuilders.wrapperQuery(query);
        final SearchResponse aggregationResult = searchAggregation(params, wrappedQuery);

        return toDtoResponse(aggregationResult.getAggregations().get(params.getAggregationField().name().toLowerCase()));
    }

    @HystrixCommand(
        fallbackMethod = "emptyAggregationResult",
        groupKey = "aggregation-service",
        commandKey = "searchAggregation")
    private SearchResponse searchAggregation(final AggregationParams params, final WrapperQueryBuilder query) {

        return ... do something with service 2 ....
    }

    //    @HystrixCommand(
    //        fallbackMethod = "rethrowTimeoutException",
    //        groupKey = "aggregation-service",
    //        commandKey = "query-for-aggregation",
    //        ignoreExceptions = TimeoutException.class)
    private String queryExplain(final AggregationParams params) throws ApiException, IOException {
        final String queryAsString = ... do something with service 1 ....
    }

    private String rethrowTimeoutException(final AggregationParams params, final Throwable e) {
        log.error("On Hystrix fallback because of ", e);
        return null;
    }

    private SearchResponse emptyAggregationResult(final AggregationParams params, final WrapperQueryBuilder query, final Throwable e) {
        log.error("On Hystrix fallback because of ", e);
        return null;
    }

}

My controller method is :

 @GetMapping("{field}")
    @ResponseStatus(HttpStatus.OK)
    public AggregationResponse aggregate(... some params ...) throws ApiException, IOException {

... omissis ....

        return aggregationService.aggregate(params);

My configuration class has these annotations :

@Configuration
@EnableHystrix

My application.properties contains:

hystrix.command.searchAggregation.execution.isolation.thread.timeoutInMilliseconds=1
hystrix.command.searchAggregation.circuitBreaker.errorThresholdPercentage=10
hystrix.command.queryExplain.execution.isolation.thread.timeoutInMilliseconds=1
hystrix.command.queryExplain.circuitBreaker.errorThresholdPercentage=10
hystrix.command.default.execution.timeout.enabled=true

I've intentionally kept the timeout for the execution isolation to 1MS in order to catch the hystrix execution.

The strange thing is that only the @HystrixCommand placed on the "aggergate" method seems to be triggered while the others not so if i comment the annotation on top "aggregate" no timeout error is triggered, if i uncomment the annotion a "missing fallback exeption" is triggered

I'm asking what i've missconfigured? Must @HystrixCommand be placed only on "called" method and can not be used onto internals ones?

Hope that my problem is clear (because to me is pretty obscure :) )

Thanks in advance


Solution

  • Javanica uses the HystrixCommandAspect aspect to detect methods annotated with the HystrixCommand annotation, and it seems that the pointcut defined affects only public methods.

    UPDATE: there is a bug report related to this specific issue.