public class HystrixRequestContext
extends java.lang.Object
implements java.io.Closeable
HystrixRequestVariableDefault objects that provide request scoped (rather than only thread scoped) variables so that multiple threads within
a
single request can share state:
HystrixRequestCache for de-duping HystrixCommand executionsHystrixRequestLogHystrixCommand executions within the scope of a request as in HystrixCollapser
If those features are not used then this does not need to be used. If those features are used then this must be initialized or a custom implementation of HystrixRequestVariable must be
returned from HystrixConcurrencyStrategy.getRequestVariable(com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableLifecycle<T>).
If HystrixRequestVariableDefault is used (directly or indirectly by above-mentioned features) and this context has not been initialized then an IllegalStateException will be thrown
with a
message such as:
HystrixRequestContext.initializeContext() must be called at the beginning of each request before RequestVariable functionality can be used.
Example ServletFilter for initializing HystrixRequestContext at the beginning of an HTTP request and shutting down at the end:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HystrixRequestContext context = HystrixRequestContext.initializeContext();
try {
chain.doFilter(request, response);
} finally {
context.shutdown();
}
}
You can find an implementation at hystrix-contrib/hystrix-request-servlet on GitHub.
NOTE: If initializeContext() is called then shutdown() must also be called or a memory leak will occur.
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Shutdown
HystrixRequestVariableDefault objects in this context. |
static HystrixRequestContext |
getContextForCurrentThread() |
static HystrixRequestContext |
initializeContext()
Call this at the beginning of each request (from parent thread)
to initialize the underlying context so that
HystrixRequestVariableDefault can be used on any children threads and be accessible from
the parent thread. |
static boolean |
isCurrentThreadInitialized() |
static void |
setContextOnCurrentThread(HystrixRequestContext state) |
void |
shutdown()
Shutdown
HystrixRequestVariableDefault objects in this context. |
public static boolean isCurrentThreadInitialized()
public static HystrixRequestContext getContextForCurrentThread()
public static void setContextOnCurrentThread(HystrixRequestContext state)
public static HystrixRequestContext initializeContext()
HystrixRequestVariableDefault can be used on any children threads and be accessible from
the parent thread.
NOTE: If this method is called then shutdown() must also be called or a memory leak will occur.
See class header JavaDoc for example Servlet Filter implementation that initializes and shuts down the context.
public void shutdown()
HystrixRequestVariableDefault objects in this context.
NOTE: This must be called if initializeContext() was called or a memory leak will occur.
public void close()
HystrixRequestVariableDefault objects in this context.
NOTE: This must be called if initializeContext() was called or a memory leak will occur.
This method invokes shutdown()
close in interface java.lang.AutoCloseableclose in interface java.io.Closeable