【发布时间】:2015-09-24 11:09:47
【问题描述】:
考虑一个代码:
@Autowired
private AsyncRestOperations restTemplate;
@RequestMapping("/abcd")
public CompletableFuture<Result> process(HttpRequest request) {
convertListenableFutureToCompletableFuture(restTemplate.getForEntity("http://...", Result.class))
.thenApply(/*Some logic here*/)
.thenCompose(/*Some logic returns future*/)
}
这里可以看到如下处理顺序:
- Spring 接收到
DispatcherServlet的请求。 -
DispatcherServlet确定处理程序并将请求传递给它。 -
process方法被调用 -
restTemplate.getForEntity被调用。 -
thenApply被调用 -
thenCompose被调用 - CompletableFuture 被“返回”回
DispatcherServlet(或其他 Spring 组件)
据我了解,1-3 和 7 点在同一个线程池中执行(是吗?)。
但是什么线程(池)用于执行点4-6?
【问题讨论】:
-
如果您发布了restTemplate bean的配置(我们只能看到接口定义),应该可以准确地告诉您调用第4步后会发生什么。
-
只是Spring rest异步模板,像
new AsyncRestTemplate()那样创建。
标签: java multithreading spring spring-mvc asynchronous