-
-
Save luke10x/1cb8303dc7be5fc3eb3cd75040baf012 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
======================================================================================================= | |
======================================================================================================= | |
All-about-reactive-programming-java | |
======================================================================================================= | |
======================================================================================================= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Reactive programming is a paradigm that can be used to implement building blocks, therefore its scope is within components/services. | |
EDA, being an architectural style, is all about the interaction between components/services. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) In the observer pattern, the observers are aware of the Subject. | |
The Subject maintains a record of the Observers. | |
Whereas, in publisher-subscriber, publishers and subscribers don’t need to know each other. | |
They simply communicate with the help of message queues or a broker. | |
2) In the publisher-subscriber pattern, components are loosely coupled as opposed to the observer pattern. | |
3) The observer pattern is mostly implemented synchronously, i.e. the Subject calls the appropriate method of all | |
its observers when an event occurs. The publisher-subscriber pattern is mostly implemented asynchronously (using a message queue). | |
4) The observer pattern needs to be implemented in a single-application address space. | |
On the other hand, the publisher-subscriber pattern is more of a cross-application pattern. | |
Ref = https://betterprogramming.pub/observer-vs-pub-sub-pattern-50d3b27f838c#:~:text=In%20the%20observer%20pattern%2C%20the,message%20queues%20or%20a%20broker. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ReactiveStream interfaces are provided by java to use. | |
There are multiple implementations are there Project reactor, RXJava etc. | |
ReactiveSteams - | |
imports = import org.reactivestreams.*; | |
* Reactive streams list of interfaces provided. | |
1) Publisher - | |
public interface Publisher<T> { | |
public void subscribe(Subscriber<? super T> s); | |
} | |
2) Subscriber - | |
public interface Subscriber<T> { | |
public void onSubscribe(Subscription s); | |
public void onNext(T t); | |
public void onError(Throwable t); | |
public void onComplete(); | |
} | |
3) Subscription - | |
public interface Subscription { | |
public void request(long n); | |
public void cancel(); | |
} | |
4) Processor - | |
public interface Processor<T, R> extends Subscriber<T>, Publisher<R> { | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# What is this ‘specific propagation of change’? | |
Here’s a real-life example. Say, it’s Friday and John wants to spend this evening with his friend Bob, scarfing pizza and watching one of the Star Wars episodes. Let’s outline the options he has. | |
John finishes his work. Then goes and orders the pizza, waits till it’s done. | |
Then picks up his friend. And finally (with Bob and pizza) makes it home and gets | |
down to the movie. It will be the sync approach and it will be way too long, so that | |
probably John will have wanted to call the thing off by that time. | |
John orders his pizza online, phones Bob, invites him to come. He heads home, has | |
his pizza delivered and starts watching the movie (and eating the pizza) without waiting | |
for Bob to show up. That is what can happen with the async approach. | |
John orders pizza, phones Bob, invites him to come, heads home, and gets his pizza delivered. | |
But this time, he waits until Bob comes and only after that he turns the movie on. | |
This is what the reactive approach is about. You wait till all async actions (changes)(eventloop) are completed and | |
then proceed with further actions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) https://www.infoq.com/news/2017/12/servlet-reactive-stack/#:~:text=The%20main%20difference%20between%20the,based%20on%20event%2Dloop%20mechanism.&text=A%20developer%20can%20run%20a,such%20as%20Tomcat%20or%20Jetty. | |
2) https://dzone.com/articles/what-are-reactive-streams-in-java | |
3) https://kamilszymanski.github.io/resources-utilization-in-reactive-services/ | |
4) https://dzone.com/articles/spring-webflux-eventloop-vs-thread-per-request-mod | |
5) https://www.youtube.com/watch?v=8aGhZQkoFbQ | |
6) https://singhkaushal.medium.com/spring-webflux-eventloop-vs-thread-per-request-model-a42d07ee8502 | |
7) https://medium.com/the-node-js-collection/what-you-should-know-to-really-understand-the-node-js-event-loop-and-its-metrics-c4907b19da4c#:~:text=js%20is%20an%20event%2Dbased,mechanism%20called%20an%20event%20loop. | |
8) https://www.slideshare.net/srt/reactive-programming-with-netbeans-and-java-8 | |
9) https://www.baeldung.com/java-observer-pattern -> Observer example RxJava | |
10) Eventloop - https://activej.io/eventloop.html#:~:text=Overview,asynchronous%20operations%20without%20multithreading%20overhead. | |
11) Basic Reactive streams example tuto - https://blog.softwaremill.com/how-not-to-use-reactive-streams-in-java-9-7a39ea9c2cb3 | |
12) Journey - https://blog.redelastic.com/a-journey-into-reactive-streams-5ee2a9cd7e29 | |
13) http - https://stackoverflow.com/questions/63065950/how-reactive-stream-works-with-http-what-is-reactive-http | |
14) Netty book - https://livebook.manning.com/book/netty-in-action/chapter-1/86 | |
15) End to end poc - https://thepracticaldeveloper.com/full-reactive-stack-2-backend-webflux/ | |
16) Server sent events - https://www.w3schools.com/html/html5_serversentevents.asp | |
17) Realtime project - https://tech.trivago.com/post/2021-03-16-java-reactive-programming-real-world/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
how servlet3.0 traditional vs servlet3.1 async works = ref link - https://dzone.com/articles/servlet-31spring-mvc-non-blocking-io | |
if we have servlet and mvc stack in async then why should we use webflux/reactive stack = https://dzone.com/articles/understanding-spring-reactiveintroducing-spring-we = spring servlet is async but for backward compatibility some of the code they made as blocking which can be use in our application hence they created seperate stack. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) Java Streams are pull-based with terminal operator. Java Streams can be used only once. | |
Java Streams can run in sequential or in parallel. Java Streams do not have time-related variants of methods | |
2) Reactive Streams are push-based using subscriber. Reactive Streams can be used many times. | |
They run in one thread or in another one. They have time-related variants of methods. | |
Reactive Streams is effectively push-based. It has a single mode of operation, which works like a pull-based system when the subscriber is slower and a push-based system when the subscriber is faster. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) How subscriber works in webflux -> Webflux generally perform autosubscribe for you when you return a publisher(Mono/Flux) from controller. | |
At that time internally webflux will do autosubscribe. There is a method where you can do subscribe by yourself. But according to publisher | |
interface when you subscribe a publisher you need to pass a subscriber. So in this case we aren't passing any subscriber this subscriber | |
creation part is taken care by webflux. The reactor.core.publisher.LambdaSubscriber is a private class that is actually create a subscriber. | |
And there is other overloaded methods which takes the consumer, which is nothing but webflux takes inner items from flux or mono and using those | |
it creates a subscribe. | |
Also, you can create your own subscriber(By implementing subscriber interface) and pass it it subscribe method as parameter. | |
2) Flatmap for flux -> Flatmap is basically Transform the elements emitted by this Flux asynchronously into Publishers, | |
then flatten these inner publishers into a single Flux through merging,which allow them to interleave. | |
3) Webflux flow | |
request->reactive(servlet/netty)->reactiveprogram(my-api-code)->reactiverestapicall(webclient)->thirdpartyservices(externalmicroservices) | |
Here data will come from request, initially data will be passed through reactive components like | |
webclient,my-api-code or reactive(servlet) | |
4) My observation on event stream of server site events in webflux using db - | |
I have created flux that will return events after 1 seconds of gap, Then I hit the API which matches suppose 20 items. So I will get all | |
items in 20 seconds, In between of those seconds I have ingested Items from outside of source so, for that query Items matches are 25. and in | |
the middle of 10th second I have ingested but still I receivd total 20 items not 25. So my observation is this webflux fetchs all items | |
from db and this event stream release that item after 1 second gap | |
4) How using webflux or pub-sub pattern reactive approach was achieve for http call - | |
Step 1 - Event loop thread will come, this thread will collect all lazy actions as kind of definitions(From controller to repo). These action | |
will return publisher(Mono/Flux). | |
Step 2 - At last it will call a subscribe method and thread will be release. Action will be passed to internal threads and those will perform | |
all the action that is defined. | |
Step 3 - These publisher will call subscriber methods(Of course they can call, because in publisher interface we have subscriber instance) | |
. so according to error or response. Subscriber method will invoked. | |
Step 4 - Once all actions done, result will go to event queue and event loop will pick result and passed it to outside world. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment