SHARE
TWEET

Untitled

a guest Oct 20th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ThreadLocalUtil {
  2.     private static ThreadLocal<Integer> idTL = new ThreadLocal<>();
  3.  
  4.     public static void setId(Integer id) {
  5.         idTL.set(id);
  6.     }
  7.  
  8.     public static void getId() {
  9.         idTL.get();
  10.     }
  11.  
  12.     public static void clear() {
  13.         idTL.remove();
  14.     }
  15. }
  16.  
  17. public class ConcurrencyStrategy extends HystrixConcurrencyStrategy {
  18.     @Override
  19.     public <T> Callable<T> wrapCallable(Callable<T> callable) {
  20.         Integer id = ThreadLocalUtil.getId();
  21.         try {
  22.             return () -> {
  23.                 ThreadLocalUtil.setId(id);
  24.                 return callable.call();
  25.             };
  26.         } finally {
  27.             // Remember to clear thee threadLocals
  28.             ThreadLocalUtil.clear()
  29.         }
  30.     }
  31. }
  32.  
  33. // Register the concurrency strategy
  34. HystrixPlugins.getInstance().registerConcurrencyStrategy(new ConcurrencyStrategy());
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top