Let’s go reactive with
JAVA Presented By :
Vinisha Sharma
Software consultant - Knoldus Inc
© 2018 Minimal – Presentation template
2
Agenda
● Reactive Programming
● Reactive Streams
Specification
● Project Reactor
● Mono/ Flux
● Spring Boot Webflux
● Demo
3
05
4
❑ Programming paradigm oriented around - data flows and
propagation of change.
❑ Asynchronous and Non Blocking.
❑ Data flow as an Event/Message Driven stream
Reactive Programming
5
Reactive Manifesto
6
Reactive
Streams
7
Reactive streams
Reactive Streams is an initiative to provide a standard for asynchronous
stream processing with non-blocking back pressure
Functional Style Code
● Java 8 Streams API
● Lambdas
Event Driven Streams
● Event or Message for
every result item
from Data Source
● Event or Message for
Completion or error
● Asynchronous and
non-blocking
Deferred Push/Pull
Model
Back Pressure
F
BE
D
8
Imperative Programming
9
Reactive Streams
Specification
10
Reactive Stream Specification
Specification or Rules for a Reactive Stream
(Created by Pivotal, Netflix, LightBend, Twitter
etc...)
11
public interface Publisher<T> {
public void subscribe(Subscriber<? super T>
s);
}
❑ Represents the Data source
❑ Database
❑ External Service, etc…..
Publisher
12
public interface Subscriber<T> {
public void onSubscribe(Subscription s);
public void onNext(T t);
public void onError(Throwable t);
public void onComplete();
}
❑ Represents the consumer of the stream data
Subscriber
13
public interface Subscription {
public void request(long n);
public void cancel();
}
Subscription
14
public interface Processor<T, R> extends
Subscriber<T>, Publisher<R> {
}
❑ A Processor represents a processing stage—which is both
a Subscriber and a Publisher.
Processor
15
Publisher/Subscriber Event Flow
16
Publisher/Subscriber Event Flow
17
Data Flow as an Event Driven Stream
18
Data Flow as an Event Driven Stream
❏ On Error
19
Data Flow as an Event Driven Stream
❏ No data
20
Back pressure
21
Project Reactor
and
Spring Webflux
22
❑ Reactor is a fully non-blocking reactive programming foundation for
the JVM, with efficient demand management (in the form of managing
"backpressure").
❑ Integrates directly with the Java 8 functional APIs, notably
CompletableFuture, Stream, and Duration.
❑ Reactive Types - Offers asynchronous sequence (MONO/FLUX) for
reactive streams
Project Reactor
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
23
❑ Publisher<T> that emits at most one item
❑ Optionally terminates with an onComplete signal or an onError signal.
Mono
24
❑ Publisher<T> that emits 0 to n items
❑ Optionally terminates with an onComplete signal or an onError signal.
Flux
25
Mono Vs Flux
Mono<Student> Flux<Student>
26
❑ Reactive-stack web framework
❑ Non Blocking and Supports Reactive Streams back
pressure
❑ Runs on such servers as Netty, Undertow, and Servlet 3.1+
containers.
Spring Webflux
27
DEMO
28
❑ https://projectreactor.io/
❑ https://github.com/reactive-streams/reactive-streams-jvm
❑ https://stackify.com/reactive-spring-5/
References
29
Thank You
30
Stay in touch
Contact Details:
vinsha.sharma@knoldus.com
or
linkedin.com/in/vinisha-sharma-26538a135

Let’s go reactive with JAVA