Modern app programming with RxJava and Eclipse Vert.x #QConSP - - PowerPoint PPT Presentation

modern app programming
SMART_READER_LITE
LIVE PREVIEW

Modern app programming with RxJava and Eclipse Vert.x #QConSP - - PowerPoint PPT Presentation

Modern app programming with RxJava and Eclipse Vert.x #QConSP @vertx_project Who am I? Vert.x core team member since 2016 Working at since 2012 Contributing specifically to monitoring and clustering @tsegismont


slide-1
SLIDE 1

#QConSP @vertx_project

Modern app programming

with RxJava and Eclipse Vert.x

slide-2
SLIDE 2

#QConSP @vertx_project

Who am I?

Vert.x core team member since 2016 Working at since 2012 Contributing specifically to monitoring and clustering

@tsegismont

slide-3
SLIDE 3

Eclipse Vert.x

A toolkit to build reactive applications on the JVM

Started in 2O12 Eclipse / Apache licensing 7K on Built on top of Netty

@vertx_project https://vertx.io

slide-4
SLIDE 4
slide-5
SLIDE 5

#QConSP @vertx_project

Pay the right price

  • Tiny footprint
  • Do one thing and do it well
  • Modular set of extensions
slide-6
SLIDE 6

Why going reactive?

slide-7
SLIDE 7

#QConSP @vertx_project

A hotel room booking startup story

slide-8
SLIDE 8

2

slide-9
SLIDE 9
slide-10
SLIDE 10

2002

slide-11
SLIDE 11
slide-12
SLIDE 12

2003

slide-13
SLIDE 13
slide-14
SLIDE 14

2006

slide-15
SLIDE 15
slide-16
SLIDE 16

2009

slide-17
SLIDE 17
slide-18
SLIDE 18

2011

NoSQL

slide-19
SLIDE 19
slide-20
SLIDE 20
slide-21
SLIDE 21

1/ Many channels 2/ Lots of I/O

slide-22
SLIDE 22

while (isRunning) { String line = bufferedReader.readLine(); switch (line.substring(0, 4)) { case "ECHO": bufferedWriter.write(line); break; // ... // other cases (...) // ... } }

slide-23
SLIDE 23

x 1000 =

slide-24
SLIDE 24

“But I don’t do Internet scale. I do enterprise!”

slide-25
SLIDE 25

2018 server: 28 cores, 512 GB memory

slide-26
SLIDE 26

Microservices

slide-27
SLIDE 27

Your container share: 2 vCPU, 512 MB memory

slide-28
SLIDE 28

1/ Keep the number of threads to a minimum 2/ Keep them busy

slide-29
SLIDE 29

Thread Events Event-loop

slide-30
SLIDE 30

Event loops in Vert.x project

slide-31
SLIDE 31

2 event-loops per core

slide-32
SLIDE 32

class HttpServerVerticle extends AbstractVerticle { public static void main(String[] args) { Vertx vertx = Vertx.vertx(); vertx.deployVerticle(HttpServerVerticle.class.getName()); } @Override public void start() throws Exception { vertx.createHttpServer() .requestHandler(req -> { req.response().end("Hello world!"); }).listen(8080); } }

Verticles

slide-33
SLIDE 33

DeploymentOptions options = new DeploymentOptions() .setInstances(3); vertx.deployVerticle(httpVerticle, options);

Load-balancing

slide-34
SLIDE 34

Communication between verticles?

slide-35
SLIDE 35

EventBus

Messaging passing, not shared data

Multi-paradigm Standalone or clustered Browser and mobile bridges

slide-36
SLIDE 36

EventBus eventBus = vertx.eventBus(); eventBus.<String>consumer("greeting").handler(msg -> { String name = msg.body(); msg.reply(String.format("Hello %s!", name)); }); EventBus eventBus = vertx.eventBus(); eventBus.<String>send("greeting", "QConSP", ar -> { if (ar.succeeded()) { System.out.println(ar.result().body()); } else { ar.cause().printStackTrace(); } });

1 2

slide-37
SLIDE 37

Verticle Bridge

slide-38
SLIDE 38

#QConSP @vertx_project

Distributed EventBus demo

slide-39
SLIDE 39

#QConSP @vertx_project

Reactive Programming with RxJava

slide-40
SLIDE 40

Everything is a stream: data or events

slide-41
SLIDE 41

Java 8 streams on steroids

slide-42
SLIDE 42

Lazy and push based

Observable Observer Subscription

slide-43
SLIDE 43

Backpressure

Observable Observer Subscription Request

slide-44
SLIDE 44

#QConSP @vertx_project

Different flavors

None One Many Completable Single<T> Observable<T> * plus Maybe<T> and Flowable<T> in RxJava 2

slide-45
SLIDE 45

Composing data flows

slide-46
SLIDE 46
slide-47
SLIDE 47

Great to deal with failures and latencies in distributed systems

slide-48
SLIDE 48
slide-49
SLIDE 49
slide-50
SLIDE 50
slide-51
SLIDE 51

Observable<String> loadPostsFromRDBMS(LocalDate date); Observable<Comment> loadFromDocumentStore(String postId); loadPostsFromRDBMS(date) .concatMap(postId -> loadFromDocumentStore(postId)) .retry(4) .timeout(500, TimeUnit.MILLISECONDS) .subscribe();

Composition

slide-52
SLIDE 52

No need to create your own Observables with Vert.x

slide-53
SLIDE 53

#QConSP @vertx_project

Rxified APIs

import io.vertx.ext.web.Router; import io.vertx.reactivex.ext.web.Router;

slide-54
SLIDE 54

#QConSP @vertx_project

Rxified APIs

Single<HttpServer> rxListen(int port); void listen(int port, Handler<AsyncResult<HttpServer>> handler);

slide-55
SLIDE 55

#QConSP @vertx_project

Rxified ReadStream<T>

class AsyncFile implements ReadStream<Buffer> { // ... // RxJava1 Observable<Buffer> toObservable(); // RxJava2 Flowable<Buffer> toFlowable(); // ... }

slide-56
SLIDE 56

#QConSP @vertx_project

Music Store demo

https://github.com/tsegismont/vertx-musicstore

slide-57
SLIDE 57

Mongo Postgres Cover Art Archive

slide-58
SLIDE 58

#QConSP @vertx_project

Learning

slide-59
SLIDE 59

#QConSP @vertx_project

Learning

slide-60
SLIDE 60

Thank you!

More questions? Come to the Red Hat booth!