Reactive Programming

Reactive Programming

Before getting into "reactive programming", we must understand the history of programming and know how we came upon today's programming paradigm shift. The concepts of advanced programming have been around for years, even decades, but our world of physical limitations have been changing over time. Today, the number of users on distributed, computational devices is basically everyone on the planet. With the desire to let every person use a program (giving life and value to a device), the program must compute at a speed that appears instant to each user.

So, reactive is simply another form of "event-driven" programming (i.e. "request driven"). Yet, the difference is that the program "reacts" to the event through functional programming instead of imperative programming. In functional programming, the program does its best to NOT rely on *memory* and maximize CPU thread to return the result. That is, try not to store data in order to process (i.e. compute) the data result that will be returned.

Another important concept of "reactive programming" is non-blocking calls. This usually relates to a call stack of functions that an event has triggered. Hence, a function that calls another function must execute in its order of function call (i.e. function A calls function B and so function A must wait for function B to complete before continuing its computation in function A). With reactive programming, the data input is computed on the spot or passed to another function without waiting. If a call is made, then the function keeps a reference for the function called (in a queue-like approach rather than a stack-like approach) and completes computation as-is. So function A calls function B and exits, and function B will call function A with its result at the function A reference (where to continue) to finish computation on data returned. In a short recap, there are not holding of threads when programming in the reactive approach.

Videos

Articles

Here are some articles that might be helpful in reviewing Reactive Programming:

References


Spring Webflux - a reactive programming framework in RxJava

https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-server-choice

Reactor

https://projectreactor.io/docs/core/release/reference/index.html

Flux

https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html

Mono

https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html

StepVerifier

JavaDoc

https://javadoc.io/static/io.projectreactor.addons/reactor-test/3.0.7.RELEASE/reactor/test/StepVerifier.Step.html

Guide

https://projectreactor.io/docs/core/release/reference/index.html#_testing_a_scenario_with_code_stepverifier_code


AssertJ library

https://assertj.github.io/doc/

BlockHound - catch any blocking calls within non-blocking threads

https://github.com/reactor/BlockHound

Learn Reactor

https://projectreactor.io/learn

Playgrounds

Only do this playground if you have nothing else to do with your life. Some of the examples are just largely incomplete and leaves you guessing for hours (while leveraging the internet) on how to actually solve for what this author is asking.

Course: Reactive Programming with Reactor 3

https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/Intro

similar (if not identical playground) @ https://www.codingame.com/playgrounds/929/reactive-programming-with-reactor-3/transform

be sure to understand the imported User (which helps to avoid guessing at the methods encapsulated):

https://github.com/reactor/lite-rx-api-hands-on/blob/master/src/main/java/io/pivotal/literx/domain/User.java

Why are we making this so hard to implement (and I just used the last answer with only 'create(flux)' and it worked)?
https://stackoverflow.com/questions/48485100/testing-flux-with-stepverifier


Tips

When solving the problem to transform the User data by using the method #asyncCapitalizeUser, there's this SO answer:

  • https://stackoverflow.com/questions/45231901/transform-flux-to-mono-execute-method-on-mono-and-transform-back-to-flux

However, I also notice we can answer in these two ways...

1)  return flux.flatMap( user -> asyncCapitalizeUser(user) );

2)  return flux.flatMap( this:: asyncCapitalizeUser );

Which is better will depend. But, know these two ways using the "lambda expression" (in first solution) and using the "method reference operator" (in second solution). Both solutions are features added into Java 8.

Here's another explanation on SO and Geek:

  • https://stackoverflow.com/questions/20001427/double-colon-operator-in-java-8
  • https://www.geeksforgeeks.org/double-colon-operator-in-java/

Author: Reactor

(and notice that the course is 4 years old)

https://tech.io/users/2070956/Reactor


Tutorials

https://levelup.gitconnected.com/reactive-asynchronous-programming-in-java-using-reactor-core-part-1-cd516dd579fe

Following link includes great explanation of using Mono#flatMap()

https://levelup.gitconnected.com/reactive-asynchronous-programming-in-java-using-reactor-core-part-2-e9c6caeb8833

https://www.baeldung.com/reactor-combine-streams

No comments:

Post a Comment