Skip to content

Commit

Permalink
Merge pull request #214 from benjchristensen/context-wrapping
Browse files Browse the repository at this point in the history
Context Wrapping
  • Loading branch information
benjchristensen committed Mar 5, 2014
2 parents 85aa3c3 + 887887a commit 01ce154
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion hystrix-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'idea'

dependencies {
compile 'com.netflix.archaius:archaius-core:0.4.1'
compile 'com.netflix.rxjava:rxjava-core:[0.14,0.15)'
compile 'com.netflix.rxjava:rxjava-core:0.16.1'
compile 'org.slf4j:slf4j-api:1.7.0'
compile 'com.google.code.findbugs:jsr305:2.0.0'
provided 'junit:junit-dep:4.10'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.concurrent.Callable;

import com.netflix.hystrix.strategy.HystrixPlugins;

/**
* Wrapper around {@link Callable} that manages the {@link HystrixRequestContext} initialization and cleanup for the execution of the {@link Callable}
*
Expand All @@ -30,6 +32,10 @@ public class HystrixContextCallable<K> implements Callable<K> {
private final Callable<K> actual;
private final HystrixRequestContext parentThreadState;

public HystrixContextCallable(Callable<K> actual) {
this(HystrixPlugins.getInstance().getConcurrencyStrategy(), actual);
}

public HystrixContextCallable(HystrixConcurrencyStrategy concurrencyStrategy, Callable<K> actual) {
this.actual = concurrencyStrategy.wrapCallable(actual);
this.parentThreadState = HystrixRequestContext.getContextForCurrentThread();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import rx.Subscription;
import rx.util.functions.Func2;

import com.netflix.hystrix.strategy.HystrixPlugins;

/**
* Wrapper around {@link Func2} that manages the {@link HystrixRequestContext} initialization and cleanup for the execution of the {@link Func2}
*
Expand Down Expand Up @@ -51,6 +53,10 @@ public class HystrixContextFunc2<T> implements Func2<Scheduler, T, Subscription>
private final AtomicReference<Scheduler> t1Holder = new AtomicReference<Scheduler>();
private final AtomicReference<T> t2Holder = new AtomicReference<T>();

public HystrixContextFunc2(Func2<? super Scheduler, ? super T, ? extends Subscription> action) {
this(HystrixPlugins.getInstance().getConcurrencyStrategy(), action);
}

public HystrixContextFunc2(final HystrixConcurrencyStrategy concurrencyStrategy, Func2<? super Scheduler, ? super T, ? extends Subscription> action) {
this.actual = action;
this.parentThreadState = HystrixRequestContext.getContextForCurrentThread();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.concurrent.Callable;

import com.netflix.hystrix.strategy.HystrixPlugins;

/**
* Wrapper around {@link Runnable} that manages the {@link HystrixRequestContext} initialization and cleanup for the execution of the {@link Runnable}
*
Expand All @@ -27,6 +29,10 @@ public class HystrixContextRunnable implements Runnable {
private final Callable<Void> actual;
private final HystrixRequestContext parentThreadState;

public HystrixContextRunnable(Runnable actual) {
this(HystrixPlugins.getInstance().getConcurrencyStrategy(), actual);
}

public HystrixContextRunnable(HystrixConcurrencyStrategy concurrencyStrategy, final Runnable actual) {
this.actual = concurrencyStrategy.wrapCallable(new Callable<Void>() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import rx.Subscription;
import rx.util.functions.Func2;

import com.netflix.hystrix.strategy.HystrixPlugins;

/**
* Wrap a {@link Scheduler} so that scheduled actions are wrapped with {@link HystrixContextFunc2} so that
* the {@link HystrixRequestContext} is properly copied across threads (if they are used by the {@link Scheduler}).
Expand All @@ -30,6 +32,10 @@ public class HystrixContextScheduler extends Scheduler {
private final HystrixConcurrencyStrategy concurrencyStrategy;
private final Scheduler actualScheduler;

public HystrixContextScheduler(Scheduler scheduler) {
this(HystrixPlugins.getInstance().getConcurrencyStrategy(), scheduler);
}

public HystrixContextScheduler(HystrixConcurrencyStrategy concurrencyStrategy, Scheduler scheduler) {
this.actualScheduler = scheduler;
this.concurrencyStrategy = concurrencyStrategy;
Expand Down

0 comments on commit 01ce154

Please sign in to comment.