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/07/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/07/after/reactor-demo-exercises.
Exercise 1
In this coding exercise, you’ll create a Flux that uses the contextWrite(ContextView) method to enrich the context with additional data.
- Given the following code:
import reactor.core.publisher.Flux; public class Exercise01 { public static void main(String[] args) { Flux<Integer> numbers = Flux.range(1, 5); String key = "divider"; double value = 10.0; double defaultValue = 1.0; // TODO: Divide each emitted number by the "divider" value from the context Flux<Double> contextualizedNumbers = null; // TODO: Add to the Context the key-value pair with the variables of the same name // TODO: Subscribe to contextualizedNumbers } } - Use the
transformDeferredContextualmethod to divide each emitted number ofnumbersby the"divider"value from the context, using the default value defined by the variabledefaultValue. - Use the
contextWrite(ContextView)method to enrich the context with a key-value pair, using the variableskeyandvalue. - Subscribe to
contextualizedNumbersprinting the emitted values. - Run the
Exercise01class and analyze the output.
Exercise 2
In this coding exercise, you’ll create a Flux that uses the contextWrite(Function<Context, Context>) method to enrich the context with additional data.
- Given the following code:
import reactor.core.publisher.Flux; public class Exercise02 { public static void main(String[] args) { Flux<Integer> numbers = Flux.range(1, 5); String key = "multiplier"; int value = 3; int defaultValue = 1; // TODO: Multiply each emitted number by the "multiplier" value from the context Flux<Integer> contextualizedNumbers = null; // TODO: Add to the Context the key-value pair with the variables of the same name // TODO: Subscribe to contextualizedNumbers } } - Use
flatMapandMono.deferContextualto multiply each emitted number ofnumbersby the"multiplier"value from the context, using the default value defined by the variabledefaultValue. - Use the
contextWrite(Function<Context, Context>)method to enrich the context with a key-value pair, using the variableskeyandvalue. - Subscribe to
contextualizedNumbersprinting the emitted values. - Run the
Exercise02class and analyze the output.
Exercise 3
In this coding exercise, you’ll use the transformDeferredContextual method to apply a transformation to a Mono while considering the context.
- Given the following code:
import reactor.core.publisher.Mono; public class Exercise03 { public static void main(String[] args) { Mono<String> greetingMono = Mono.just("Hello"); String key = "username"; // TODO: Append the value of the "username" key from the context to the emitted string Mono<String> contextualizedGreetingMono = null; // TODO: Add to the Context the value "Alice" // TODO: Subscribe to contextualizedNumbers // TODO: Add to the Context the value "Bob" // TODO: Subscribe again to contextualizedNumbers } } - Use
transformDeferredContextualto append the value of the"username"key from the context to the string emitted bygreetingMono. - Use the
contextWrite(Function<Context, Context>)method to enrich the context with a key-value pair, using the variablekeyas key, and"Alice"as value. - Subscribe to
contextualizedNumbersprinting the emitted value. - One more time, use the
contextWrite(Function<Context, Context>)method to enrich the context with a key-value pair, using the variablekeyas key, and"Bob"as value. - Subscribe again to
contextualizedNumbersprinting the emitted value. - Run the
Exercise03class and analyze the output.