Closed
Description
- Framework version: 1.4
- Implementations: SpringBoot 2.x
Scenario
WebFlux method that returns a flux of multiple objects by wrapping a CompletableFuture
. Issue originally reported by @cesardrk in #239
Expected behavior
Response output stream should remain open until the Flux
is complete and has delivered all messages.
Actual behavior
When creating a flux from a CompletableFuture
, Spring's ServletHttpHandlerAdapter
calls flush right away, causing the framework to release the latch and return an empty response. Further, it seems that the adapter never calls the close
method on the output stream making it pointless to move the latch release logic.
Steps to reproduce
This method will return and empty response right away:
@RequestMapping(path = "/entity", method = RequestMethod.GET)
ResponseEntity<Flux<String>> messageEntity() {
return ResponseEntity.ok().body(Mono.fromFuture(CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return MESSAGE;
})
).flux());
}
This method will succeed:
@RequestMapping(path = "/asyncflux", method = RequestMethod.GET)
Flux<String> futureFlux() {
return Flux.create((sink) -> {
System.out.println("Supply async");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
sink.next(MESSAGE);
});
}