title: How can you keep calling API until you get a certain value?
body: <p>I'm new to reactive programming and WebClient. I would like to repeatedly call an API until I get a specific value from the API (the API I am calling is a regular API that's configured for event streaming). Once I get the desired value, I would take an action.</p>
  <p>I attempted to solve this with Flux interval, but haven't had much luck. I'm not sure if there is something wrong with my code, or if I am taking the wrong approach.</p>
  <pre><code>public Flux&lt;TransactionStatus&gt; watchAndTransact(Duration intervalDuration, Duration watchDuration,
                                                  String toAddress, String fromAddress) {
      return Flux.interval(intervalDuration)
              .take(watchDuration)
              .flatMap(aFloat -&gt; getBalance(fromAddress))
              //.retryWhen(getBalance(fromAddress).block() &lt; 0)
              .flatMap(balance -&gt; {
  
                  log.info(&quot;Checking balance {}&quot;, Instant.now());
                  //If we find that an amount was deposited into the deposit account
                  if (balance &gt; 0) {
                      log.info(&quot;Balance Present {}&quot;, balance.toString());
  
                      //Build a transaction
                      Transaction transaction = new Transaction(fromAddress, toAddress, balance);
  
                      //Post the transaction
                      return postTransactionService.postTransaction(transaction);
  
                  }
                  return Mono.empty();
              }).switchIfEmpty(s -&gt; s.onError(new RuntimeException()));
  }
  </code></pre>
  <p>I am getting the balance from the API using the code below:</p>
  <pre><code>public Mono&lt;AddressInfo&gt; getAddressInfo(String address) {
      log.info(&quot;Pulling information on the address {}&quot;, address);
  
      return webConfig.api
              .get()
              .uri(&quot;/addresses/{address}&quot;, address)
              .retrieve()
                  .onStatus(HttpStatus::is4xxClientError,
                          error -&gt; Mono.error(new NotFoundResourceException(&quot;Couldn't find a record&quot;)))
                  .onStatus(HttpStatus::is5xxServerError,
                          error -&gt; Mono.error(new BadRequestException(&quot;Server isn't responding&quot;)))
              .bodyToMono(AddressInfo.class)
              .onErrorReturn(new AddressInfo(0f, EMPTY_LIST));
  }
  
  public Mono&lt;Float&gt; getBalance(String address) {
      return getAddressInfo(address).map(AddressInfo::getBalance);
  }
  </code></pre>
  <p>This is how I setup my webclient:</p>
  <pre><code>@Bean
  @LoadBalanced
  public WebClient.Builder loadBalancedWebClientBuilder() {
  
      return WebClient.builder();
  }
  
  public WebClient dbClient = loadBalancedWebClientBuilder()
          .baseUrl(gatewayUrl+&quot;/api/v2/data&quot;)
          .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
          .build();
  
  public WebClient apiClient = loadBalancedWebClientBuilder()
          .baseUrl(url)
          .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
          .build();
  </code></pre>
  <p><strong>How Can I call a regular API repeatedly using Spring Boot Reactive?</strong></p>