java多线程父子孙传递参数No thread-bound request found: Are you referring to request attributes outside of an a

代码日记:

实现一个需求,多线程嵌套多线程最后调用feign接口,请求某个微服务。容易出现请求头丢失的情况,
报错 The request object has been recycled and is no longer associated with this facade
现在用本地线程变量来接受存储。

Java 使用线程池,父子孙之间传递参数。思路使用 TransmittableThreadLocal
代码1:

public class holder {
    private static final TransmittableThreadLocal<ServletRequestAttributes> requestHolder = new TransmittableThreadLocal<>();
    public static void set(ServletRequestAttributes request) {
        requestHolder.set(request);
    }
    public static ServletRequestAttributes get() {
        return requestHolder.get();
    }
    public static void remove() {
        requestHolder.remove();
    }
}

代码2:在主程序

ConcurrentHashMap<String, Object> lists = new ConcurrentHashMap<>();
            int threadCount = 4;
            CyclicBarrier cyclicBarrier = new CyclicBarrier(threadCount);
            try {
                for (int i = 1; i < threadCount; i++) {
                    int finalI = i;
                    fixedThreadPool.submit(() -> {
                        holder.set(servletRequestAttributes);
                        try {
                            if (finalI == 1) {
                                lists.put("xx", XXX(id));
                            } else if (finalI == 2) {
                                lists.put("xx", XXX(id);
                            } 
                        } catch (
                                Exception e) {
                            e.printStackTrace();
                        } finally {
                            holder.remove();
                            try {
                                cyclicBarrier.await();
                            } catch (
                                    Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            } catch (
                    Exception e) {
                e.printStackTrace();
            } finally {
                cyclicBarrier.await();
            }

代码3 在XXX方法中

ServletRequestAttributes servletRequestAttributes = holder.get();

       int threadCount = 4;
       CyclicBarrier cyclicBarrier = new CyclicBarrier(threadCount);
       try {
           for (int i = 1; i < threadCount; i++) {
               int finalI = i;
               fixedThreadPool.submit(() -> {
                   holder.set(servletRequestAttributes);
                   try {
                       if (finalI == 1){
                           List<Map<String, Object>> apple = YYY(id);
                           ref.lists.addAll(apple);
                       }else if (finalI == 2){
                           List<Map<String, Object>> bannana = YYY(id);
                           ref.lists.addAll(bannana);
                       }                   }catch (Exception e) {
                       e.printStackTrace();
                   } finally {
                       try {
                           holder.remove();
                           cyclicBarrier.await();
                       } catch (Exception e) {
                           e.printStackTrace();
                       }
                   }
               });
           }
       }finally {
           cyclicBarrier.await();
       }

代码4 在YYY方法中 最终

ServletRequestAttributes servletRequestAttributes = ViewHolder.get();
        for (int i = 0; i < poolNumber; i++) {
            int finalI = i;
            fixedThreadPool.execute(()->{
                try{
                	//feign接口调用另一个服务,需要用到http请求头
                    feign.eat();
                }
		}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值