/** * Retrieve instance of {@link HystrixConcurrencyStrategy} to use based on order of precedence as defined in {@link HystrixPlugins} class header. * <p> * Override default by using {@link #registerConcurrencyStrategy(HystrixConcurrencyStrategy)} or setting property (via Archaius): <code>hystrix.plugin.HystrixConcurrencyStrategy.implementation</code> with the * full classname to load. * * @return {@link HystrixConcurrencyStrategy} implementation to use */ public HystrixConcurrencyStrategy getConcurrencyStrategy() { if (concurrencyStrategy.get() == null) { // check for an implementation from Archaius first Object impl = getPluginImplementation(HystrixConcurrencyStrategy.class); if (impl == null) { // nothing set via Archaius so initialize with default concurrencyStrategy.compareAndSet(null, HystrixConcurrencyStrategyDefault.getInstance()); // we don't return from here but call get() again in case of thread-race so the winner will always get returned } else { // we received an implementation from Archaius so use it concurrencyStrategy.compareAndSet(null, (HystrixConcurrencyStrategy) impl); } } return concurrencyStrategy.get(); }
@Test(expected = IllegalStateException.class) public void testCacheWithoutContext() { HystrixRequestCache.getInstance( HystrixCommandKey.Factory.asKey("command1"), HystrixConcurrencyStrategyDefault.getInstance() ).get("any"); }
/** * Clears the cache for a given cacheKey context. * * @param context the runtime information about an intercepted method invocation for a method * annotated with {@link CacheRemove} annotation */ public void clearCache(CacheInvocationContext<CacheRemove> context) { HystrixCacheKeyGenerator defaultCacheKeyGenerator = HystrixCacheKeyGenerator.getInstance(); String cacheName = context.getCacheAnnotation().commandKey(); HystrixGeneratedCacheKey hystrixGeneratedCacheKey = defaultCacheKeyGenerator.generateCacheKey(context); String key = hystrixGeneratedCacheKey.getCacheKey(); HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey(cacheName), HystrixConcurrencyStrategyDefault.getInstance()).clear(key); } }
@Test public void testCacheWithoutRequestContext() { HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance(); //HystrixRequestContext context = HystrixRequestContext.initializeContext(); try { HystrixRequestCache cache1 = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy); //this should fail, as there's no HystrixRequestContext instance to place the cache into cache1.putIfAbsent("valueA", new TestObservable("a1")); fail("should throw an exception on cache put"); } catch (Exception e) { //expected e.printStackTrace(); } }
@Test public void testClearCache() { HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance(); HystrixRequestContext context = HystrixRequestContext.initializeContext(); try { HystrixRequestCache cache1 = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy); cache1.putIfAbsent("valueA", new TestObservable("a1")); assertEquals("a1", cache1.get("valueA").toObservable().toBlocking().last()); cache1.clear("valueA"); assertNull(cache1.get("valueA")); } catch (Exception e) { fail("Exception: " + e.getMessage()); e.printStackTrace(); } finally { context.shutdown(); } }
@Test public void testCache() { HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance(); HystrixRequestContext context = HystrixRequestContext.initializeContext(); try {
/** * Retrieve instance of {@link HystrixConcurrencyStrategy} to use based on order of precedence as defined in {@link HystrixPlugins} class header. * <p> * Override default by using {@link #registerConcurrencyStrategy(HystrixConcurrencyStrategy)} or setting property (via Archaius): <code>hystrix.plugin.HystrixConcurrencyStrategy.implementation</code> with the * full classname to load. * * @return {@link HystrixConcurrencyStrategy} implementation to use */ public HystrixConcurrencyStrategy getConcurrencyStrategy() { if (concurrencyStrategy.get() == null) { // check for an implementation from Archaius first Object impl = getPluginImplementation(HystrixConcurrencyStrategy.class); if (impl == null) { // nothing set via Archaius so initialize with default concurrencyStrategy.compareAndSet(null, HystrixConcurrencyStrategyDefault.getInstance()); // we don't return from here but call get() again in case of thread-race so the winner will always get returned } else { // we received an implementation from Archaius so use it concurrencyStrategy.compareAndSet(null, (HystrixConcurrencyStrategy) impl); } } return concurrencyStrategy.get(); }
/** * Allow the cache to be flushed for this object. * * @param id * argument that would normally be passed to the command */ public static void flushCache(int id) { HystrixRequestCache.getInstance(GETTER_KEY, HystrixConcurrencyStrategyDefault.getInstance()).clear(String.valueOf(id)); }
public void evictCache(String name) { HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("HelloWorld"), HystrixConcurrencyStrategyDefault.getInstance()).clear(name); } }
static HystrixStrategies getDefault() { return new HystrixStrategies(HystrixPropertiesStrategyDefault.getInstance(), HystrixConcurrencyStrategyDefault.getInstance(), HystrixEventNotifierDefault.getInstance(), "default"); }
/** * Clears the cache for a given cacheKey context. * * @param context the runtime information about an intercepted method invocation for a method * annotated with {@link CacheRemove} annotation */ public void clearCache(CacheInvocationContext<CacheRemove> context) { HystrixCacheKeyGenerator defaultCacheKeyGenerator = HystrixCacheKeyGenerator.getInstance(); String cacheName = context.getCacheAnnotation().commandKey(); HystrixGeneratedCacheKey hystrixGeneratedCacheKey = defaultCacheKeyGenerator.generateCacheKey(context); String key = hystrixGeneratedCacheKey.getCacheKey(); HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey(cacheName), HystrixConcurrencyStrategyDefault.getInstance()).clear(key); } }