title: Streaming a large Mongo result thru OpenCSV to RestController timing out
body: <p>I am attempting to stream a large Mongo query result as a CSV file to a web front end using ReactiveMongoTemplate, OpenCSV and <code>ResponseEntity&lt;Mono&lt;Resource&gt;&gt;</code>, however it seems entire file must still be loaded into memory before it's served to the client, resulting in a timeout.</p>
  <p>My controller request handler</p>
  <pre><code>@Autowired
  CsvWorker csvWorker;
  
  @RequestMapping(value = &quot;/csv&quot;, method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
  public ResponseEntity&lt;Mono&lt;Resource&gt;&gt; getCaseReportCsv(
          HttpServletResponse response) {
      
      return ResponseEntity.ok()
              .header(HttpHeaders.CONTENT_DISPOSITION,  &quot;attachment; filename=file.csv&quot;)
              .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
              .body(csvManager.generateCsv()
                      .flatMap(x -&gt; {
                          Resource resource = new InputStreamResource(x);
                          return Mono.just(resource);
                      }));
  
  }
  </code></pre>
  <p>Within CsvWorker, I use this example to create the CSV writer: <a href="https://medium.com/@victortemitope95/how-to-write-and-download-a-csv-file-in-spring-webflux-5df8d817a597" rel="nofollow noreferrer">https://medium.com/@victortemitope95/how-to-write-and-download-a-csv-file-in-spring-webflux-5df8d817a597</a></p>
  <pre><code>public Mono&lt;ByteArrayInputStream&gt; generateCsv() {
              
      return Mono.fromCallable(() -&gt; {
          try {
              ByteArrayInOutStream stream = new ByteArrayInOutStream();
              OutputStreamWriter streamWriter = new OutputStreamWriter(stream);
              CSVWriter writer = new CSVWriter(
                      streamWriter, 
                      CSVWriter.DEFAULT_SEPARATOR, 
                      CSVWriter.NO_QUOTE_CHARACTER, 
                      CSVWriter.NO_ESCAPE_CHARACTER, 
                      CSVWriter.DEFAULT_LINE_END);
  
              CsvMappingStrategy&lt;CsvRow&gt; mappingStrategy = new CsvMappingStrategy&lt;CsvRow&gt;();
              mappingStrategy.setType(CsvRow.class);
  
              StatefulBeanToCsv&lt;CsvRow&gt; beanToCsv = new StatefulBeanToCsvBuilder&lt;CsvRow&gt;(writer)
                      .withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
                      .withMappingStrategy(mappingStrategy)
                      .withSeparator(',')
                      .build();
  
              beanToCsv.write(historyRepo.findAll().map(cR -&gt; new CaseReportCsvRow(cR)).toStream());
              streamWriter.flush();
              return stream.getInputStream();
          }
          catch (Exception e) {
              throw new RuntimeException(e);
          }
  
      }).subscribeOn(Schedulers.boundedElastic());
      
  }
  </code></pre>
  <p>This results in a proper CSV being downloaded, but as the row count increases, the time before the download begins increases, and at a certain point an async timeout exception is thrown: <code>org.springframework.web.context.request.async.AsyncRequestTimeoutException: null</code>.</p>
  <p>Increasing the timeout helps, but ideally any sized file would be exportable, with the client download beginning as soon as the request was sent.</p>
  <p>Why is this code not creating an output stream to the client the way I'm expecting?</p>