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 #QConSP @vertx_project
Started in 2O12 Eclipse Vert.x Eclipse / Apache licensing A toolkit to build reactive 7K on applications on the JVM Built on top of Netty @vertx_project https://vertx.io
Pay the right price ● Tiny footprint ● Do one thing and do it well ● Modular set of extensions #QConSP @vertx_project
Why going reactive?
A hotel room booking startup story #QConSP @vertx_project
2 0 0 0
2002
2003
2006
2009
NoSQL 2011
1/ Many channels 2/ 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 loops in 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 -> { Verticles req.response().end( "Hello world!" ); }).listen(8080); } }
DeploymentOptions options = new DeploymentOptions() .setInstances(3); vertx.deployVerticle(httpVerticle, options); Load-balancing
Communication between verticles?
Multi-paradigm EventBus Standalone or clustered Messaging passing, Browser and mobile bridges not shared data
EventBus eventBus = vertx .eventBus(); eventBus.<String>consumer( "greeting" ).handler(msg -> { String name = msg.body(); msg.reply(String. format ( "Hello %s!" , name)); }); 2 1 EventBus eventBus = vertx .eventBus(); eventBus.<String>send( "greeting" , "QConSP" , ar -> { if (ar.succeeded()) { System. out .println(ar.result().body()); } else { ar.cause().printStackTrace(); } });
Bridge Verticle
Distributed EventBus demo #QConSP @vertx_project
Reactive Programming with RxJava #QConSP @vertx_project
Everything is a stream: data or events
Java 8 streams on steroids
Observable Observer Subscription Lazy and push based
Request Observable Observer Subscription Backpressure
Different flavors None One Many Completable Single<T> Observable<T> * plus Maybe<T> and Flowable<T> in RxJava 2 #QConSP @vertx_project
Composing data flows
Great to deal with failures and latencies in distributed systems
Observable<String> loadPostsFromRDBMS(LocalDate date); Observable<Comment> loadFromDocumentStore(String postId); Composition loadPostsFromRDBMS(date) .concatMap(postId -> loadFromDocumentStore(postId)) .retry(4) .timeout(500, TimeUnit. MILLISECONDS ) .subscribe();
No need to create your own Observables with Vert.x
Rxified APIs import io.vertx.ext.web.Router; import io.vertx.reactivex.ext.web.Router; #QConSP @vertx_project
Rxified APIs void listen( int port, Handler<AsyncResult<HttpServer>> handler); Single<HttpServer> rxListen( int port); #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 #QConSP @vertx_project
Mongo Postgres Cover Art Archive
Learning #QConSP @vertx_project
Learning #QConSP @vertx_project
Thank you! More questions? Come to the Red Hat booth!
Recommend
More recommend