Skip to content

Instantly share code, notes, and snippets.

@yp-palF
Last active December 18, 2019 02:39
Show Gist options
  • Save yp-palF/1f0803d57048a858093283864af0eeb5 to your computer and use it in GitHub Desktop.
Save yp-palF/1f0803d57048a858093283864af0eeb5 to your computer and use it in GitHub Desktop.
public class ThreadLocalUtil {
private static ThreadLocal<Integer> idTL = new ThreadLocal<>();
public static void setId(Integer id) {
idTL.set(id);
}
public static void getId() {
idTL.get();
}
public static void clear() {
idTL.remove();
}
}
public class ConcurrencyStrategy extends HystrixConcurrencyStrategy {
@Override
public <T> Callable<T> wrapCallable(Callable<T> callable) {
Integer id = ThreadLocalUtil.getId();
try {
return () -> {
ThreadLocalUtil.setId(id);
T result = callable.call();
ThreadLocalUtil.clear();
return result;
};
}
}
}
// Register the concurrency strategy
HystrixPlugins.getInstance().registerConcurrencyStrategy(new ConcurrencyStrategy());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment