Open akarnokd opened 8 years ago
Gem 22 handles the case when there is a subscriber but it hasn't called request()
yet. fromCallable
runs immediately but defers the emission. This gem defers the execution because the front just
emits only when requested, then that value triggers the execution of the callable
Here's the code from Gem 22 (with a concrete supplier and a log instead of using sysout):
Callable<String> callable = () -> "foo";
Mono.just("irrelevant").log().map(unused -> {
try {
return callable.call();
} catch (Exception ex) {
throw Exceptions.bubble(ex); // or Exceptions.propagate(ex)
}
}).subscribe(log::info, Throwable::printStackTrace);
and here's the output:
10:06:25.504 [main] INFO reactor.core.publisher.FluxLog - onSubscribe(reactor.core.util.ScalarSubscription@5442a311)
10:06:25.506 [main] INFO reactor.core.publisher.FluxLog - request(unbounded)
10:06:25.506 [main] INFO reactor.core.publisher.FluxLog - onNext(irrelevant)
10:06:25.506 [main] INFO com.example.MonoFeaturesTests - foo
10:06:25.506 [main] INFO reactor.core.publisher.FluxLog - onComplete()
Here's the vanilla fromCallable()
(as I understood Gem 22 this should be different)
Mono.fromCallable(callable).log().subscribe(log::info, Throwable::printStackTrace);
and the output:
10:05:13.317 [main] INFO reactor.core.publisher.FluxLog - onSubscribe(reactor.core.subscriber.DeferredScalarSubscriber@1794d431)
10:05:13.318 [main] INFO reactor.core.publisher.FluxLog - request(unbounded)
10:05:13.318 [main] INFO reactor.core.publisher.FluxLog - onNext(foo)
10:05:13.318 [main] INFO com.example.MonoFeaturesTests - foo
10:05:13.318 [main] INFO reactor.core.publisher.FluxLog - onComplete()
What's the difference?
Updated the gem to show the effect if the request is actually delayed.
I see, thanks.
From @dsyer