【发布时间】:2016-05-31 05:45:09
【问题描述】:
我有一个控制器和 3 个服务 bean。我的目标是在请求范围内从 ExecutorService 触发 3 个并发 REST 服务。
当它尝试在请求范围内为每个@autowired 服务 bean 初始化 bean 3 次时,我遇到了异常。
谁能帮我找出根本原因并解决问题。
Exception: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.RestService1: Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton;
nested exception is 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.
代码:
@RestController
@RequestMapping(value = "/")
public class TestController {
@Autowired
private RestService1 restService1;
@Autowired
private RestService2 restService2;
@Autowired
private RestService3 restService3;
@RequestMapping(value = "/testConcurency",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody testConcurency(@Valid @RequestBody TestRequest req, BindingResult errors) throws Exception {
ExecutorService executor = null;
Collection<Callable<TestResponse>> tasks = new ArrayList();
tasks.add(restService1);
tasks.add(restService2);
tasks.add(restService3);
executor = Executors.newFixedThreadPool(tasks.size());
List<Future<KycResponse>> list = executor.invokeAll(tasks,30, TimeUnit.SECONDS);
}
}
@Service
@Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode=ScopedProxyMode.TARGET_CLASS)
public class RestService1 implements IThreadService, Callable<KycResponse>{
@Override
public TestResponse call() throws Exception {
System.out.println("Hello from"+this);
// REST service will be called from here.
}
}
Web.xml 我添加了一个监听器
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
谢谢!
【问题讨论】:
-
请添加完整的堆栈跟踪
标签: spring multithreading executorservice