Exercises
In these exercises, you’ll practice some of the concepts taught in this module.
First, either create a new Java project, adding the reactor-bom
and reactor-core
dependencies to your build file (Maven or Gradle) or use the stub you can find at: https://github.com/eh3rrera/project-reactor-course/tree/main/03/before/reactor-demo-exercises.
I’ll give you the instructions (and sometimes hints) so you can put all the code together in the main
method of a class and observe the output.
Here you can find the solution for the coding exercises: https://github.com/eh3rrera/project-reactor-course/tree/main/03/after/reactor-demo-exercises.
Exercise 1
Given a Flux
of Integer
values, your task is to transform each value to its hex string representation.
- First, create a class named
Exercise01
with amain
method. - Create a
Flux
that emits a sequence ofInteger
values. Let’s say, from10
to14
. - Apply the map method to the Flux to transform each emitted value to its hex string representation using something like
String.format("0x%08X", value)
. - Subscribe to the transformed
Flux
, printing the emitted items. - Run the
Exercise01
class and analyze the output.
Exercise 2
Given a Mono
that emits an Integer
value, your task is to transform the emitted value by fetching its square from a separate asynchronous method that returns a Mono
.
- First, create a class named
Exercise02
with amain
method. - Create a
Mono
that emits a singleInteger
value. - Implement the following method so that it returns a
Mono<Integer>
representing the square of the input number:private static Mono<Integer> getSquareAsync(Integer value) { // TODO: Create a Mono publisher that emits the square of the input number return null; }
- Apply the appropriate operator to the
Mono
created in step 2 to transform the emitted value using the asynchronous method created in step 3. - Subscribe to the transformed
Mono
, printing the emitted item. - Run the
Exercise02
class and analyze the output.
Exercise 3
Given a Flux
that emits a sequence of Integer
values, your task is to transform each emitted value into a Flux
that emits the value and its square.
- First, create a class named
Exercise03
with amain
method. - Create a
Flux
that emits a sequence ofInteger
values. Let’s say from 1 to 5. - Implement the following method so that it returns a
Flux<Integer>
that emits the input number and its square:private static Flux<Integer> getNumberAndSquare(Integer value) { // TODO: Create a Flux publisher that emits the input number and its square return null; }
- Apply the appropriate operator to the
Flux
created in step 2 to transform each emitted value by fetching its associatedFlux
using the method created in step 3. - Subscribe to the transformed
Flux
, printing the emitted items. - Run the
Exercise03
class and analyze the output.
Exercise 4
Modify the previous example so that the method getNumberAndSquare
returns an Iterable
, so all elements can be played sequentially:
private static Iterable<Integer> getNumberAndSquare(Integer value) {
// TODO: Create an Iterable with the input number and its square
}
Exercise 5
In this exercise, you’ll use the map
and flatMap
operators to transform a Flux
sequence.
- First, create a class named
Exercise05
with amain
method. - Create a
Flux
that emits a sequence ofInteger
values. Let’s say from 1 to 5. - Use a
map
operator to triple each emitted value. - Next, use a
flatMap
operator to create a newFlux
that emits the tripled value and its square. - Subscribe to the transformed
Flux
, printing the emitted items. - Run the
Exercise05
class and analyze the output.