Java 由于缺少请求,获取无法发出滴答声0(interval不支持补充速度比滴答声慢的小型下游请求)
我正在使用带有反应式WebClient的公共API。我想调用API并在每1小时内从那里保存最新的数据。当我将Java 由于缺少请求,获取无法发出滴答声0(interval不支持补充速度比滴答声慢的小型下游请求),java,reactive-programming,spring-webflux,project-reactor,Java,Reactive Programming,Spring Webflux,Project Reactor,我正在使用带有反应式WebClient的公共API。我想调用API并在每1小时内从那里保存最新的数据。当我将.repeatWhen()方法添加到Flux流时,问题出现了,如下所示: @Service public class Covid19APIService { @Value("${rapid-API-URL}") private String covidAPIURL; @Value("${x-rapidapi-host}") private String covidAPI
.repeatWhen()
方法添加到Flux
流时,问题出现了,如下所示:
@Service
public class Covid19APIService {
@Value("${rapid-API-URL}")
private String covidAPIURL;
@Value("${x-rapidapi-host}")
private String covidAPI;
@Value("${x-rapidapi-key}")
private String covidAPIKey;
private WebClient buildWebClient() {
return WebClient.builder()
.baseUrl(covidAPIURL)
.exchangeStrategies(ExchangeStrategies.builder().codecs(this::acceptedCodecs).build())
.defaultHeaders(
httpHeaders -> {
httpHeaders.add("x-rapidapi-host", covidAPI);
httpHeaders.add("x-rapidapi-key", covidAPIKey);
})
.build();
}
public Flux<CountryCasesHistoryWrapper> findCasesHistoryForCountryAndDate1(
String country, String date) {
return buildWebClient()
.get()
.uri(
builder ->
builder
.path("/history_by_particular_country_by_date.php")
.queryParam("country", country)
.queryParam("date", date)
.build())
.retrieve()
.onStatus(
HttpStatus::is4xxClientError,
response -> error(new InfrastructureException("Covid19 public API not found")))
.onStatus(
HttpStatus::is4xxClientError,
response -> error(new InfrastructureException("Covid19 public API server exception")))
.bodyToMono(CountryCasesHistoryWrapper.class)
.repeatWhen(interval -> Flux.interval(Duration.ofMinutes(10)))
.single()
.repeatWhen(interval -> Flux.interval(Duration.ofSeconds(30))).delayElements(Duration.ofSeconds(10));
}
private void acceptedCodecs(ClientCodecConfigurer clientCodecConfigurer) {
clientCodecConfigurer
.customCodecs()
.register(new Jackson2JsonEncoder(new ObjectMapper(), TEXT_HTML));
clientCodecConfigurer
.customCodecs()
.register(new Jackson2JsonDecoder(new ObjectMapper(), TEXT_HTML));
}
}
我注意到了类似的问题,但尽管添加了delayElements()
方法,异常仍然在发生。问题来了,如何修复当前的实现并达到预期的目标,即每小时调用API并从中获取最新数据。我将感谢所有建议
public Mono<ServerResponse> getCasesHistoryForCountryAndDate1(ServerRequest serverRequest) {
String country =
serverRequest
.queryParam("country")
.orElseThrow(
() -> new InfrastructureException("Country parameter is required for this action"));
String date =
serverRequest
.queryParam("date")
.orElseThrow(
() -> new InfrastructureException("Date parameter is required for this action"));
final Flux<CountryCasesHistoryWrapper> casesHistoryForCountryAndDate =
apiService.findCasesHistoryForCountryAndDate1(country, date);
return casesHistoryForCountryAndDate
.flatMap(
countryCasesHistoryWrapper ->
repository.saveAll(countryCasesHistoryWrapper.getStatsByCountry()))
.collectList()
.flatMap(
countryCasesHistories ->
ServerResponse.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(countryCasesHistories)));
}
reactor.core.Exceptions$OverflowException: Could not emit tick 0 due to lack of requests (interval doesn't support small downstream requests that replenish slower than the ticks)
at reactor.core.Exceptions.failWithOverflow(Exceptions.java:231) ~[reactor-core-3.3.3.RELEASE.jar:3.3.3.RELEASE]