- Spring 5 provides Spring WebFlux which helps to create web application that helps to create reactive web applications.
Be can handle more request(using pub-sub model)
Use more lambdas
Data is transfer as event driven ---whenever changes happen pubhlisher publish the event
Add limitation on data from database
Reactive programming is a programming paradigm where the focus is on developing asynchronous and non-blocking applications in an event-driven form.
Publisher
interface Publisher<T>
{
public void subsribe(Subscribe<? super T>s)
}
Subsriber
interface Subsriber<>{
onSubscribe(Subscription s)
onNext(T t)
onError(Throwable t)
onComplete()
}
Subscription
interface Subscription{
cancel()
request(long var)
}
Processor
- Reactor
- RxJava
- Jdk9 Flow Reactor Stream
Project reactor is library that implements reactive specification for building non-blocking and asynchronous applications on JVM
-
Flux - 0...N elements
-
Mono - return 0 or 1 element
- Creating Mono and Flux
Mongo<String> mono=Mono.just("data")
- Error
Mono.just("data").then(Mono.error(new RuntimeException("ERROR")))
- zip() merge mono provide Mono of Tuple
- withZip()
combines the result from this mono and another mono.
var m1=Mono.just("First");
var m2=Mono.just("Second");
Mono<Tuple2<String, String>> tuple2Mono = m1.zipWith(m2);
tuple2Mono.subscribe(values->{
System.out.println(values.getT1());
System.out.println(values.getT2());
});
- map()
transform the value emmited by current using syn function
var m1 = Mono.just("First Project working");
var m2 = Mono.just(124);
Mono<String> changeMono = m1.map(item -> item.toUpperCase());
changeMono.subscribe(System.out::println);
-
flatMap() and flatMapMany()
-
flatMap() - transform the value emmited by current mono async, returning the value emmited by another mono (change value type possible )
-
flatMapMany() - transform this mono into publisher , then change to return a many value that is Flux.
var m1 = Mono.just("First");
var m2 = Mono.just(124);
Mono<String> integerMono = m1.flatMap(item1 -> m2.map(item2 -> item2.toString().concat(item1)));
integerMono.subscribe(System.out::println);
var m1 = Mono.just("First Project working");
var m2 = Mono.just(124);
- concatWith()
join to mono and provide Flux
Flux<String> secondString = m1.concatWith(Mono.just("second string"));
-
deleyElement() to provide delay on onNext()
-
map
-
filter
-
flatMap(flatMapMany(mono to flux)) mapping convert each element to flux of element.(Asynchronous)
-
concatMap
preserve the order
-
delayElements- delay the elements
-
transform -- take functional interface to transform the data
-
defaultIfEmpty--> provide the default data if empty
defaultIfEmpty(pass the default value)
-
switchIfEmpty-
switch to new set of data switchIfEmpty(passflux)
-
concat(static) & concatWith(instance method) (sync)
-
merge & and mergeWith (async)
-
mergeSequential
working with different type reactive type
-
zip & zipWith
Flux.zip(flux1,flux2,(first,second)->{ return first+second; }).log()
SideEffect operators -> dont change the actual behaviour
- onDoNext()
- when next is called
- doOnSuscribe()
- when subscribe
Exception Handling