Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.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
Java 如何避免每个类中的硬代码WebClient retryWhen_Java_Spring_Spring Webclient - Fatal编程技术网

Java 如何避免每个类中的硬代码WebClient retryWhen

Java 如何避免每个类中的硬代码WebClient retryWhen,java,spring,spring-webclient,Java,Spring,Spring Webclient,我想实现重试机制,我做了如下操作: public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5)) .onRetryExhaustedThrow( (retryBackoffSpec, retrySignal) -> new TimeoutException( retrySignal.failure().getMessage()

我想实现重试机制,我做了如下操作:

  public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
      .onRetryExhaustedThrow(
          (retryBackoffSpec, retrySignal) -> new TimeoutException(
              retrySignal.failure().getMessage()));
在这里使用这种方法:

 public List<A> findByTimestamp(LocalDate localDate) {
    return webClient.get()
        .uri(bProp.getPath())
        .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
        .retrieve()
        .bodyToFlux(bData.class)
        .retryWhen(fixedRetry)
        .map(this::toC)
        .collectList()
        .block();
  }
public List findByTimestamp(LocalDate LocalDate){
返回webClient.get()
.uri(bProp.getPath())
.header(HttpHeaders.ACCEPT,MediaType.APPLICATION\u JSON\u值)
.retrieve()
.bodyToFlux(b数据类)
.retryWhen(固定测光)
.map(this::toC)
.LIST()
.block();
}

但是我想创建一个通用的方法,以便在所有应用程序中使用它,而不是在所有类中编写第一个方法,如何才能更有效地做到这一点?

我会将webClient注册为配置类中的一个bean,并在那里实现重试逻辑,然后在需要webClient的地方自动连接它,而不是显式地创建一个新对象。我找到了一个可能有用的答案

我没有时间检查这段代码是否有效,但我的意思是:

@Configuration
public class WebClientConfiguration {

@Bean
public WebClient retryWebClient(WebClient.Builder builder) {
    return builder.baseUrl("http://localhost:8080")
            .filter((request, next) -> next.exchange(request)
                    .retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(5))
                            .onRetryExhaustedThrow(
                                    (retryBackoffSpec, retrySignal) -> new TimeoutException(
                                            retrySignal.failure().getMessage()))))
            .build();
}
}
然后将其自动连接到需要的任何位置:

@Autowired
private WebClient webClient;

我将把webClient注册为配置类中的一个bean,并在那里实现重试逻辑,然后在需要webClient的地方自动连接它,而不是显式地创建一个新对象。我找到了一个可能有用的答案

我没有时间检查这段代码是否有效,但我的意思是:

@Configuration
public class WebClientConfiguration {

@Bean
public WebClient retryWebClient(WebClient.Builder builder) {
    return builder.baseUrl("http://localhost:8080")
            .filter((request, next) -> next.exchange(request)
                    .retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(5))
                            .onRetryExhaustedThrow(
                                    (retryBackoffSpec, retrySignal) -> new TimeoutException(
                                            retrySignal.failure().getMessage()))))
            .build();
}
}
然后将其自动连接到需要的任何位置:

@Autowired
private WebClient webClient;

我决定实施的方式如下:

public class RetryWebClient {

  public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
      .onRetryExhaustedThrow(
          (retryBackoffSpec, retrySignal) -> new TimeoutException(
              retrySignal.failure().getMessage()));

  private RetryWebClient() {}

}
并在
retryWhen
中调用它:

.retryWhen(RetryWebClient.fixedRetry)

我决定实施的方式如下:

public class RetryWebClient {

  public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
      .onRetryExhaustedThrow(
          (retryBackoffSpec, retrySignal) -> new TimeoutException(
              retrySignal.failure().getMessage()));

  private RetryWebClient() {}

}
并在
retryWhen
中调用它:

.retryWhen(RetryWebClient.fixedRetry)

我可以创建一个只使用FixedMethod的类来在我的第二个方法中传递它吗?我可以创建一个只使用FixedMethod的类来在我的第二个方法中传递它吗?酷,我希望我能提供帮助。我不是100%确定将fixedRetry设置为静态并将其传递给bean创建方法是可以的(我假设您正在这样做),但是应该可以,因为默认范围是单例。很酷,我希望我能够提供帮助。我不能100%确定将fixedRetry设置为静态并将其传递给bean创建方法是正确的(我假设您正在这样做),但是应该可以,因为默认范围是单例。