with rxjava and eclipse vert - qconsp · a toolkit to build reactive applications on the jvm...

60
#QConSP @vertx_project Modern app programming with RxJava and Eclipse Vert.x

Upload: others

Post on 22-May-2020

18 views

Category:

Documents


0 download

TRANSCRIPT

#QConSP @vertx_project

Modern app programmingwith 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

Eclipse Vert.xA 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

#QConSP @vertx_project

Pay the right price

● Tiny footprint● Do one thing and do it well● Modular set of extensions

Why going reactive?

#QConSP @vertx_project

A hotel room booking startup story

2000

2002

2003

2006

2009

2011

NoSQL

1/ Many channels2/ Lots of I/O

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

x 1000 =

“But I don’t do Internet scale.

I do enterprise!”

2018 server: 28 cores, 512 GB memory

Microservices

Your container share: 2 vCPU, 512 MB memory

1/ Keep the number of threads to a minimum

2/ Keep them busy

Thread

Events

Event-loop

Event loopsin Vert.x project

2 event-loops per core

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

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

Load-balancing

Communication between verticles?

EventBusMessaging passing,

not shared data

Multi-paradigm

Standalone or clustered

Browser and mobile bridges

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

Verticle

Bridge

#QConSP @vertx_project

Distributed EventBus demo

#QConSP @vertx_project

Reactive Programming with RxJava

Everything is a stream: data or events

Java 8 streams on steroids

Lazy and push based

Observable Observer

Subscription

Backpressure

Observable Observer

Subscription

Request

#QConSP @vertx_project

Different flavors

None One Many

Completable Single<T> Observable<T>

* plus Maybe<T> and Flowable<T> in RxJava 2

Composing data flows

Great to deal withfailures and latenciesin distributed systems

Observable<String> loadPostsFromRDBMS(LocalDate date);

Observable<Comment> loadFromDocumentStore(String postId);

loadPostsFromRDBMS(date) .concatMap(postId -> loadFromDocumentStore(postId)) .retry(4) .timeout(500, TimeUnit.MILLISECONDS) .subscribe();

Composition

No need to createyour own Observableswith Vert.x

#QConSP @vertx_project

Rxified APIs

import io.vertx.ext.web.Router;

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

#QConSP @vertx_project

Rxified APIs

Single<HttpServer> rxListen(int port);

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

#QConSP @vertx_project

Rxified ReadStream<T>

class AsyncFile implements ReadStream<Buffer> {

// ...

// RxJava1 Observable<Buffer> toObservable();

// RxJava2 Flowable<Buffer> toFlowable();

// ...

}

#QConSP @vertx_project

Music Store demo

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

Mongo

Postgres

Cover Art Archive

#QConSP @vertx_project

Learning

#QConSP @vertx_project

Learning

Thank you!

More questions?Come to the Red Hat booth!