title: Spring Web-Flux: How to return a Flux to a web client on request?
body: <p>We are working with spring boot 2.0.0.BUILD_SNAPSHOT and spring boot webflux 5.0.0 and currently we cant transfer a flux to a client on request.</p>
  
  <p>Currently I am creating the flux from an iterator:</p>
  
  <pre><code>public Flux&lt;ItemIgnite&gt; getAllFlux() {
      Iterator&lt;Cache.Entry&lt;String, ItemIgnite&gt;&gt; iterator = this.getAllIterator();
  
      return Flux.create(flux -&gt; {
          while(iterator.hasNext()) {
              flux.next(iterator.next().getValue());
          }
      });
  }
  </code></pre>
  
  <p>And on request I am simply doing:</p>
  
  <pre><code>@RequestMapping(value="/all", method=RequestMethod.GET, produces="application/json")
  public Flux&lt;ItemIgnite&gt; getAllFlux() {
      return this.provider.getAllFlux();
  }
  </code></pre>
  
  <p>When I now locally call <code>localhost:8080/all</code> after 10 seconds I get a <code>503</code> status code. Also as at client when I request <code>/all</code> using the <code>WebClient</code>:</p>
  
  <pre><code>public Flux&lt;ItemIgnite&gt; getAllPoducts(){
      WebClient webClient = WebClient.create("http://localhost:8080");
  
      Flux&lt;ItemIgnite&gt; f = webClient.get().uri("/all").accept(MediaType.ALL).exchange().flatMapMany(cr -&gt; cr.bodyToFlux(ItemIgnite.class));
      f.subscribe(System.out::println);
      return f;
  
  }
  </code></pre>
  
  <p>Nothing happens. No data is transferred.</p>
  
  <p>When I do the following instead:</p>
  
  <pre><code>public Flux&lt;List&lt;ItemIgnite&gt;&gt; getAllFluxMono() {
      return Flux.just(this.getAllList());
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@RequestMapping(value="/allMono", method=RequestMethod.GET, produces="application/json")
  public Flux&lt;List&lt;ItemIgnite&gt;&gt; getAllFluxMono() {
      return this.provider.getAllFluxMono();
  }
  </code></pre>
  
  <p>It is working. I guess its because all data is already finished loading and just transferred to the client as it usually would transfer data without using a flux.</p>
  
  <p>What do I have to change to get the flux streaming the data to the web client which requests those data?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have data inside an <a href="https://ignite.apache.org/features/sql.html" rel="nofollow noreferrer">ignite cache</a>. So my <code>getAllIterator</code> is loading the data from the ignite cache:</p>
  
  <pre><code>public Iterator&lt;Cache.Entry&lt;String, ItemIgnite&gt;&gt; getAllIterator() {
      return this.igniteCache.iterator();
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>adding <code>flux.complete()</code> like @Simon Baslé suggested:</p>
  
  <pre><code>public Flux&lt;ItemIgnite&gt; getAllFlux() {
      Iterator&lt;Cache.Entry&lt;String, ItemIgnite&gt;&gt; iterator = this.getAllIterator();
  
      return Flux.create(flux -&gt; {
          while(iterator.hasNext()) {
              flux.next(iterator.next().getValue());
          }
  
          flux.complete(); // see here
      });
  }
  </code></pre>
  
  <p>Solves the <code>503</code> problem in the browser. But it does not solve the problem with the <code>WebClient</code>. There is still no data transferred.</p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>using <code>publishOn</code> with <code>Schedulers.parallel()</code>:</p>
  
  <pre><code>public Flux&lt;ItemIgnite&gt; getAllFlux() {
      Iterator&lt;Cache.Entry&lt;String, ItemIgnite&gt;&gt; iterator = this.getAllIterator();
  
      return Flux.&lt;ItemIgnite&gt;create(flux -&gt; {
          while(iterator.hasNext()) {
              flux.next(iterator.next().getValue());
          }
  
          flux.complete();
      }).publishOn(Schedulers.parallel());
  }
  </code></pre>
  
  <p>Does not change the result.</p>
  
  <p>Here I post you what the WebClient receives:</p>
  
  <pre><code>value :[Item ID: null, Product Name: null, Product Group: null]
  complete
  </code></pre>
  
  <p>So it seems like he is getting One item (out of over 35.000) and the values are null and he is finishing after.</p>