public HystrixContextRunnable(HystrixConcurrencyStrategy concurrencyStrategy, final Runnable actual) { this(concurrencyStrategy, HystrixRequestContext.getContextForCurrentThread(), actual); }
/** * 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); } }
/** * 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 HystrixContextCallable(HystrixConcurrencyStrategy concurrencyStrategy, Callable<K> actual) { this.actual = concurrencyStrategy.wrapCallable(actual); this.parentThreadState = HystrixRequestContext.getContextForCurrentThread(); }
public static HystrixCommandCompletion from(ExecutionResult executionResult, HystrixCommandKey commandKey, HystrixThreadPoolKey threadPoolKey) { return from(executionResult, commandKey, threadPoolKey, HystrixRequestContext.getContextForCurrentThread()); }
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); } } }); }
@Override public K call() throws Exception { HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread(); try { // set the state of this thread to that of its parent HystrixRequestContext.setContextOnCurrentThread(parentThreadState); // execute actual Callable with the state of the parent return actual.call(); } finally { // restore this thread back to its original state HystrixRequestContext.setContextOnCurrentThread(existingState); } }
@Override public void run() { HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread(); try { // set the state of this thread to that of its parent HystrixRequestContext.setContextOnCurrentThread(parentThreadState); // execute actual Callable with the state of the parent try { actual.call(); } catch (Exception e) { throw new RuntimeException(e); } } finally { // restore this thread back to its original state HystrixRequestContext.setContextOnCurrentThread(existingState); } }
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 {}
if (HystrixRequestContext.getContextForCurrentThread() == null) { throw new IllegalStateException(HystrixRequestContext.class.getSimpleName() + ".initializeContext() must be called at the beginning of each request before RequestVariable functionality can be used."); ConcurrentHashMap<HystrixRequestVariableDefault<?>, LazyInitializer<?>> variableMap = HystrixRequestContext.getContextForCurrentThread().state;
@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); } } });
@Override protected Observable<String> construct() { assertNotNull("Wiring the Batch command into the Observable chain should have a HystrixRequestContext", HystrixRequestContext.getContextForCurrentThread()); if (commandExecutionFails) { return Observable.error(new RuntimeException("Synthetic error while running batch command")); } else { return Observable.create(new OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { try { assertNotNull("Executing the Batch command should have a HystrixRequestContext", HystrixRequestContext.getContextForCurrentThread()); Thread.sleep(1); for (Integer arg : args) { int numEmits = emitsPerArg.get(arg.toString()); for (int j = 1; j < numEmits + 1; j++) { subscriber.onNext(arg + ":" + (arg * j)); Thread.sleep(1); } Thread.sleep(1); } subscriber.onCompleted(); } catch (Throwable ex) { ex.printStackTrace(); subscriber.onError(ex); } } }); } } }
@Override protected HystrixObservableCommand<String> createCommand(Collection<CollapsedRequest<String, String>> collapsedRequests) { assertNotNull("command creation should have HystrixRequestContext", HystrixRequestContext.getContextForCurrentThread()); if (commandConstructionFails) { throw new RuntimeException("Exception thrown in command construction"); } else { List<Integer> args = new ArrayList<Integer>(); for (CollapsedRequest<String, String> collapsedRequest : collapsedRequests) { String stringArg = collapsedRequest.getArgument(); int intArg = Integer.parseInt(stringArg); args.add(intArg); } return new TestCollapserCommandWithMultipleResponsePerArgument(args, emitsPerArg, commandExecutionFails); } }
@Override public void call(Subscriber<? super String> subscriber) { try { assertNotNull("Executing the Batch command should have a HystrixRequestContext", HystrixRequestContext.getContextForCurrentThread()); Thread.sleep(1); for (Integer arg : args) { int numEmits = emitsPerArg.get(arg.toString()); for (int j = 1; j < numEmits + 1; j++) { subscriber.onNext(arg + ":" + (arg * j)); Thread.sleep(1); } Thread.sleep(1); } subscriber.onCompleted(); } catch (Throwable ex) { ex.printStackTrace(); subscriber.onError(ex); } } });
final HystrixRequestContext hystrixRequestContext = HystrixRequestContext.getContextForCurrentThread();
final HystrixRequestContext requestContext = HystrixRequestContext.getContextForCurrentThread(); long latency = System.currentTimeMillis() - executionResult.getStartTimestamp();
final HystrixRequestContext currentRequestContext = HystrixRequestContext.getContextForCurrentThread();
/** * 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 HystrixContextRunnable(HystrixConcurrencyStrategy concurrencyStrategy, final Runnable actual) { this.actual = concurrencyStrategy.wrapCallable(new Callable<Void>() { @Override public Void call() throws Exception { actual.run(); return null; } }); this.parentThreadState = HystrixRequestContext.getContextForCurrentThread(); }
public static void shutdownHystrixRequestContext() { if (HystrixRequestContext.isCurrentThreadInitialized()) { HystrixRequestContext.getContextForCurrentThread().shutdown(); } }