Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Mono/Flux.fromCallable and Mono.defer

I usually have the requirement of generating a Mono/Flux whose values will be generated/calculated on subscription time. For this both fromCallable and defer operators seem to be fine.

I can´t see the difference clearly in the javadoc:

FromCallable:

public static Mono fromCallable(Callable supplier)

Create a Mono producing its value using the provided Callable. If the Callable resolves to null, the resulting Mono completes empty.

Defer:

public static Mono defer(Supplier> supplier)

Create a Mono provider that will supply a target Mono to subscribe to for each Subscriber downstream.

Could you explain if both can be used for this requirement and what exact difference there is between them?

like image 704
codependent Avatar asked Feb 05 '20 14:02

codependent


People also ask

What is the difference between mono and flux?

Mono — A publisher that can emit 0 or 1 element. Flux — A publisher that can emit 0.. N elements.

What does mono defer do?

What Is the Mono. defer Method? Here, defer takes in a Supplier of Mono publisher and returns that Mono lazily when subscribed downstream.

What is mono and flux in Java?

A Flux object represents a reactive sequence of 0.. N items, while a Mono object represents a single-value-or-empty (0..1) result. This distinction carries a bit of semantic information into the type, indicating the rough cardinality of the asynchronous processing.

What is transformDeferred?

The transformDeferred operator is similar to transform and also lets you encapsulate operators in a function.

What is the difference between mono and flux?

And a Flux<T>, which can return zero to many, possibly infinite, results before completing. The first type of publisher is a Mono. The Mono API allows you to emit only a single value, after which it will immediately complete. This means that the Mono is the reactive counter-part of returning a simple object, or an Optional.

What is the mono defer method?

What Is the Mono.defer Method? We can create a cold publisher which can produce at most one value using defer method of the Mono. Let's look at the method signature: Here, defer takes in a Supplier of Mono publisher and returns that Mono lazily when subscribed downstream.

What is the mono API?

The Mono API allows you to emit only a single value, after which it will immediately complete. This means that the Mono is the reactive counter-part of returning a simple object, or an Optional. For example, let’s say we have the following code:

What is mono in Java 8?

The Mono API allows you to emit only a single value, after which it will immediately complete. This means that the Mono is the reactive counter-part of returning a simple object, or an Optional. For example, let’s say we have the following code: In Java 8, we could rewrite this as:


1 Answers

Mono.defer is usually used when you already have a Mono from a third-party source, but you want to delay its creation until subscription time because during its creation something is done eagerly.

Consider the following example:

public static void main(String[] args)
{
    callExternalService()
            .repeat(3)
            .subscribe();
}

private static Mono<?> callExternalService()
{
    System.out.println("External service is called.");

    return Mono.just("result");
}

At first glance, you'd think this example has no issues but when you check the output, you can see that External service is called is only printed once instead of the expected four (one original + three repeats) because it is executed outside of the scope of the returned Mono.

However, if you defer the execution of the Mono, it will be printed four times as expected:

Mono.defer(() -> callExternalService())
    .repeat(3)
    .subscribe();

Another use case for defer is when you want to test a repeat/retry/re-subscribe logic and you want to have different return values for the mocked service which returns the Mono.

In conclusion, it is indeed very similar to fromCallable, but mostly used when you already have a method which returns a Mono and does something eager. If you have complete control over the code, then you are completely fine with fromCallable.

like image 74
Martin Tarjányi Avatar answered Oct 10 '22 06:10

Martin Tarjányi