简体   繁体   English

Spring WebFlux WebClient timeout()和exchange()

[英]Spring WebFlux WebClient timeout() and exchange()

I have code that is similar to the following example: 我有类似于以下示例的代码:

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

The spring documentation says: 春天的文件说:

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. 使用exchange()时,必须始终使用ClientResponse的body或toEntity方法中的任何一种,以确保释放资源并避免HTTP连接池的潜在问题。 You can use bodyToMono(Void.class) if no response content is expected. 如果不需要响应内容,则可以使用bodyToMono(Void.class)。 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. 但是请记住,如果响应中确实包含内容,则连接将关闭,并且不会放回池中。

Question: In case of a TimeoutException triggered by timeout(...) as in the code above, do I have to do something explicitely to make sure that all resources are released properly or is the code above sufficient? 问题:如果如上述代码中的timeout(...)触发了TimeoutException,我是否必须明确地做一些事情以确保所有资源都被正确释放,或者上面的代码是否足够? I want to avoid having a memory leak here. 我想避免这里发生内存泄漏。

I don't think this is an issue in this case. 在这种情况下,我认为这不是问题。

When triggered, timeout will cancel() upstream, effectively closing the connection and not returning it to the connection pool. 触发后, timeout将在上游cancel() ,从而有效关闭连接并将其不返回到连接池。 You don't need to do anything special here, and there won't be a memory leak (besides buffers already sitting in reactor internal queues, which is a problem Spring Framework will solve in SPR-17025 ). 您无需在这里做任何特殊的事情,也不会发生内存泄漏(除了已经位于反应堆内部队列中的缓冲区之外,这是Spring Framework在SPR-17025中将解决的问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM