Issue
The API responds with the following "application/json"
body type construct:
{
"transactions": [
{
"transaction": "1",
},
{
"transaction": "2",
},
{
"transaction": "3",
}
]
}
essentially I mapped it to..
public class Transactions {
private List<Transaction> transactions;
...
}
Currently I retrieve it as a Mono<Transactions>
, but it has drawbacks - it buffers whole list in memory.
With Spring Reactive API
, is it possible to process list of transactions as Flux in a way that it does not get buffered in memory as a whole?
Many thanks!
Solution
After receiving more information in the comments this is the answer to the question.
If a webflux application is making a blocking call to a non-webflux api, measures should be made to wrap the call in a Mono#fromCallable
and placed on it's own thread so that it does not interfere with the rest of the non blocking threads. All this is documented in the reactor documentation under their faq
A blocking call will always return a single response, a Mono
so in the above case it will return a List<Transaction>
.
Since we already have the full list after the blocking call, most of the time there is no actual purpose to put this in a flux and then push these one by one out to the client.
So returning a Mono<List<Transaction>>
to the calling client is probably the way to go.
Answered By - Toerktumlare
Answer Checked By - Mildred Charles (JavaFixing Admin)