diff --git a/hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixRequestVariableHolder.java b/hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixRequestVariableHolder.java index 84bd70d76..54a6dd56d 100644 --- a/hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixRequestVariableHolder.java +++ b/hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixRequestVariableHolder.java @@ -65,7 +65,11 @@ public T get(HystrixConcurrencyStrategy concurrencyStrategy) { } } - return (T) requestVariableInstance.get(key).get(); + T result = (T) requestVariableInstance.get(key).get(); + if (result == null) { + throw new IllegalStateException("Failed to get HystrixRequestVariable. Maybe you need to initialize the HystrixRequestContext?"); + } + return result; } private static class RVCacheKey { diff --git a/hystrix-core/src/test/java/com/netflix/hystrix/HystrixRequestCacheTest.java b/hystrix-core/src/test/java/com/netflix/hystrix/HystrixRequestCacheTest.java index dbc66c172..353ebc2cd 100644 --- a/hystrix-core/src/test/java/com/netflix/hystrix/HystrixRequestCacheTest.java +++ b/hystrix-core/src/test/java/com/netflix/hystrix/HystrixRequestCacheTest.java @@ -66,6 +66,14 @@ public void testCache() { } } + @Test(expected = IllegalStateException.class) + public void testCacheWithoutContext() { + HystrixRequestCache.getInstance( + HystrixCommandKey.Factory.asKey("command1"), + HystrixConcurrencyStrategyDefault.getInstance() + ).get("any"); + } + @Test public void testClearCache() { HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance();