Use Mono.create instead of Mono.fromCallable

It is better to poll message from `QueueChannel`
when ever a reactive request happens.
The `Mono.fromCallable()` operator does poll on subscription, not request
and caches the value.
However we may lose such a value in between.

* Use `Mono.create()` with its `monoSink.onRequest()` callback in the
`adaptPollableChannelToPublisher()` implementation to defer `inputChannel.receive()`
until an on demand request downstream
This commit is contained in:
Artem Bilan
2020-03-02 16:57:29 -05:00
parent cdbaf18664
commit e6e9f45164

View File

@@ -73,7 +73,9 @@ public final class MessageChannelReactiveUtils {
@SuppressWarnings("unchecked")
private static <T> Publisher<Message<T>> adaptPollableChannelToPublisher(PollableChannel inputChannel) {
return Mono.fromCallable(() -> (Message<T>) inputChannel.receive(0))
return Mono.<Message<T>>create(monoSink ->
monoSink.onRequest(value ->
monoSink.success((Message<T>) inputChannel.receive(0))))
.subscribeOn(Schedulers.boundedElastic())
.repeatWhenEmpty(it -> it.delayElements(Duration.ofMillis(100))) // NOSONAR - magic
.repeat();