At the moment usage of StreamingResponseBody
implies asynchronous processing on background thread.
Often the handler function is essentially synchronous and could easily be executed in request handler without spawning async task and all the implied complexity such as more complex exception and timeout handling as well as wasting extra thread.
While it's possible to simply grab ServletResponse and write to output stream, you're then losing the ability to use nice ResponseEntity API and related classes.
A way to configure Synchronous execution would be useful IMHO.
Comment From: quaff
I have a related use case
@Transactional(readOnly = true)
public ResponseEntity<StreamingResponseBody> download(@SortDefault(sort = "id") Sort sort) {
return ResponseEntity.ok(outputStream -> {
try (Stream<User> all = userRepository.findBy(sort)) {
// streaming instead of query all into List
}
});
}
Although I mark method with @Transactional(readOnly = true)
, It still raise an exception because the actual process is on background thread.
org.springframework.dao.InvalidDataAccessApiUsageException: You're trying to execute a streaming query method without a surrounding transaction that keeps the connection open so that the Stream can actually be consumed. Make sure the code consuming the stream uses @Transactional or any other way of declaring a (read-only) transaction.
Comment From: bclozel
Closing due to lack of interest from the community.