java - Spring WebFlux WebClient timeout() 和 exchange()

标签 java spring webclient spring-webflux project-reactor

我有类似于以下示例的代码:

Mono<ResponseEntity<String>> result = webClient
    .post()
    .body(Mono.just(command), MyCommand.class)
    .exchange()
    .timeout(calculateTimeout(command))
    .flatMap(clientResponse -> clientResponse.toEntity(String.class));

spring documentation说:

When using exchange() you must always use any of the body or toEntity methods of ClientResponse to ensure resources are released and to avoid potential issues with HTTP connection pooling. You can use bodyToMono(Void.class) if no response content is expected. However keep in mind that if the response does have content, the connection will be closed and will not be placed back in the pool.

问题:如果上面代码中的 timeout(...) 触发了 TimeoutException,我是否必须明确地做一些事情来确保所有资源都被正确释放或者上面的代码是否足够?我想避免在这里发生内存泄漏。

最佳答案

在这种情况下,我认为这不是问题。

触发时,timeoutcancel() 上游,有效地关闭连接并且不将其返回到连接池。您不需要在这里做任何特别的事情,也不会有内存泄漏(除了已经位于 react 器内部队列中的缓冲区,这是 Spring Framework 将在 SPR-17025 中解决的问题)。

关于java - Spring WebFlux WebClient timeout() 和 exchange(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51937483/

相关文章:

spring - 一种定向建模在使用Spring Data Redis时不拉取记录

c# - 从 C# 中生成的搜索结果中选择一个随机链接

.net - 将整个文件夹上传到FTP的PowerShell脚本

c# - 如何在 C# .NET 中从 GitHub 下载原始文件

java - 使用 Spring Boot 和 Spring Data JPA 的相同域模型的多个数据库

java - Eclipse:将旧项目一起归档到根项目/文件夹中的提示?

java - 尝试在 Java 中使用 hasNext 时出现错误

java - 将通用列表传递给 JSP 标记

json - 将 JSON 数据传递到 Spring Controller 时出现错误请求

java - Apache Tomcat 重新启动。无法加载 com.mysql.jdbc.SQLError