/** * Removes the value of the HystrixRequestVariable from the current request. * <p> * This will invoke {@link #shutdown} if implemented. * <p> * If the value is subsequently fetched in the thread, the {@link #initialValue} method will be called again. */ public void remove() { if (HystrixRequestContext.getContextForCurrentThread() != null) { remove(HystrixRequestContext.getContextForCurrentThread(), this); } }
public HystrixContextCallable(HystrixConcurrencyStrategy concurrencyStrategy, Callable<K> actual) { this.actual = concurrencyStrategy.wrapCallable(actual); this.parentThreadState = HystrixRequestContext.getContextForCurrentThread(); }
/** * Return null if the {@link HystrixRequestContext} has not been initialized for the current thread. * <p> * If {@link HystrixRequestContext} has been initialized then call method in superclass: * {@link HystrixRequestVariableDefault#get()} */ @Override public T get() { if (!HystrixRequestContext.isCurrentThreadInitialized()) { return null; } return super.get(); }
private void shutdownContextIfExists() { // instead of storing the reference from initialize we'll just get the current state and shutdown if (HystrixRequestContext.getContextForCurrentThread() != null) { // it could have been set NULL by the test HystrixRequestContext.getContextForCurrentThread().shutdown(); } } private static class DummyHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {}
@Override protected void after() { if (this.context != null) { this.context.shutdown(); this.context = null; } }
private static void setRequestContextIfNeeded(final HystrixRequestContext currentRequestContext) { if (!HystrixRequestContext.isCurrentThreadInitialized()) { // even if the user Observable doesn't have context we want it set for chained operators HystrixRequestContext.setContextOnCurrentThread(currentRequestContext); } }
@Before public void prepareForTest() { /* we must call this to simulate a new request lifecycle running and clearing caches */ HystrixRequestContext.initializeContext(); }
/** * Sets the value of the HystrixRequestVariable for the current request context. * <p> * 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 {@link #shutdown} method will not be * called * * @param value * the value to set */ public void set(T value) { HystrixRequestContext.getContextForCurrentThread().state.put(this, new LazyInitializer<T>(this, value)); }
public HystrixContextRunnable(HystrixConcurrencyStrategy concurrencyStrategy, final Runnable actual) { this(concurrencyStrategy, HystrixRequestContext.getContextForCurrentThread(), actual); }
public HystrixContextRunnable(final HystrixConcurrencyStrategy concurrencyStrategy, final HystrixRequestContext hystrixRequestContext, final Runnable actual) { this.actual = concurrencyStrategy.wrapCallable(new Callable<Void>() { @Override public Void call() throws Exception { actual.run(); return null; } }); this.parentThreadState = hystrixRequestContext; }
/** * Delegates to the wrapped {@link HystrixRequestVariableLifecycle} * @param value * of request variable to allow cleanup activity. * <p> * If nothing needs to be cleaned up then nothing needs to be done in this method. */ @Override public void shutdown(T value) { lifecycle.shutdown(value); }
/** * Delegates to the wrapped {@link HystrixRequestVariableLifecycle} * @return T with initial value or null if none. */ @Override public T initialValue() { return lifecycle.initialValue(); }
@SuppressWarnings("unchecked") /* package */static <T> void remove(HystrixRequestContext context, HystrixRequestVariableDefault<T> v) { // remove first so no other threads get it LazyInitializer<?> o = context.state.remove(v); if (o != null) { // this thread removed it so let's execute shutdown v.shutdown((T) o.get()); } }
/** * {@link HystrixRequestLog} for current request as defined by {@link HystrixRequestContext}. * * @return {@link HystrixRequestLog} */ public static HystrixRequestLog getCurrentRequest(HystrixConcurrencyStrategy concurrencyStrategy) { return currentRequestLog.get(concurrencyStrategy); }
public HystrixContextScheduler(HystrixConcurrencyStrategy concurrencyStrategy, HystrixThreadPool threadPool, Func0<Boolean> shouldInterruptThread) { this.concurrencyStrategy = concurrencyStrategy; this.threadPool = threadPool; this.actualScheduler = new ThreadPoolScheduler(threadPool, shouldInterruptThread); }
public synchronized T get() { if (!initialized) { value = rv.initialValue(); initialized = true; } return value; } }
@Override public Worker createWorker() { return new ThreadPoolWorker(threadPool, shouldInterruptThread); }
public HystrixContexSchedulerAction(final HystrixConcurrencyStrategy concurrencyStrategy, Action0 action) { this.actual = action; this.parentThreadState = HystrixRequestContext.getContextForCurrentThread(); this.c = concurrencyStrategy.wrapCallable(new Callable<Void>() { @Override public Void call() throws Exception { HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread(); try { // set the state of this thread to that of its parent HystrixRequestContext.setContextOnCurrentThread(parentThreadState); // execute actual Action0 with the state of the parent actual.call(); return null; } finally { // restore this thread back to its original state HystrixRequestContext.setContextOnCurrentThread(existingState); } } }); }
/** * Shutdown {@link HystrixRequestVariableDefault} objects in this context. * <p> * <b>NOTE: This must be called if <code>initializeContext()</code> was called or a memory leak will occur.</b> * * This method invokes <code>shutdown()</code> */ public void close() { shutdown(); }
/** * {@link HystrixRequestLog} for current request as defined by {@link HystrixRequestContext}. * <p> * NOTE: This uses the default {@link HystrixConcurrencyStrategy} or global override. If an injected strategy is being used by commands you must instead use * {@link #getCurrentRequest(HystrixConcurrencyStrategy)}. * * @return {@link HystrixRequestLog} */ public static HystrixRequestLog getCurrentRequest() { return currentRequestLog.get(HystrixPlugins.getInstance().getConcurrencyStrategy()); }