Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 有没有一种方法可以为Rest模板提供动态超时?_Spring Boot_Connection Pooling_Resttemplate_Completable Future_Spring Webclient - Fatal编程技术网

Spring boot 有没有一种方法可以为Rest模板提供动态超时?

Spring boot 有没有一种方法可以为Rest模板提供动态超时?,spring-boot,connection-pooling,resttemplate,completable-future,spring-webclient,Spring Boot,Connection Pooling,Resttemplate,Completable Future,Spring Webclient,我正在使用SpringREST模板和apache的PoolightTPClientConnectionManager进行API调用。我正在处理的项目需要为我通过rest模板发出的每个HTTP请求设置自定义超时。为了实现这一点,我使用CompletableFuture和一个单独的ExecutorService,并调用get(Timeout)方法 试试看{ CompletableFuture=CompletableFuture.SupplySync(()->bidderService.getBid(

我正在使用SpringREST模板和apache的PoolightTPClientConnectionManager进行API调用。我正在处理的项目需要为我通过rest模板发出的每个HTTP请求设置自定义超时。为了实现这一点,我使用CompletableFuture和一个单独的ExecutorService,并调用get(Timeout)方法

试试看{
CompletableFuture=CompletableFuture.SupplySync(()->bidderService.getBid(),executorService);
bidResponse=future.get(bidderTimeout,TimeUnit.millizes);
}捕获(InterruptedException | TimeoutException | ExecutionException e){
bidResponse=getTimeoutBidResponse();
}
不幸的是,这种方法的问题是,在超时的情况下,底层线程将继续工作,直到rest模板完成其调用。因此,我在某种程度上丢失了线程池中的一个线程,以及HTTP连接池中的一个连接。 有没有办法在收到超时异常后立即关闭HTTP连接,并将HTTP连接返回到池中

p、 我还尝试使用SpringWebClient和Mono.timeout。实际上,它会立即关闭HTTP连接,但不会将其返回HTTP池

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) 
{
    return restTemplateBuilder
       .setConnectTimeout(...)
       .setReadTimeout(...)
       .build();
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) 
{
    return restTemplateBuilder
       .setConnectTimeout(...)
       .setReadTimeout(...)
       .build();
}