Kilka requestów dopóki warunek jest spełniony

0

Mój problem polega na tym, że mam kod podobny do tego poniżej, należy w nim wysyłać requesty dopóki w odpowiedzi z hosta pojawia się odpowiednie pole, a przy każdym requescie należy zbierać dane i na końcu zwrócić obiekt ze wszystkimi danym. Moje pytanie do Was w jaki sposób to napisać? Na tą chwilę w tym kodzie poniżej w momencie repeat zamiast wysłać nowy request i wywołać metode executeRequest , cały czas mam ten sam productResponse czyli ta samą odpowiedz. Sprawdziłem w debugu , program nie wchodzi do metody gdzie jest wywolanie webClient.call

public Mono<MyAppResponse> execute(ProductRequest productRequest, LinkedMultiValueMap<String, String> headers) {

    AtomicReference<List<Integer>> numbers = new AtomicReference<>();
    numbers.set(new ArrayList<>());

    AtomicReference<StringBuilder> descriptionFromAllRq = new AtomicReference<>();
    AtomicReference<MyAppResponse> myAppResponseAtomicReference = new AtomicReference<>();

    return executeRequest(productRequest, headers)
          .flatMap(productResponse -> {
              numbers.get().addAll(productResponse.getNumbers());
              descriptionFromAllRq.get().append(productResponse.getNext());
              myAppResponseAtomicReference.get().setNext(productResponse.getNext());
              return Mono.just(myAppResponseAtomicReference.get());
          })
          .repeat()
          .takeWhile(myAppResponse -> descriptionFromAllRq.get().toString().equals("YES NEXT"))
          .doOnComplete(() -> {
              myAppResponseAtomicReference.get().setAllNumbers(numbers.get());
              myAppResponseAtomicReference.get().setDescriptionFromAllRq(descriptionFromAllRq.get().toString());
              System.out.println("Successful response from the host ");
          })
          .last();
}

private Mono<ProductResponse> executeRequest(ProductRequest productRequest, LinkedMultiValueMap<String, String> headers) {
    return webClient.call(productRequest, headers);
}

class MyAppResponse {
    private List<Integer> allNumbers;
    private String descriptionFromAllRq;
    private String next;

    public String getNext() {
        return next;
    }

    public void setNext(String next) {
        this.next = next;
    }

    public List<Integer> getAllNumbers() {
        return allNumbers;
    }

    public void setAllNumbers(List<Integer> allNumbers) {
        this.allNumbers = allNumbers;
    }

    public String getDescriptionFromAllRq() {
        return descriptionFromAllRq;
    }

    public void setDescriptionFromAllRq(String descriptionFromAllRq) {
        this.descriptionFromAllRq = descriptionFromAllRq;
    }
}

class ProductRequest {

}

class ProductResponse {
    private List<Integer> numbers;
    private String next;

    public List<Integer> getNumbers() {
        return numbers;
    }

    public String getNext() {
        return next;
    }
}
0

Ja bym napisał to mniej więcej tak:

@Override
public Mono<ServerResponse> handle(ServerRequest request) {

    WebClient build = WebClient.builder().build();

    // cialo req ktore wysylasz over and over again
    HostQuery body = new HostQuery();
    body.setName(UUID.randomUUID().toString());
    body.setSurname(UUID.randomUUID().toString());

    // cache na wszystkie wyniki/responsy
    AtomicReference<List<HostQuery>> cache = new AtomicReference<>();
    cache.set(new ArrayList<>());

    // strzel do systemu trzeciego
    return build.post()
            .uri("localhost:8080/call")
            .bodyValue(body)
            .retrieve()
            .bodyToMono(HostQuery.class)
            .flatMap(resp -> {
                // wszystkie odpowiedzi dodawaj do cache
                cache.get().add(resp);
                // jak system trzeci odpowie, sprawdź czy twoj warunek jest spełniony
                if (resp.getId() != 5) {
                    // jak odpowiedz nie spelnia warunku, zmapuj na fałszywy błąd
                    return Mono.error(new Exception());
                }
                // zwróć cokolwiek
                return Mono.just(resp);
            })
            .retry() // retry działa gdy wyżej masz Mono.error
            .zipWith(Mono.just(cache.get())) // wepnij cache do łańcucha WebClienta aby nie operować na dziko w mono lambdach.
            .flatMap(last -> {
                // last powinien zawierac ostatnią odpowiedz z hosta + zipowany cache
                List<HostQuery> hostQueries = last.getT2();
                // tu sobie przetworzyć sobie odpowiedzi według uznania.
                hostQueries.forEach(System.out::println);
                return ServerResponse.ok().build();
            });
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1