Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luke10x/1cb8303dc7be5fc3eb3cd75040baf012 to your computer and use it in GitHub Desktop.
Save luke10x/1cb8303dc7be5fc3eb3cd75040baf012 to your computer and use it in GitHub Desktop.
=======================================================================================================
=======================================================================================================
All-about-reactive-programming-java
=======================================================================================================
=======================================================================================================
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.
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.
-- 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> {
}
# 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.
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.
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.
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