spring - 在 @Async 方法中通过 Spring RestTemplate 调用 Rest API
问题描述
据我所知,Spring RestTemplate是同步的并阻塞线程,直到 Web 客户端收到响应,而 Spring WebClient是异步且非阻塞的。
但是,如果我们在@Async注释方法中使用RestTemplate调用 API 会怎样?
它会阻止@Async创建的新线程吗?
最后,您对 Rest API 的异步调用有什么建议(没有WebClient,因为我使用的是早于 5 的 Spring 版本)。谢谢
解决方案
如果我们在带
@Async
注释的方法中使用 RestTemplate 调用 API 会怎样?
@Async
该方法将在您在注释参数中指定的执行器上异步运行。例如@Async("threadPool")
,其中“threadPool”是 Executor bean 的名称。
它会阻止@Async 创建的新线程吗?
是的,它会阻塞 Spring 运行你的方法的线程。但是,线程不一定是由 Spring 创建的,它可以从您在@Async
注释中定义的线程池中获取。
你对 Rest API 的异步调用有什么建议(没有 WebClient,因为我使用的是早于 5 的 Spring 版本)?
您可以使用 CompletableFuture API,或者@Async
如果您只需要“异步”效果。但是,如果您还需要“非阻塞”属性,则需要使用一些非阻塞 HTTP 客户端,例如okhttp。
使用 okhttp 的非阻塞异步 HTTP 调用如下所示:
public CompletableFuture<Response> call() {
Request request = new Request.Builder()
.url(URL)
.build();
Call call = client.newCall(request);
CompletableFuture<Response> result = new CompletableFuture<>();
call.enqueue(new Callback() {
public void onResponse(Call call, Response response) throws IOException {
result.complete(response);
}
public void onFailure(Call call, IOException e) {
result.completeExceptionally(e);
}
});
return result;
}
推荐阅读
- mapbox - 无论在 MapBox 中如何缩放,如何显示更多等高线
- javascript - 为什么 npm start 不起作用,react app 没有在浏览器中打开。react 和 npm 都不起作用?
- amazon-s3 - 看不到从 S3 返回的标头上的元数据
- xml - 如何在 XSLT 中拆分数据并存储在多个节点中
- python - 使用 pandas 操作数据时出错(以防数据丢失)
- java - 在 google colab 中上传文件
- c++ - 无法生成预期的输出
- splunk - 在 splunk 中将国旗表情符号注释到 3166 个国家/地区代码
- javascript - 导入 JSPDF:未捕获的 SyntaxError:无法在模块外使用 import 语句
- sql - SQL - 如何计算期初余额和期末余额