首页 > 解决方案 > Bean 范围请求的多线程问题

问题描述

Thread目前在使用以下代码的并行流时面临问题

private ThreadPoolTaskExecutor executor

Future<List<Boolean>> resList = executor.submit(()->requestIds.parallelStream().map(id->completeTask(id)).collect(Collectors.toList()));
            List<Boolean> resultList = resList.get();

无法使用获取范围请求的 bean/线程异常。流中使用的方法正在调用另一个类的另一个方法,这会为范围请求提供错误。

Error : java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

RequestContextListener在 Spring Boot 应用程序中使用

标签: javamultithreadingspring-bootjava-8parallel-processing

解决方案


推荐阅读