The Reactive Landscape
Clement Escoffier, Vert.x Core Developer, Red Hat
#vertx #reactive #rhoar #devnation @clementplop2
Reactive all the things ???
System
eXtensions
Programming
Manifesto
Spring
Streams
Asynchrony
Asynchronous
Scalability
ResilienceElasticity
Back-Pressure
Spreadsheets
Actor
Data Flows
Observable
Events
Message
Reactor
RX Java
#vertx #reactive #rhoar #devnation @clementplop3
https://en.oxforddictionaries.com/definition/reactive
#vertx #reactive #rhoar #devnation @clementplop4
FAKE !
#vertx #reactive #rhoar #devnation @clementplop5
FAKE !
#vertx #reactive #rhoar #devnation @clementplop6
FAKE !
#vertx #reactive #rhoar #devnation @clementplop7
The 2+1 parts of the reactive spectrum
Reactive
A software showing
responses to stimuli
Reactive
Systems
Reactive
Programming
Akka, Vert.x
Reactor, Reactive Spring,
Reactive eXtension (RX
Java), Vert.x
Manifesto, Actor
Messages, Resilience
Elasticity, Scalability,
Asynchronous
Streams, Flows,
Events,
Spreadsheets
Asynchronous
#vertx #reactive #rhoar #devnation @clementplop8
The 2+1 parts of the reactive spectrum
Reactive
A software showing
responses to stimuli
Reactive
Systems
Reactive
Programming
Akka, Vert.x
Reactor, Reactive Spring,
Reactive eXtension (RX
Java), Vert.x
Reactive
Streams
Akka Streams, RX
v2, Reactor, Vert.x
Data Flow
Back-Pressure
Non-Blocking,
Asynchronous
Manifesto, Actor
Messages, Resilience
Elasticity, Scalability,
Asynchronous
Streams, Flows,
Events,
Spreadsheets
Asynchronous
#vertx #reactive #rhoar #devnation @clementplop9
The 2+1 parts of the reactive spectrum
Reactive
A software showing
responses to stimuli
Reactive
Systems
Reactive
Programming
Akka, Vert.x
Reactor, Reactive Spring,
Reactive eXtension (RX
Java), Vert.x
Reactive
Streams
Akka Streams, RX
v2, Reactor, Vert.x
Data Flow
Back-Pressure
Non-Blocking,
Asynchronous
Manifesto, Actor
Messages, Resilience
Elasticity, Scalability,
Asynchronous
Streams, Flows,
Events,
Spreadsheets
Asynchronous
#vertx #reactive #rhoar #devnation @clementplop10
Mind the step…
Why my simple application is such a mess...
My
Application
Some
Software
Some
Services
#vertx #reactive #rhoar #devnation @clementplop11
Modern software is distributed...
Remote interactions everywhere
My
Application
Some
Software
Some
Services
#vertx #reactive #rhoar #devnation @clementplop12
Distributed systems fail
How to stay responsive in face of failures, under varying workload
My
Application
Some
Software
Some
Services
#vertx #reactive #rhoar #devnation @clementplop13
Distributed systems fail
How to stay responsive in face of failures, under varying workload
My
Application
Some
Software
Some
Services
Reactive Systems => Responsive Systems
#vertx #reactive #rhoar #devnation @clementplop15
Reactive Manifesto
http://www.reactivemanifesto.org/
#vertx #reactive #rhoar #devnation @clementplop16
Asynchronous message passing
Sends to an
address
Subscribes to
the address
Message
backbone
#vertx #reactive #rhoar #devnation @clementplop17
Asynchronous message passing
Not blocked while
waiting
for a reply
Message
backbone
#vertx #reactive #rhoar #devnation @clementplop18
Reactive Systems from the trenches
My
Application
Some
Software
Some
Services
Asynchronous development model
#vertx #reactive #rhoar #devnation @clementplop20
Asynchronous execution
Ideal
world
Task A Task B Task C
Real
world
Blocking I/O
Asynchronous
execution
Async programming model
Non-blocking IO
Task-based concurrency
#vertx #reactive #rhoar #devnation @clementplop21
Asynchronous, welcome to Hollywood
public int compute(int a, int b) {
return ...;
}
public void compute(int a, int b,
Handler<AsyncResult<Integer>> h){
// ...
handler.handle(i);
}
int res = compute(1, 2);
compute(1, 2, res -> {
// Called with the
// async result
});
Synchronous
Asynchronous
Don’t wait, we will call you...
#vertx #reactive #rhoar #devnation @clementplop22
Callbacks for notifications and async actions
Web server example
vertx.createHttpServer()
.requestHandler(req -> // Async reaction
req.response().end("Reactive greetings")
)
.listen(8080, ar -> { // Async operation
//...
});
#vertx #reactive #rhoar #devnation @clementplop23
Callbacks lead to...
Reality check
client.getConnection(conn -> {
if (conn.failed()) {/* failure handling */}
else {
SQLConnection connection = conn.result();
connection.query("SELECT * from PRODUCTS",
rs -> {
if (rs.failed()) {/* failure handling */}
else {
List<JsonArray> lines = rs.result().getResults();
for (JsonArray l : lines) { System.out.println(new Product(l)); }
connection.close(
done -> {
if (done.failed()) {/* failure handling */}
});
}
});
}
});
Reactive Programming...
Tame the asynchronous
#vertx #reactive #rhoar #devnation @clementplop25
Are you an “Excel” user?
My Expense Report
Lunch 15€
Coffee 25€
Drinks 45€
Total 85€
#vertx #reactive #rhoar #devnation @clementplop26
Are you an “Excel” user?
My Expense Report
Lunch 15€
Coffee 25€
Drinks 45€
Total =sum(B2:B4)
Observe
#vertx #reactive #rhoar #devnation @clementplop27
Streams (a.k.a Observables, Flowables, Publishers)
My Expense Report
Lunch 15€
Coffee 0€
Drinks 0€
Total 15€
My Expense Report
Lunch 15€
Coffee 25€
Drinks 0€
Total 40€
My Expense Report
Lunch 15€
Coffee 25€
Drinks 45€
Total 85€
#vertx #reactive #rhoar #devnation @clementplop28
Reactive Programming
Publisher and Subscriber
1 2 3 4
2 3 4 5
5 9
#vertx #reactive #rhoar #devnation @clementplop29
Reactive Extension - RX Java 2
Flowable<Integer> obs1 = Flowable.range(1, 10);
Flowable<Integer> obs2 = obs1.map(i -> i + 1);
Flowable<Integer> obs3 = obs2.window(2)
.flatMap(MathFlowable::sumInt);
obs3.subscribe(
i -> System.out.println("Computed " + i)
);
#vertx #reactive #rhoar #devnation @clementplop30
Reactive types & Asynchronous
Flowable / Observable - Stream of data, Async reaction
Bounded or unbounded stream of values
Data, Error, End of Stream
Singles / Maybe - Async operation
Stream of one value
Data, Error, Completion (maybe)
Completables - Async operation (no return)
Stream without a value
Completion, Error
dd d X?
d|X
X|e
d|eSingle
Maybe
#vertx #reactive #rhoar #devnation @clementplop31
Reactive types & Asynchronous
client.rxGetConnection() // Single(async op)
.flatMapPublisher(conn ->
conn
.rxQueryStream("SELECT * from PRODUCTS")
.flatMapPublisher(SQLRowStream::toFlowable)
.doAfterTerminate(conn::close)
) // Flowable of Rows
.map(Product::new) // Flowable of Products
.subscribe(System.out::println);
Q
R R
P P
C
flatmap
flatmap
map
Back pressure & Reactive streams
#vertx #reactive #rhoar #devnation @clementplop33
What if … the processing can’t keep up
I
J
map
Slow processing
J J J
Streams.getStream()
.observeOn(Schedulers.computation())
.map(i -> {
Thread.sleep(100); // I'm slow...
return i.getLong("id");
})
.blockingSubscribe(
x -> System.out.println("Processed:" + x),
Throwable::printStackTrace,
() -> System.out.println("Completed !")
);
I
Stream
#vertx #reactive #rhoar #devnation @clementplop34
Buffer to handle small bumps
I
J
Slow processing
J J J
Streams.getStream()
.onBackPressureBuffer(10000)
.observeOn(Schedulers.computation())
.map(i -> {
Thread.sleep(100); // I'm slow...
return i.getLong("id");
})
.blockingSubscribe(
x -> System.out.println("Processed:" + x),
Throwable::printStackTrace,
() -> System.out.println("Completed !")
);
I
Stream
buffer
#vertx #reactive #rhoar #devnation @clementplop35
Drop when data loss is acceptable
I
J
Slow processing
J J J
Streams.getStream()
.onBackPressureDrop()
.observeOn(Schedulers.computation())
.map(i -> {
Thread.sleep(100); // I'm slow...
return i.getLong("id");
})
.blockingSubscribe(
x -> System.out.println("Processed:" + x),
Throwable::printStackTrace,
() -> System.out.println("Completed !")
);
I
Stream
/dev/null
#vertx #reactive #rhoar #devnation @clementplop36
Control Flow and Reactive Streams
I
J J J J
Streams.getStreamWithBackPressure()
.observeOn(Schedulers.computation())
.map(i -> {
Thread.sleep(100); // I'm slow...
return i.getLong("id");
})
.blockingSubscribe(
x -> System.out.println("Processed:" + x),
Throwable::printStackTrace,
() -> System.out.println("Completed !")
);
I
request(x)
Publisher
Subscriber
Reactive Stream API - http://www.reactive-streams.org
Java 9 Flow - http://download.java.net/java/jdk9/docs/api/java/util/concurrent/Flow.html
#vertx #reactive #rhoar #devnation @clementplop37
Back to our database access
request(x)
client.rxGetConnection() // Single(async op)
.flatMapPublisher(conn ->
conn
.rxQueryStream("SELECT * from PRODUCTS")
.flatMapPublisher(SQLRowStream::toFlowable)
.doAfterTerminate(conn::close)
) // Flowable of Rows
.map(Product::new) // Flowable of Products
.subscribe(p -> {
// do something slow, but it’s fine
});
Eclipse Vert.x
Unleash your reactive superpowers
VERT.X
#vertx #reactive #rhoar #devnation @clementplop39
Eclipse Vert.x
Microservice, Web applications, IOT, API
Gateway, High-volume event processing,
Full-blown backend message bus.
Vert.x is a toolkit to build
distributed and reactive systems
Designed with
reactive
in mind
Asynchronous non-
blocking
development models
Simplified
concurrency
(event loop)
Polyglot
(java, scala, kotlin,
groovy, ruby,
ceylon…)(Clustered) event
bus
#vertx #reactive #rhoar #devnation @clementplop40
Vert.x - the all-in-one toolkit
Reactive
A software showing
responses to stimuli
Reactive
Systems
Reactive
Programming
Reactive
Streams
#vertx #reactive #rhoar #devnation @clementplop41
Taming the asynchronous
Distributed systems done right
My
Application
Some
Software
Some
Services
#vertx #reactive #rhoar #devnation @clementplop42
Reactive Web Application
https://github.com/cescoffier/reactive-landscape-devnation-17
private void add(RoutingContext rc) {
String name = rc.getBodyAsString();
database.insert(name) // Single (async)
.subscribe(
() -> rc.response().setStatusCode(201).end(),
rc::fail
);
}
private void list(RoutingContext rc) {
HttpServerResponse response = rc.response().setChunked(true);
database.retrieve() // Flowable<Product> (async)
.subscribe(
p -> response.write(Json.encode(p) +" nn"),
rc::fail,
response::end);
}
My
Application
#vertx #reactive #rhoar #devnation @clementplop43
Orchestrating remote interactions
Sequential composition
WebClient pricer = ...
HttpServerResponse response = rc.response().setChunked(true);
database.retrieve()
// For each row call...
.flatMapSingle(p ->
webClient
.get("/prices/" + p.getName())
.rxSend()
.map(HttpResponse::bodyAsJsonObject)
.map(json -> p.setPrice(json.getDouble("price")))
)
.subscribe(
p -> response.write(Json.encode(p) + " nn"),
rc::fail,
response::end);
My
Application
URL
#vertx #reactive #rhoar #devnation @clementplop44
Orchestrating remote interactions
Event Bus
WebClient pricer = ...
HttpServerResponse response = rc.response().setChunked(true);
database.retrieve()
// For each row call...
.flatMapSingle(p ->
vertx.eventBus()
.<JsonObject>rxSend("pricer", p.getName())
.map(Message::body)
.map(json -> p.setPrice(json.getDouble("price")))
)
.subscribe(
p -> response.write(Json.encode(p) + " nn"),
rc::fail,
response::end);
My
Application
Address
#vertx #reactive #rhoar #devnation @clementplop45
Push data using event bus bridges
Web Socket, SSE...
String name = rc.getBodyAsString().trim();
database.insert(name)
.flatMap(...)
.subscribe(
p -> {
String json = Json.encode(p);
rc.response().setStatusCode(201).end(json);
vertx.eventBus().publish("products", json);
},
rc::fail);
My
Application
SockJS
#vertx #reactive #rhoar #devnation @clementplop46
Executing several operations concurrently
database.insert(name)
.flatMap(p -> {
Single<Product> price = getPriceForProduct(p);
Single<Integer> audit = sendActionToAudit(p);
return Single.zip(price, audit, (pr, a) -> pr);
})
.subscribe(
p -> {
String json = Json.encode(p);
rc.response().setStatusCode(201).end(json);
vertx.eventBus().publish("products", json);
},
rc::fail);
My
Application
Reactive => Build better systems
#vertx #reactive #rhoar #devnation @clementplop48
Welcome to an asynchronous world
Reactive
A software showing
responses to stimuli
Reactive
Systems
Reactive
Programming
Reactive
Streams
Responsive
distributed
systems
Back-Pressure
protocol
Asynchronous
development
model
Building Reactive Microservices in Java
Use Eclipse Vert.x to build reactive microservices:
● Explore the elements of reactive microservices and learn how Vert.x
works.
● Build and consume a single microservice to understand how messaging
improves its reactiveness.
● Create an entire microservices system, using stability and resilience
patterns to manage failures.
Download the book from: http://bit.ly/vertx-reactive
clement@apache.org

The Reactive Landscape

  • 1.
    The Reactive Landscape ClementEscoffier, Vert.x Core Developer, Red Hat
  • 2.
    #vertx #reactive #rhoar#devnation @clementplop2 Reactive all the things ??? System eXtensions Programming Manifesto Spring Streams Asynchrony Asynchronous Scalability ResilienceElasticity Back-Pressure Spreadsheets Actor Data Flows Observable Events Message Reactor RX Java
  • 3.
    #vertx #reactive #rhoar#devnation @clementplop3 https://en.oxforddictionaries.com/definition/reactive
  • 4.
    #vertx #reactive #rhoar#devnation @clementplop4 FAKE !
  • 5.
    #vertx #reactive #rhoar#devnation @clementplop5 FAKE !
  • 6.
    #vertx #reactive #rhoar#devnation @clementplop6 FAKE !
  • 7.
    #vertx #reactive #rhoar#devnation @clementplop7 The 2+1 parts of the reactive spectrum Reactive A software showing responses to stimuli Reactive Systems Reactive Programming Akka, Vert.x Reactor, Reactive Spring, Reactive eXtension (RX Java), Vert.x Manifesto, Actor Messages, Resilience Elasticity, Scalability, Asynchronous Streams, Flows, Events, Spreadsheets Asynchronous
  • 8.
    #vertx #reactive #rhoar#devnation @clementplop8 The 2+1 parts of the reactive spectrum Reactive A software showing responses to stimuli Reactive Systems Reactive Programming Akka, Vert.x Reactor, Reactive Spring, Reactive eXtension (RX Java), Vert.x Reactive Streams Akka Streams, RX v2, Reactor, Vert.x Data Flow Back-Pressure Non-Blocking, Asynchronous Manifesto, Actor Messages, Resilience Elasticity, Scalability, Asynchronous Streams, Flows, Events, Spreadsheets Asynchronous
  • 9.
    #vertx #reactive #rhoar#devnation @clementplop9 The 2+1 parts of the reactive spectrum Reactive A software showing responses to stimuli Reactive Systems Reactive Programming Akka, Vert.x Reactor, Reactive Spring, Reactive eXtension (RX Java), Vert.x Reactive Streams Akka Streams, RX v2, Reactor, Vert.x Data Flow Back-Pressure Non-Blocking, Asynchronous Manifesto, Actor Messages, Resilience Elasticity, Scalability, Asynchronous Streams, Flows, Events, Spreadsheets Asynchronous
  • 10.
    #vertx #reactive #rhoar#devnation @clementplop10 Mind the step… Why my simple application is such a mess... My Application Some Software Some Services
  • 11.
    #vertx #reactive #rhoar#devnation @clementplop11 Modern software is distributed... Remote interactions everywhere My Application Some Software Some Services
  • 12.
    #vertx #reactive #rhoar#devnation @clementplop12 Distributed systems fail How to stay responsive in face of failures, under varying workload My Application Some Software Some Services
  • 13.
    #vertx #reactive #rhoar#devnation @clementplop13 Distributed systems fail How to stay responsive in face of failures, under varying workload My Application Some Software Some Services
  • 14.
    Reactive Systems =>Responsive Systems
  • 15.
    #vertx #reactive #rhoar#devnation @clementplop15 Reactive Manifesto http://www.reactivemanifesto.org/
  • 16.
    #vertx #reactive #rhoar#devnation @clementplop16 Asynchronous message passing Sends to an address Subscribes to the address Message backbone
  • 17.
    #vertx #reactive #rhoar#devnation @clementplop17 Asynchronous message passing Not blocked while waiting for a reply Message backbone
  • 18.
    #vertx #reactive #rhoar#devnation @clementplop18 Reactive Systems from the trenches My Application Some Software Some Services
  • 19.
  • 20.
    #vertx #reactive #rhoar#devnation @clementplop20 Asynchronous execution Ideal world Task A Task B Task C Real world Blocking I/O Asynchronous execution Async programming model Non-blocking IO Task-based concurrency
  • 21.
    #vertx #reactive #rhoar#devnation @clementplop21 Asynchronous, welcome to Hollywood public int compute(int a, int b) { return ...; } public void compute(int a, int b, Handler<AsyncResult<Integer>> h){ // ... handler.handle(i); } int res = compute(1, 2); compute(1, 2, res -> { // Called with the // async result }); Synchronous Asynchronous Don’t wait, we will call you...
  • 22.
    #vertx #reactive #rhoar#devnation @clementplop22 Callbacks for notifications and async actions Web server example vertx.createHttpServer() .requestHandler(req -> // Async reaction req.response().end("Reactive greetings") ) .listen(8080, ar -> { // Async operation //... });
  • 23.
    #vertx #reactive #rhoar#devnation @clementplop23 Callbacks lead to... Reality check client.getConnection(conn -> { if (conn.failed()) {/* failure handling */} else { SQLConnection connection = conn.result(); connection.query("SELECT * from PRODUCTS", rs -> { if (rs.failed()) {/* failure handling */} else { List<JsonArray> lines = rs.result().getResults(); for (JsonArray l : lines) { System.out.println(new Product(l)); } connection.close( done -> { if (done.failed()) {/* failure handling */} }); } }); } });
  • 24.
  • 25.
    #vertx #reactive #rhoar#devnation @clementplop25 Are you an “Excel” user? My Expense Report Lunch 15€ Coffee 25€ Drinks 45€ Total 85€
  • 26.
    #vertx #reactive #rhoar#devnation @clementplop26 Are you an “Excel” user? My Expense Report Lunch 15€ Coffee 25€ Drinks 45€ Total =sum(B2:B4) Observe
  • 27.
    #vertx #reactive #rhoar#devnation @clementplop27 Streams (a.k.a Observables, Flowables, Publishers) My Expense Report Lunch 15€ Coffee 0€ Drinks 0€ Total 15€ My Expense Report Lunch 15€ Coffee 25€ Drinks 0€ Total 40€ My Expense Report Lunch 15€ Coffee 25€ Drinks 45€ Total 85€
  • 28.
    #vertx #reactive #rhoar#devnation @clementplop28 Reactive Programming Publisher and Subscriber 1 2 3 4 2 3 4 5 5 9
  • 29.
    #vertx #reactive #rhoar#devnation @clementplop29 Reactive Extension - RX Java 2 Flowable<Integer> obs1 = Flowable.range(1, 10); Flowable<Integer> obs2 = obs1.map(i -> i + 1); Flowable<Integer> obs3 = obs2.window(2) .flatMap(MathFlowable::sumInt); obs3.subscribe( i -> System.out.println("Computed " + i) );
  • 30.
    #vertx #reactive #rhoar#devnation @clementplop30 Reactive types & Asynchronous Flowable / Observable - Stream of data, Async reaction Bounded or unbounded stream of values Data, Error, End of Stream Singles / Maybe - Async operation Stream of one value Data, Error, Completion (maybe) Completables - Async operation (no return) Stream without a value Completion, Error dd d X? d|X X|e d|eSingle Maybe
  • 31.
    #vertx #reactive #rhoar#devnation @clementplop31 Reactive types & Asynchronous client.rxGetConnection() // Single(async op) .flatMapPublisher(conn -> conn .rxQueryStream("SELECT * from PRODUCTS") .flatMapPublisher(SQLRowStream::toFlowable) .doAfterTerminate(conn::close) ) // Flowable of Rows .map(Product::new) // Flowable of Products .subscribe(System.out::println); Q R R P P C flatmap flatmap map
  • 32.
    Back pressure &Reactive streams
  • 33.
    #vertx #reactive #rhoar#devnation @clementplop33 What if … the processing can’t keep up I J map Slow processing J J J Streams.getStream() .observeOn(Schedulers.computation()) .map(i -> { Thread.sleep(100); // I'm slow... return i.getLong("id"); }) .blockingSubscribe( x -> System.out.println("Processed:" + x), Throwable::printStackTrace, () -> System.out.println("Completed !") ); I Stream
  • 34.
    #vertx #reactive #rhoar#devnation @clementplop34 Buffer to handle small bumps I J Slow processing J J J Streams.getStream() .onBackPressureBuffer(10000) .observeOn(Schedulers.computation()) .map(i -> { Thread.sleep(100); // I'm slow... return i.getLong("id"); }) .blockingSubscribe( x -> System.out.println("Processed:" + x), Throwable::printStackTrace, () -> System.out.println("Completed !") ); I Stream buffer
  • 35.
    #vertx #reactive #rhoar#devnation @clementplop35 Drop when data loss is acceptable I J Slow processing J J J Streams.getStream() .onBackPressureDrop() .observeOn(Schedulers.computation()) .map(i -> { Thread.sleep(100); // I'm slow... return i.getLong("id"); }) .blockingSubscribe( x -> System.out.println("Processed:" + x), Throwable::printStackTrace, () -> System.out.println("Completed !") ); I Stream /dev/null
  • 36.
    #vertx #reactive #rhoar#devnation @clementplop36 Control Flow and Reactive Streams I J J J J Streams.getStreamWithBackPressure() .observeOn(Schedulers.computation()) .map(i -> { Thread.sleep(100); // I'm slow... return i.getLong("id"); }) .blockingSubscribe( x -> System.out.println("Processed:" + x), Throwable::printStackTrace, () -> System.out.println("Completed !") ); I request(x) Publisher Subscriber Reactive Stream API - http://www.reactive-streams.org Java 9 Flow - http://download.java.net/java/jdk9/docs/api/java/util/concurrent/Flow.html
  • 37.
    #vertx #reactive #rhoar#devnation @clementplop37 Back to our database access request(x) client.rxGetConnection() // Single(async op) .flatMapPublisher(conn -> conn .rxQueryStream("SELECT * from PRODUCTS") .flatMapPublisher(SQLRowStream::toFlowable) .doAfterTerminate(conn::close) ) // Flowable of Rows .map(Product::new) // Flowable of Products .subscribe(p -> { // do something slow, but it’s fine });
  • 38.
    Eclipse Vert.x Unleash yourreactive superpowers VERT.X
  • 39.
    #vertx #reactive #rhoar#devnation @clementplop39 Eclipse Vert.x Microservice, Web applications, IOT, API Gateway, High-volume event processing, Full-blown backend message bus. Vert.x is a toolkit to build distributed and reactive systems Designed with reactive in mind Asynchronous non- blocking development models Simplified concurrency (event loop) Polyglot (java, scala, kotlin, groovy, ruby, ceylon…)(Clustered) event bus
  • 40.
    #vertx #reactive #rhoar#devnation @clementplop40 Vert.x - the all-in-one toolkit Reactive A software showing responses to stimuli Reactive Systems Reactive Programming Reactive Streams
  • 41.
    #vertx #reactive #rhoar#devnation @clementplop41 Taming the asynchronous Distributed systems done right My Application Some Software Some Services
  • 42.
    #vertx #reactive #rhoar#devnation @clementplop42 Reactive Web Application https://github.com/cescoffier/reactive-landscape-devnation-17 private void add(RoutingContext rc) { String name = rc.getBodyAsString(); database.insert(name) // Single (async) .subscribe( () -> rc.response().setStatusCode(201).end(), rc::fail ); } private void list(RoutingContext rc) { HttpServerResponse response = rc.response().setChunked(true); database.retrieve() // Flowable<Product> (async) .subscribe( p -> response.write(Json.encode(p) +" nn"), rc::fail, response::end); } My Application
  • 43.
    #vertx #reactive #rhoar#devnation @clementplop43 Orchestrating remote interactions Sequential composition WebClient pricer = ... HttpServerResponse response = rc.response().setChunked(true); database.retrieve() // For each row call... .flatMapSingle(p -> webClient .get("/prices/" + p.getName()) .rxSend() .map(HttpResponse::bodyAsJsonObject) .map(json -> p.setPrice(json.getDouble("price"))) ) .subscribe( p -> response.write(Json.encode(p) + " nn"), rc::fail, response::end); My Application URL
  • 44.
    #vertx #reactive #rhoar#devnation @clementplop44 Orchestrating remote interactions Event Bus WebClient pricer = ... HttpServerResponse response = rc.response().setChunked(true); database.retrieve() // For each row call... .flatMapSingle(p -> vertx.eventBus() .<JsonObject>rxSend("pricer", p.getName()) .map(Message::body) .map(json -> p.setPrice(json.getDouble("price"))) ) .subscribe( p -> response.write(Json.encode(p) + " nn"), rc::fail, response::end); My Application Address
  • 45.
    #vertx #reactive #rhoar#devnation @clementplop45 Push data using event bus bridges Web Socket, SSE... String name = rc.getBodyAsString().trim(); database.insert(name) .flatMap(...) .subscribe( p -> { String json = Json.encode(p); rc.response().setStatusCode(201).end(json); vertx.eventBus().publish("products", json); }, rc::fail); My Application SockJS
  • 46.
    #vertx #reactive #rhoar#devnation @clementplop46 Executing several operations concurrently database.insert(name) .flatMap(p -> { Single<Product> price = getPriceForProduct(p); Single<Integer> audit = sendActionToAudit(p); return Single.zip(price, audit, (pr, a) -> pr); }) .subscribe( p -> { String json = Json.encode(p); rc.response().setStatusCode(201).end(json); vertx.eventBus().publish("products", json); }, rc::fail); My Application
  • 47.
    Reactive => Buildbetter systems
  • 48.
    #vertx #reactive #rhoar#devnation @clementplop48 Welcome to an asynchronous world Reactive A software showing responses to stimuli Reactive Systems Reactive Programming Reactive Streams Responsive distributed systems Back-Pressure protocol Asynchronous development model
  • 49.
    Building Reactive Microservicesin Java Use Eclipse Vert.x to build reactive microservices: ● Explore the elements of reactive microservices and learn how Vert.x works. ● Build and consume a single microservice to understand how messaging improves its reactiveness. ● Create an entire microservices system, using stability and resilience patterns to manage failures. Download the book from: http://bit.ly/vertx-reactive clement@apache.org