T
- Type to be stored on the HystrixRequestVariable
Example 1: HystrixRequestVariable<ConcurrentHashMap<String, DataObject>>
Example 2: HystrixRequestVariable<PojoThatIsThreadSafe>
public class HystrixRequestVariableDefault<T> extends java.lang.Object implements HystrixRequestVariable<T>
HystrixRequestVariable
. Similar to ThreadLocal
but scoped at the user request level. Context is managed via HystrixRequestContext
.
All statements below assume that child threads are spawned and initialized with the use of HystrixContextCallable
or HystrixContextRunnable
which capture state from a parent thread
and propagate to the child thread.
Characteristics that differ from ThreadLocal:
HystrixRequestContext.initializeContext()
HystrixRequestContext.shutdown()
which execute remove()
for each
HystrixRequestVariableshutdown(T)
lifecycle method that gets called at the end of every user request (invoked when HystrixRequestContext.shutdown()
is called) to allow for
resource cleanup.HystrixRequestContext.getContextForCurrentThread()
and HystrixRequestContext.setContextOnCurrentThread(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext)
functionality.shutdown(T)
method.Note on thread-safety: By design a HystrixRequestVariables is intended to be accessed by all threads in a user request, thus anything stored in a HystrixRequestVariables must be thread-safe and plan on being accessed/mutated concurrently.
For example, a HashMap would likely not be a good choice for a RequestVariable value, but ConcurrentHashMap would.
Constructor and Description |
---|
HystrixRequestVariableDefault()
Creates a new HystrixRequestVariable that will exist across all threads
within a
HystrixRequestContext |
Modifier and Type | Method and Description |
---|---|
T |
get()
Get the current value for this variable for the current request context.
|
T |
initialValue()
Computes the initial value of the HystrixRequestVariable in a request.
|
void |
remove()
Removes the value of the HystrixRequestVariable from the current request.
|
void |
set(T value)
Sets the value of the HystrixRequestVariable for the current request context.
|
void |
shutdown(T value)
Provide life-cycle hook for a HystrixRequestVariable implementation to perform cleanup
before the HystrixRequestVariable is removed from the current thread.
|
public HystrixRequestVariableDefault()
HystrixRequestContext
public T get()
get
in interface HystrixRequestVariable<T>
public T initialValue()
This is called the first time the value of the HystrixRequestVariable is fetched in a request. Override this to provide an initial value for a HystrixRequestVariable on each request on which it is used. The default implementation returns null.
initialValue
in interface HystrixRequestVariableLifecycle<T>
public void set(T value)
Note, if a value already exists, the set will result in overwriting that value. It is up to the caller to ensure the existing value is cleaned up. The shutdown(T)
method will not be
called
value
- the value to setpublic void remove()
This will invoke shutdown(T)
if implemented.
If the value is subsequently fetched in the thread, the initialValue()
method will be called again.
public void shutdown(T value)
This is executed at the end of each user request when HystrixRequestContext.shutdown()
is called or whenever remove()
is invoked.
By default does nothing.
NOTE: Do not call get()
from within this method or initialValue()
will be invoked again. The current value is passed in as an argument.
shutdown
in interface HystrixRequestVariableLifecycle<T>
value
- the value of the HystrixRequestVariable being removed