HystrixRequestCache always get empty cacheInstance
I try to use Hystrix with hystrix-javanica. add those dependency to my pom.
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-request-servlet</artifactId>
<version>1.5.3</version>
</dependency>
and config filter in my web.xml
<filter>
<display-name>HystrixRequestContextServletFilter</display-name>
<filter-name>HystrixRequestContextServletFilter</filter-name>
<filter-class>com.netflix.hystrix.contrib.requestservlet.HystrixRequestContextServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HystrixRequestContextServletFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
add annotation on my method.
@CacheResult
@HystrixCommand(groupKey = "PoiService", commandKey = "GetTopPoiByDestinationId", fallbackMethod = "getTopPoiByDestinationIdFallback",
commandProperties = {
@HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500") },
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "30"),
@HystrixProperty(name = "maxQueueSize", value = "101"),
@HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "15")
}
)
public List<ImageModel> getTopPoiImageByDestinationId(@CacheKey Integer destinationId, @CacheKey CateEnum category) {
But the cache not work. I find in this method.
/**
* Retrieve a cached Future for this request scope if a matching command has already been executed/queued.
*
* @return {@code Future<T>}
*/
// suppressing warnings because we are using a raw Future since it's in a heterogeneous ConcurrentHashMap cache
@SuppressWarnings({ "unchecked" })
/* package */<T> HystrixCachedObservable<T> get(String cacheKey) {
ValueCacheKey key = getRequestCacheKey(cacheKey);
if (key != null) {
ConcurrentHashMap<ValueCacheKey, HystrixCachedObservable<?>> cacheInstance = requestVariableForCache.get(concurrencyStrategy);
if (cacheInstance == null) {
throw new IllegalStateException("Request caching is not available. Maybe you need to initialize the HystrixRequestContext?");
}
/* look for the stored value */
return (HystrixCachedObservable<T>) cacheInstance.get(key);
}
return null;
}
cacheInstance is always empty.
This is an example that wires together the RequestContext servlet filter with some Hystrix commands. Can you take a look at this and see where your project differs?
https://github.com/Netflix/Hystrix/tree/master/hystrix-examples-webapp
I choose every branches in this project,there are no code in this project.
https://github.com/Netflix/Hystrix/tree/master/hystrix-examples-webapp
If you checkout the master branch of this project, cd to hystrix-examples-webapp, then run ../gradlew jettyRun, you'll get a server that runs a series of Hystrix commands every time you hit the proper localhost endpoint. The code is all in that module: https://github.com/Netflix/Hystrix/tree/master/hystrix-examples-webapp/src/main/webapp
I see. I was try to use hystrix with annotation. So I import hystrix-javanica to my project.I think is annotation problem. In those example you extands HystrixCommand.
I'm not saying javanica is the problem. It's widely used and well-supported. But I don't know where your problem is with the level of detail you've provided. So I would start with the example in hystrix-examples-webapp, get that working, and then add javanica.
Hi, I write a example to provided more detail. https://github.com/wangweiqing/hystrix-javanica-test If you have time, you can have a look.
If you look at the for kI just made and run it, you can see that HystrixRequestContext is getting set (as expected). So it looks like everything works the way I would expect.
Sorry I don't know what do you mean about "look at the for kl just made and run it". In my case, HystrixRequestContext is getting set. I think the cache would be work with the same cache key. I think if I run a HystrixCommand with the same param, It would not run HystrixCommand's run method. AKA If the method has @HystrixCommand and @CacheResult. It will not be invoked with the same param. Is that right?
Sorry, was typing too quickly and forgot to paste the link I meant to. https://github.com/mattrjacobs/hystrix-javanica-test is a fork I made of your repo. I added a log that confirms the HystrixRequestContext is getting set.
Your understanding is correct - if two commands with the same cache key execute in the same request, only 1 actually executes the run() method.
Closing due to inactivity. Please re-open if there's more to discuss.