Hystrix: Latency and Fault Tolerance for Distributed Systems



com.netflix.hystrix.strategy.concurrency
Class HystrixRequestContext

java.lang.Object
  extended by com.netflix.hystrix.strategy.concurrency.HystrixRequestContext

public class HystrixRequestContext
extends java.lang.Object

Contains the state and manages the lifecycle of HystrixRequestVariableDefault objects that provide request scoped (rather than only thread scoped) variables so that multiple threads within a single request can share state:

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).

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.


Method Summary
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isCurrentThreadInitialized

public static boolean isCurrentThreadInitialized()

getContextForCurrentThread

public static HystrixRequestContext getContextForCurrentThread()

setContextOnCurrentThread

public static void setContextOnCurrentThread(HystrixRequestContext state)

initializeContext

public 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.

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.


shutdown

public void shutdown()
Shutdown HystrixRequestVariableDefault objects in this context.

NOTE: This must be called if initializeContext() was called or a memory leak will occur.