Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javanica @HystrixCommand and spring @Cacheable execution order

In a Spring Application (not spring-boot), I'm using javanica @HystrixCommand annotation and spring cache @Cacheable annotation on the same bean method, Spring execute the cache advice before the Hystrix advice. It's what I'm waiting, but for me the cache advice and hystrix advice without any configuration have the same order in spring : LOWEST_PRECEDENCE.

I wan't to know what make this order : is it defined somewhere or this an undefined order and I'm lucky to have the good order ? What must be done to ensure that cache advice will be executed before Hystrix advice (set order on cache:annotation-driven before LOWEST_PRECEDENCE ?)

This is an example of my code :

...
import org.springframework.cache.annotation.CacheConfig;

import org.springframework.cache.annotation.Cacheable;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
...


@Service
@Slf4j
@CacheConfig(cacheManager="myCacheManager")
public class MyRessourcesImpl implements MyRessources {
...
    @Override
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000") })
    @Cacheable("cacheName")  //from spring cache
    public Map<String, String> getTheMap() {
        ...
    }
...    
}

With this spring config :

<bean id="myCache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
    <property name="configLocation" value="classpath:/META-INF/...."  />
</bean>

<bean id="myCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="myCache" />
</bean>

<cache:annotation-driven cache-manager="myCacheManager" />

<aop:aspectj-autoproxy/>

<bean id="hystrixAspect" class="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect" />

Thanks for help

like image 544
Christophe L Avatar asked Oct 30 '22 20:10

Christophe L


1 Answers

According to the docs, if you don't specify the order, it is undefined:

When two pieces of advice defined in different aspects both need to run at the same join point, unless you specify otherwise the order of execution is undefined. You can control the order of execution by specifying precedence. This is done in the normal Spring way by either implementing the org.springframework.core.Ordered interface in the aspect class or annotating it with the Order annotation. Given two aspects, the aspect returning the lower value from Ordered.getValue() (or the annotation value) has the higher precedence.

Credit: spring annotation advice order

like image 95
dannymac Avatar answered Dec 05 '22 11:12

dannymac