Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:

>> GET ACCESS NOW

1. Overview

In our Intro to Project Reactor, we learned about Mono<T>, which is a publisher of an instance of type T.

In this quick tutorial, we'll demonstrate both a blocking and non-blocking way to extract from the Monoblock and subscribe.

2. Blocking Way

In general, Mono completes successfully by emitting an element at some point in time.

Let's start with an example publisher Mono<String>:

Mono<String> blockingHelloWorld() {
    return Mono.just("Hello world!");
}

String result = blockingHelloWorld().block();
assertEquals("Hello world!", result);

Here, we're blocking the execution as long as the publisher doesn't emit the value. However, it can take any amount of time to finish.

To get more control, we'll set an explicit duration:

String result = blockingHelloWorld().block(Duration.of(1000, ChronoUnit.MILLIS));
assertEquals(expected, result);

If the publisher doesn't emit a value within the set duration, a RuntimeException is thrown.

Additionally, Mono could be empty and the block() method above would return null. In that case, we could make use of blockOptional:

Optional<String> result = Mono.<String>empty().blockOptional();
assertEquals(Optional.empty(), result);

In general, blocking contradicts the principles of reactive programming. It's highly discouraged to block the execution in reactive applications.

So now let's see how to get the value in a non-blocking way.

3. Non-Blocking Way 

First of all, we should subscribe in a non-blocking way using the subscribe() method. We'll also specify the consumer of the final value:

blockingHelloWorld()
  .subscribe(result -> assertEquals(expected, result));

Here, even if it takes some time to produce the value, the execution immediately continues without blocking on the subscribe() call.

In some cases, we'll want to consume the value in intermediate steps. Therefore, we can use an operator to add behavior:

blockingHelloWorld()
  .doOnNext(result -> assertEquals(expected, result))
  .subscribe();

4. Conclusion

In this brief article, we explored two ways of consuming a value produced by Mono<String>.

As always, the code example can be found over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!