public abstract class HystrixConcurrencyStrategy
extends java.lang.Object
For example, every Callable executed by HystrixCommand will call wrapCallable(Callable) to give a chance for custom implementations to decorate the Callable with
additional behavior.
When you implement a concrete HystrixConcurrencyStrategy, you should make the strategy idempotent w.r.t ThreadLocals.
Since the usage of threads by Hystrix is internal, Hystrix does not attempt to apply the strategy in an idempotent way.
Instead, you should write your strategy to work idempotently. See https://github.com/Netflix/Hystrix/issues/351 for a more detailed discussion.
See HystrixPlugins or the Hystrix GitHub Wiki for information on configuring plugins: https://github.com/Netflix/Hystrix/wiki/Plugins.
| Constructor and Description |
|---|
HystrixConcurrencyStrategy() |
| Modifier and Type | Method and Description |
|---|---|
java.util.concurrent.BlockingQueue<java.lang.Runnable> |
getBlockingQueue(int maxQueueSize)
Factory method to provide instance of
BlockingQueue<Runnable> used for each ThreadPoolExecutor as constructed in getThreadPool(com.netflix.hystrix.HystrixThreadPoolKey, com.netflix.hystrix.strategy.properties.HystrixProperty<java.lang.Integer>, com.netflix.hystrix.strategy.properties.HystrixProperty<java.lang.Integer>, com.netflix.hystrix.strategy.properties.HystrixProperty<java.lang.Integer>, java.util.concurrent.TimeUnit, java.util.concurrent.BlockingQueue<java.lang.Runnable>). |
<T> HystrixRequestVariable<T> |
getRequestVariable(HystrixRequestVariableLifecycle<T> rv)
Factory method to return an implementation of
HystrixRequestVariable that behaves like a ThreadLocal except that it
is scoped to a request instead of a thread. |
java.util.concurrent.ThreadPoolExecutor |
getThreadPool(HystrixThreadPoolKey threadPoolKey,
HystrixProperty<java.lang.Integer> corePoolSize,
HystrixProperty<java.lang.Integer> maximumPoolSize,
HystrixProperty<java.lang.Integer> keepAliveTime,
java.util.concurrent.TimeUnit unit,
java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue)
Factory method to provide
ThreadPoolExecutor instances as desired. |
<T> java.util.concurrent.Callable<T> |
wrapCallable(java.util.concurrent.Callable<T> callable)
Provides an opportunity to wrap/decorate a
Callable<T> before execution. |
public java.util.concurrent.ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey, HystrixProperty<java.lang.Integer> corePoolSize, HystrixProperty<java.lang.Integer> maximumPoolSize, HystrixProperty<java.lang.Integer> keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue)
ThreadPoolExecutor instances as desired.
Note that the corePoolSize, maximumPoolSize and keepAliveTime values will be dynamically set during runtime if their values change using the ThreadPoolExecutor.setCorePoolSize(int),
ThreadPoolExecutor.setMaximumPoolSize(int) and ThreadPoolExecutor.setKeepAliveTime(long, java.util.concurrent.TimeUnit) methods.
Default Implementation
Implementation using standard java.util.concurrent.ThreadPoolExecutor
threadPoolKey - HystrixThreadPoolKey representing the HystrixThreadPool that this ThreadPoolExecutor will be used for.corePoolSize - Core number of threads requested via properties (or system default if no properties set).maximumPoolSize - Max number of threads requested via properties (or system default if no properties set).keepAliveTime - Keep-alive time for threads requested via properties (or system default if no properties set).unit - TimeUnit corresponding with keepAliveTimeworkQueue - BlockingQueue<Runnable> as provided by getBlockingQueue(int)ThreadPoolExecutorpublic java.util.concurrent.BlockingQueue<java.lang.Runnable> getBlockingQueue(int maxQueueSize)
BlockingQueue<Runnable> used for each ThreadPoolExecutor as constructed in getThreadPool(com.netflix.hystrix.HystrixThreadPoolKey, com.netflix.hystrix.strategy.properties.HystrixProperty<java.lang.Integer>, com.netflix.hystrix.strategy.properties.HystrixProperty<java.lang.Integer>, com.netflix.hystrix.strategy.properties.HystrixProperty<java.lang.Integer>, java.util.concurrent.TimeUnit, java.util.concurrent.BlockingQueue<java.lang.Runnable>).
Note: The maxQueueSize value is provided so any type of queue can be used but typically an implementation such as SynchronousQueue without a queue (just a handoff) is preferred as
queueing is an anti-pattern to be purposefully avoided for latency tolerance reasons.
Default Implementation
Implementation returns SynchronousQueue when maxQueueSize <= 0 or LinkedBlockingQueue when maxQueueSize > 0.
maxQueueSize - The max size of the queue requested via properties (or system default if no properties set).BlockingQueue<Runnable>public <T> java.util.concurrent.Callable<T> wrapCallable(java.util.concurrent.Callable<T> callable)
Callable<T> before execution.
This can be used to inject additional behavior such as copying of thread state (such as ThreadLocal).
Default Implementation
Pass-thru that does no wrapping.
callable - Callable<T> to be executed via a ThreadPoolExecutorCallable<T> either as a pass-thru or wrapping the one givenpublic <T> HystrixRequestVariable<T> getRequestVariable(HystrixRequestVariableLifecycle<T> rv)
HystrixRequestVariable that behaves like a ThreadLocal except that it
is scoped to a request instead of a thread.
For example, if a request starts with an HTTP request and ends with the HTTP response, then HystrixRequestVariable should
be initialized at the beginning, available on any and all threads spawned during the request and then cleaned up once the HTTP request is completed.
If this method is implemented it is generally necessary to also implemented wrapCallable(Callable) in order to copy state
from parent to child thread.
rv - HystrixRequestVariableLifecycle with lifecycle implementations from HystrixHystrixRequestVariable<T>