Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  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());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement