Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save hflamboauto1/6f8897a35aacdf7fa5b564772d4ab890 to your computer and use it in GitHub Desktop.

Select an option

Save hflamboauto1/6f8897a35aacdf7fa5b564772d4ab890 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);
return callable.call();
};
} finally {
// Remember to clear thee threadLocals
ThreadLocalUtil.clear()
}
}
}
// 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