2

I have the following method which calls a rest api using webclient in a library.

public Flux<Offer[]> getOffersByIds(List<String> ids) {
    return Flux.fromStream(batch(ids, 100)).flatMap(batch -> getOffersForBatch(batch));
}

private Mono<Offer[]> getOffersForBatch(List<String> ids) {
    return webClient.get().uri(URL + PROMOTIONS_ENDPOINT, uriBuilder -> uriBuilder
                    .queryParam("ids", String.join(",", ids))
                    .build())
            .header("Accept-Language", "en-GB")
            .retrieve()
            .onStatus(HttpStatus::is5xxServerError, response -> Mono.error(new RetryableException("api error")))
            .bodyToMono(Offer[].class)
            .retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
                    .jitter(0.75)
                    .filter(throwable -> throwable instanceof RunException)
                    .onRetryExhaustedThrow(((retryBackoffSpec, retrySignal) -> {
                        throw new ServerException("service failed after max retries");
                    })));
}

public static <T> Stream<List<T>> batch(List<T> source, int length) {
    if (length <= 0)
        throw new IllegalArgumentException("length = " + length);
    int size = source.size();
    if (size <= 0)
        return Stream.empty();
    int fullChunks = (size - 1) / length;
    return IntStream.range(0, fullChunks + 1).mapToObj(
            n -> source.subList(n * length, n == fullChunks ? size : (n + 1) * length));
}

In the service, I am trying to invoke the api:

offService.getOffersByIds(ids)
          .subscribe(
                  success -> log.info("Success:" + success.toString()),
                  error -> log.error("Failure:" + error.getMessage()),
                  () -> log.error("No value")
          );

But I don't see any logs related to response

Martin Tarjányi
  • 8,863
  • 2
  • 31
  • 49
cppcoder
  • 22,227
  • 6
  • 56
  • 81
  • How does the service look like from where you are calling offService? Are you aware that calling `subscribe` makes the flow async in this case and it will not wait the end of the execution? – Martin Tarjányi Sep 19 '22 at 11:33
  • I resolved the problem. The issue was longer retry delay resulting in the async waiting longer than expected. I expected that the individual calls also would log inside `subscribe`, but looks like only final retry only results in `subscribe` result – cppcoder Sep 19 '22 at 17:40

0 Answers0