got a question during this session? post it on sli.do ( # ... · future future = ex.submit(() ->...

183
Got a question during this session? Post it on sli.do ( #K100 )

Upload: others

Post on 27-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Got a question during this session? Post it on sli.do ( #K100 )

Page 2: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava, RxJava 2, ReactorState of the art of Reactive Streams on the JVM

Page 3: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

David Wursteisen

Page 4: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 5: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Writing asynchronous code: it sucks

Page 6: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

FutureExecutorService ex = Executors.newCachedThreadPool();Future<String> future = ex.submit(() -> longProcessing());String result = future.get();

Blocking call

Page 7: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

FutureFuture<?> future1 = /* ... */Future<?> future2 = /* ... */Future<?> future3 = /* ... */Future<?> future4 = /* ... */Future<?> future5 = /* ... */

Optimal Orchestration ?

Page 8: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

CallbackRemoteService service = buildRemoteService();

service.getUser(id -> { service.getData(id, data -> { service.getSomething(data, whut -> { service.neverEndingCallBack(whut, () -> { }); }); });});

Callback Hell

Page 9: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Relationship Status:it’s complicated

Page 10: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

The problem of synchronous code

Page 11: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

SynchronousWaiting for the

response

Caller

Called

Page 12: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Impact on response time

Slow server

Page 13: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

It never works!

It piss me off!

Page 14: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

AsynchronousCaller

Called

Continuous work

Page 15: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Asynchronous allow to take advantage of

the waiting time

Page 16: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Minimal impact on the response

time

Page 17: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Write asynchronous code

easily?

Page 18: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Emergence

of different approaches

Page 19: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

2Reactive Streams

Page 20: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Reactive StreamsReactive Streams API

RxJava 2 Reactor

Interface

Implementation

Page 21: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Reactive Streams API is a bridge

between implementations

Page 22: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Reactive Streams contract

onSubscribe onNext onErroronComplete

Page 23: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava is not compatible

with Reactive Streams(You’ll have to use an adapter: RxJavaReactiveStreams)

https://github.com/ReactiveX/RxJavaReactiveStreams

Page 24: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

onNext * ( onError | onComplete ) onNext * ( onError | onCompleted )RxJava

Reactive Streams

Page 25: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

onNext * ( onError | onComplete ) onNext * ( onError | onCompleted )RxJava

Reactive Streams

Different name

Page 26: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Common approach

Page 27: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

A P I t o h a n d l e e v e n t s synchronously or asynchronously through a flow of events

Page 28: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 29: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Map ( ⃝ → ⃞)

Page 30: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 31: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

remoteApi.people(1).flatMap(luke -> { Observable<String> vehicles = Observable.from(luke.getVehiclesIds()) .flatMap(remoteApi::vehicle) .map(vehicle -> luke.getName() + " can drive " + vehicle.getName()); Observable<String> starships = Observable.from(luke.getStarshipsIds()) .flatMap(remoteApi::starship) .map(starship -> luke.getName() + " can fly with " + starship.getName()); return Observable.merge(vehicles, starships);}).subscribe(System.out::println);

Page 32: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

remoteApi.people(1).flatMap(luke -> { Observable<String> vehicles = Observable.from(luke.getVehiclesIds()) .flatMap(remoteApi::vehicle) .map(vehicle -> luke.getName() + " can drive " + vehicle.getName()); Observable<String> starships = Observable.from(luke.getStarshipsIds()) .flatMap(remoteApi::starship) .map(starship -> luke.getName() + " can fly with " + starship.getName()); return Observable.merge(vehicles, starships);}).subscribe(System.out::println);

Push of the result

Page 33: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

remoteApi.people(1).flatMap(luke -> { Observable<String> vehicles = Observable.from(luke.getVehiclesIds()) .flatMap(remoteApi::vehicle) .map(vehicle -> luke.getName() + " can drive " + vehicle.getName()); Observable<String> starships = Observable.from(luke.getStarshipsIds()) .flatMap(remoteApi::starship) .map(starship -> luke.getName() + " can fly with " + starship.getName()); return Observable.merge(vehicles, starships);}).subscribe(System.out::println);

Get Luke’s vehicles

Get Luke’s starships

Page 34: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

remoteApi.people(1).flatMap(luke -> { Observable<String> vehicles = Observable.from(luke.getVehiclesIds()) .flatMap(remoteApi::vehicle) .map(vehicle -> luke.getName() + " can drive " + vehicle.getName()); Observable<String> starships = Observable.from(luke.getStarshipsIds()) .flatMap(remoteApi::starship) .map(starship -> luke.getName() + " can fly with " + starship.getName()); return Observable.merge(vehicles, starships);}).subscribe(System.out::println);

Merge of two flows

Page 35: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

remoteApi.people(1).flatMap(luke -> { Observable<String> vehicles = Observable.from(luke.getVehiclesIds()) .flatMap(remoteApi::vehicle) .map(vehicle -> luke.getName() + " can drive " + vehicle.getName()); Observable<String> starships = Observable.from(luke.getStarshipsIds()) .flatMap(remoteApi::starship) .map(starship -> luke.getName() + " can fly with " + starship.getName()); return Observable.merge(vehicles, starships);}).subscribe(System.out::println);

Really execute the code

Page 36: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

remoteApi.people(1).flatMap(luke -> { Observable<String> vehicles = Observable.from(luke.getVehiclesIds()) .flatMap(remoteApi::vehicle) .map(vehicle -> luke.getName() + " can drive " + vehicle.getName()); Observable<String> starships = Observable.from(luke.getStarshipsIds()) .flatMap(remoteApi::starship) .map(starship -> luke.getName() + " can fly with " + starship.getName()); return Observable.merge(vehicles, starships);}).subscribe(System.out::println);

Flow of events

Page 37: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

remoteApi.people(1).flatMap(luke -> { Observable<String> vehicles = Observable.from(luke.getVehiclesIds()) .flatMap(remoteApi::vehicle) .map(vehicle -> luke.getName() + " can drive " + vehicle.getName()); Observable<String> starships = Observable.from(luke.getStarshipsIds()) .flatMap(remoteApi::starship) .map(starship -> luke.getName() + " can fly with " + starship.getName()); return Observable.merge(vehicles, starships);}).subscribe(System.out::println);

Flow of events

Flow of events

Page 38: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 39: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Button btn = new Button();btn.setText("Click Me");JavaFx.fromClick(btn) .observeOn(Schedulers.io()) .switchMap(evt -> remoteApi.getData()) .observeOn(javaFx()) .doOnNext(value -> btn.setText("Data: " + value)) .subscribe();

Page 40: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Button btn = new Button();btn.setText("Click Me");JavaFx.fromClick(btn) // Observable<Event> .observeOn(Schedulers.io()) .switchMap(evt -> remoteApi.getData()) .observeOn(javaFx()) .doOnNext(value -> btn.setText("Data: " + value)) .subscribe();

Listen for clicks

Page 41: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Button btn = new Button();btn.setText("Click Me");JavaFx.fromClick(btn) // Observable<Event> .observeOn(Schedulers.io()) .switchMap(evt -> remoteApi.getData()) .observeOn(javaFx()) .doOnNext(value -> btn.setText("Data: " + value)) .subscribe();

Execution context switch

Page 42: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Button btn = new Button();btn.setText("Click Me");JavaFx.fromClick(btn) // Observable<Event> .observeOn(Schedulers.io()) .switchMap(evt -> remoteApi.getData()) // Observable<Data> .observeOn(javaFx()) .doOnNext(value -> btn.setText("Data: " + value)) .subscribe();

Asynchronous call to a web service

Page 43: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Button btn = new Button();btn.setText("Click Me");JavaFx.fromClick(btn) // Observable<Event> .observeOn(Schedulers.io()) .switchMap(evt -> remoteApi.getData()) // Observable<Data> .observeOn(javaFx()) .doOnNext(value -> btn.setText("Data: " + value)) .subscribe();

Execution context switch

Page 44: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Button btn = new Button();btn.setText("Click Me");JavaFx.fromClick(btn) // Observable<Event> .observeOn(Schedulers.io()) .switchMap(evt -> remoteApi.getData()) // Observable<Data> .observeOn(javaFx()) .doOnNext(value -> btn.setText("Data: " + value)) .subscribe();Update on the UI

Page 45: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Button btn = new Button();btn.setText("Click Me");JavaFx.fromClick(btn) // Observable<Event> .observeOn(Schedulers.io()) .switchMap(evt -> remoteApi.getData()) // Observable<Data> .observeOn(javaFx()) .doOnNext(value -> btn.setText("Data: " + value)) .subscribe();

Flow of events

Page 46: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Thanks to RxJava and Reactor…

Page 47: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Writing asynchronous code: it sucks

Page 48: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Once upon a time…

Page 49: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Creation of Reactive Extensions

Creation of RxJava

Active Participation Resumption of

RxJava & RxJava 2

Creation of Reactor

Work on Reactor

Page 50: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava is a proved

technologie

Page 51: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Reactorbenefits from the experience of

RxJava(and vice versa)

Page 52: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Object types

Page 53: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Observable

Page 54: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 55: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 56: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 57: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 58: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 59: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Single

Page 60: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 61: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Completable

Page 62: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 63: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJavaContrat Backpressure

Observable [N] Yes

Single [1] No

Completable [0] No

Added afterward

Web service call

Background process

Page 64: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Listen a websocket, for each received command, compose a response by calling 3 different webservices, then execute 2 jobs sequentially ?

Page 65: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Listen a websocket, for each received command, compose a response by calling 3 different webservices, then execute 2 jobs sequentially ?

Page 66: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Listen a websocket, for each received command, compose a response by calling 3 different webservices, then execute 2 jobs sequentially ?

Page 67: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Listen a websocket, for each received command, compose a response by calling 3 different webservices, then execute 2 jobs sequentially ?

Page 68: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

websocket("/topics/cmd") .observeOn(Schedulers.io()) .switchMap(cmd -> Single.zip( api.getActions(), api.getScore(), api.getUserData(), this::composeResult).toObservable()) .observeOn(Schedulers.computation()) .concatMap(result -> updateDb(result).andThen(getLastResults())) .subscribe(last -> System.out.println("last results -> " + last));

Page 69: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

websocket("/topics/cmd") .observeOn(Schedulers.io()) .switchMap(cmd -> Single.zip( api.getActions(), api.getScore(), api.getUserData(), this::composeResult).toObservable()) .observeOn(Schedulers.computation()) .concatMap(result -> updateDb(result).andThen(getLastResults())) .subscribe(last -> System.out.println("last results -> " + last));

Listen a websocket

Observable

Page 70: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

websocket("/topics/cmd") .observeOn(Schedulers.io()) .switchMap(cmd -> Single.zip( api.getActions(), api.getScore(), api.getUserData(), this::composeResult).toObservable()) .observeOn(Schedulers.computation()) .concatMap(result -> updateDb(result).andThen(getLastResults())) .subscribe(last -> System.out.println("last results -> " + last));

Webservices composition

Single

Page 71: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

websocket("/topics/cmd") .observeOn(Schedulers.io()) .switchMap(cmd -> Single.zip( api.getActions(), api.getScore(), api.getUserData(), this::composeResult).toObservable()) .observeOn(Schedulers.computation()) .concatMap(result -> updateDb(result).andThen(getLastResults())) .subscribe(last -> System.out.println("last results -> " + last));

Completable

2 jobs executions

Page 72: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

websocket("/topics/cmd") .observeOn(Schedulers.io()) .switchMap(cmd -> Single.zip( api.getActions(), api.getScore(), api.getUserData(), this::composeResult).toObservable()) .observeOn(Schedulers.computation()) .concatMap(result -> updateDb(result).andThen(getLastResults())) .subscribe(last -> System.out.println("last results -> " + last));

Listen a websocket

Observable Webservices composition

Single

Completable

2 jobs executions

Page 73: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava 2Contrat Backpressure

Observable [N] No

Single [1] No

Completable [0] No

Maybe [0|1] No New!Close to Java 8

Optional

Page 74: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Maybe

Page 75: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 76: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Backpressure using RxJava 2

Page 77: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Consumer

Page 78: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Consumer

To many things !

Page 79: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

backpressure

Page 80: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

MissingBackpressureException ?

Page 81: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

acceptedIntent .filter(intent -> !intent.getBooleanExtra("UpdatePhoneMode", false)) .concatMap(intent -> approximatedEngine.detectCurrentPlace()) .doOnNext(score -> Log.info(TAG, "Scan completed with result " + score)) .concatMap(this::detectSleepMode) .concatMap((score) -> isNewPlace(score.getScore().getPlace()).map(p -> score)) .doOnNext((p) -> Log.info(TAG, "Current place found is : " + p)) .subscribe()

Page 82: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

acceptedIntent .filter(intent -> !intent.getBooleanExtra("UpdatePhoneMode", false)) .onBackpressureDrop() .concatMap(intent -> approximatedEngine.detectCurrentPlace()) .doOnNext(score -> Log.info(TAG, "Scan completed with result " + score)) .onBackpressureDrop() .concatMap(this::detectSleepMode) .onBackpressureDrop() .concatMap((score) -> isNewPlace(score.getScore().getPlace()).map(p -> score)) .doOnNext((p) -> Log.info(TAG, "Current place found is : " + p)) .subscribe()

Added while I panicked

Page 83: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava 2Contrat Backpressure

Observable [N] No

Single [1] No

Completable [0] No

Maybe [0|1] No New!Close to Java 8

Optional

Page 84: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava 2Contrat Backpressure

Observable [N] No

Single [1] No

Completable [0] No

Maybe [0|1] No

Flowable [N] Yes

New!

Observable with back pressure

Close to Java 8 Optional

Page 85: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Observable

→ less than 1000 events

→ User interface management

→ To be used instead of Java 8 Streams

Flowable

→ more than 10 000 events

→ Control the data flow

→ Network stream with flow management

Page 86: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

What does Reactor offer ?

Page 87: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Flux

Mono

Maximum of 1 element

Page 88: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

ReactorContrat Backpressure

Flux [N] Yes

Mono [0|1] Yes

Identical to Flowable

Flux with only 1 element

Page 89: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Object types andReactive Streams

Page 90: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Publisher

Flux Flowable

Reactive Streams

Page 91: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Flux.range(1, 10) .flatMap(i -> Flux.just(1)) .subscribe();

Page 92: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Flux.range(1, 10) .flatMap(i -> Flux.just(1)) .subscribe();

Publisher

Page 93: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Flux.range(1, 10) .flatMap(i -> Flowable.just(1)) .subscribe();

Publisher

RxJava 2

Page 94: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Flowable.defer(() -> Flux.range(1, 10)) .subscribe(System.out::println);

RxJava 2 Reactor

Page 95: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Flux.defer(() -> Flowable.range(1, 10)) .subscribe(System.out::println);

RxJava 2Reactor

Page 96: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

You can use a library which use RxJava 2 in your Reactor project

(and vice versa)

Page 97: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Operators

Page 98: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Consequent and homogenous Catalogue

Page 99: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

all amb ambArray ambWith any as awaitOnSubscribe blockFirst blockFirstMillis blockLast blockLastMillis blockingFirst blockingForEach blockingIterable blockingLast blockingLatest blockingMostRecent blockingNext blockingSingle blockingSubscribe buffer bufferMillis bufferSize bufferTimeout bufferTimeoutMillis bufferUntil bufferWhile cache cacheWithInitialCapacity cancelOn cast checkpoint collect collectInto collectList collectMap collectMultimap collectSortedList combineLatest combineLatestDelayError compose concat concatArray concatArrayDelayError concatArrayEager concatDelayError concatEager concatMap concatMapDelayError concatMapEager concatMapEagerDelayError concatMapIterable concatWith contains count create debounce defaultIfEmpty defer delay delayElements delayElementsMillis delayMillis delaySubscription delaySubscriptionMillis dematerialize distinct distinctUntilChanged doAfterNext doAfterTerminate doFinally doOnCancel doOnComplete doOnEach doOnError doOnLifecycle doOnNext doOnRequest doOnSubscribe doOnTerminate elapsed elementAt elementAtOrError empty equals error filter first firstElement firstEmitting firstEmittingWith firstOrError flatMap flatMapCompletable flatMapIterable flatMapMaybe flatMapSequential flatMapSingle forEach forEachWhile from fromArray fromCallable fromFuture fromIterable fromPublisher fromStream generate getClass getPrefetch groupBy groupJoin handle hasElement hasElements hashCode hide ignoreElements interval intervalMillis intervalRange isEmpty join just last lastElement lastOrError lift limitRate log map mapError materialize merge mergeArray mergeArrayDelayError mergeDelayError mergeSequential mergeWith never next notify notifyAll observeOn ofType onBackpressureBuffer onBackpressureDrop onBackpressureError onBackpressureLatest onErrorResumeNext onErrorResumeWith onErrorReturn onErrorReturnItem onExceptionResumeNext onTerminateDetach parallel publish publishNext publishOn range rangeLong rebatchRequests reduce reduceWith repeat repeatUntil repeatWhen replay replayMillis retry retryUntil retryWhen safeSubscribe sample sampleFirst sampleFirstMillis sampleMillis sampleTimeout scan scanWith sequenceEqual serialize share single singleElement singleOrEmpty singleOrError skip skipLast skipMillis skipUntil skipUntilOther skipWhile sort sorted startWith startWithArray strict subscribe subscribeOn subscribeWith switchIfEmpty switchMap switchMapDelayError switchOnError switchOnNext switchOnNextDelayError take takeLast takeMillis takeUntil takeUntilOther takeWhile test then thenEmpty thenMany throttleFirst throttleLast throttleWithTimeout timeInterval timeout timeoutMillis timer timestamp to toFuture toIterable toList toMap toMultimap toObservable toSortedList toStream toString transform unsafeCreate unsubscribeOn using wait window windowMillis windowTimeout windowTimeoutMillis windowUntil windowWhile withLatestFrom zip zipArray zipIterable zipWith zipWithIterable

Page 100: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor

flatMap flatMap flatMap Emit Noe, one or more events

amb amb firstEmitting Emit events from the first emitting stream

… … … …

debounce debounce N/A Ignore events during a time laps

Page 101: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor

flatMap flatMap flatMap Emit Noe, one or more events

amb amb firstEmitting Emit events from the first emitting stream

… … … …

debounce debounce N/A Ignore events during a time laps

Renamed

Page 102: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Operators

cover a lot of scenarios

Page 103: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Nota bene

Page 104: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

https://github.com/ReactiveX/RxJava/wiki/Implementing-custom-operators-(draft)

writing operators is hardwhen one writes an operator, the Observable protocol, unsubscr iption, backpressure and concurrency have to be taken into account and adhered to the letter

Page 105: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Writing a new operator with RxJava 2

is more complex than with RxJava

Page 106: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Make a application Reactive

Page 107: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Reactive

ReactiveSynchronous API

Callback

Reactive

Reactive

Page 108: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Factory

Page 109: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor

Flowable.just Flux.just Emitting existing value

Flowable.defer Flux.defer Lazy emitting

Flowable.fromCallable Mono.fromCallable Lazy emitting, computed from a method call

Flowable.create Flux.create Manual emitting

Flowable.using Flux.using Resource management

Flowable.fromPublisher Flux.from Using a Publisher (Reactive Streams)

Flowable.generate Flux.generate Using a value generator

Page 110: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor

Flowable.just Flux.just Emitting existing value

Flowable.defer Flux.defer Lazy emitting

Flowable.fromCallable Mono.fromCallable Lazy emitting, computed from a method call

Flowable.create Flux.create Manual emitting

Flowable.using Flux.using Resource management

Flowable.fromPublisher Flux.from Using a Publisher (Reactive Streams)

Flowable.generate Flux.generate Using a value generator

Page 111: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor

Flowable.just Flux.just Emitting existing value

Flowable.defer Flux.defer Lazy emitting

Flowable.fromCallable Mono.fromCallable Lazy emitting, computed from a method call

Flowable.create Flux.create Manual emitting

Flowable.using Flux.using Resource management

Flowable.fromPublisher Flux.from Using a Publisher (Reactive Streams)

Flowable.generate Flux.generate Using a value generator

Page 112: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor

Flowable.just Flux.just Emitting existing value

Flowable.defer Flux.defer Lazy emitting

Flowable.fromCallable Mono.fromCallable Lazy emitting, computed from a method call

Flowable.create Flux.create Manual emitting

Flowable.using Flux.using Resource management

Flowable.fromPublisher Flux.from Using a Publisher (Reactive Streams)

Flowable.generate Flux.generate Using a value generator

Page 113: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

example of wrapping

Page 114: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

@RestControllerpublic class HelloController { private static final byte[] TOPIC_NAME = "topic".getBytes(); @RequestMapping(value = "/redis") private String redis() throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); AtomicReference<String> result = new AtomicReference<>(); this.connection.subscribe((message, pattern) -> { result.set(message.toString()); latch.countDown(); }, TOPIC_NAME); latch.await(); return result.get(); }}

Page 115: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

@RestControllerpublic class HelloController { private static final byte[] TOPIC_NAME = "topic".getBytes(); @RequestMapping(value = "/redis") private String redis() throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); AtomicReference<String> result = new AtomicReference<>(); this.connection.subscribe((message, pattern) -> { result.set(message.toString()); latch.countDown(); }, TOPIC_NAME); latch.await(); return result.get(); }}

Page 116: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

@RestControllerpublic class HelloController { private static final byte[] TOPIC_NAME = "topic".getBytes(); @RequestMapping(value = "/redis") private String redis() throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); AtomicReference<String> result = new AtomicReference<>(); this.connection.subscribe((message, pattern) -> { result.set(message.toString()); latch.countDown(); }, TOPIC_NAME); latch.await(); return result.get(); }}

Page 117: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

@RestControllerpublic class HelloController { private static final byte[] TOPIC_NAME = "topic".getBytes(); @RequestMapping(value = "/redis") private String redis() throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); AtomicReference<String> result = new AtomicReference<>(); this.connection.subscribe((message, pattern) -> { result.set(message.toString()); latch.countDown(); }, TOPIC_NAME); latch.await(); return result.get(); }}

Code for synchronisation

Code for synchronisation

Page 118: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Step 1 Wrapping

Page 119: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

@RestControllerpublic class HelloController { private static final byte[] TOPIC_NAME = "topic".getBytes(); @RequestMapping(value = "/redis") private String redis() throws InterruptedException { String result = Flowable.create(sub -> { this.connection.subscribe((message, pattern) -> { sub.onNext(message.toString()); sub.onComplete(); }, TOPIC_NAME); }, BackpressureStrategy.BUFFER) .blockingFirst(); return result; }}

Page 120: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

@RestControllerpublic class HelloController { private static final byte[] TOPIC_NAME = "topic".getBytes(); @RequestMapping(value = "/redis") private String redis() throws InterruptedException { String result = Flowable.create(sub -> { this.connection.subscribe((message, pattern) -> { sub.onNext(message.toString()); sub.onComplete(); }, TOPIC_NAME); }, BackpressureStrategy.BUFFER) .blockingFirst(); return result; }}

Wrapping

Synchronisation

Reactive Contract

Page 121: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Step 2 Asynchronous

Page 122: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

@RestControllerpublic class HelloController { private static final byte[] TOPIC_NAME = "topic".getBytes(); @RequestMapping(value = "/redis") private DeferredResult<String> redis() throws InterruptedException { DeferredResult<String> result = new DeferredResult<>(10_000l); Flowable.create(sub -> { this.connection.subscribe((message, pattern) -> { sub.onNext(message.toString()); sub.onComplete(); }, TOPIC_NAME); }, BackpressureStrategy.BUFFER) .subscribe(result::setResult); return result; }

Page 123: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

@RestControllerpublic class HelloController { private static final byte[] TOPIC_NAME = "topic".getBytes(); @RequestMapping(value = "/redis") private DeferredResult<String> redis() throws InterruptedException { DeferredResult<String> result = new DeferredResult<>(10_000l); Flowable.create(sub -> { this.connection.subscribe((message, pattern) -> { sub.onNext(message.toString()); sub.onComplete(); }, TOPIC_NAME); }, BackpressureStrategy.BUFFER) .subscribe(result::setResult); return result; } Lazy result

Use of DeferredResult

Page 124: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Step 3 Reactive Streams

Page 125: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

@RestControllerpublic class HelloController { private static final byte[] TOPIC_NAME = "topic".getBytes(); @RequestMapping(value = "/redis", produces = MediaType.TEXT_EVENT_STREAM_VALUE) private Flux<String> redis() throws InterruptedException { Flowable<String> rxjava = Flowable.create(sub -> { this.connection.subscribe((message, pattern) -> sub.onNext(message.toString()), TOPIC_NAME); }, BackpressureStrategy.BUFFER); return Flux.defer(() -> rxjava); }}

Page 126: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

@RestControllerpublic class HelloController { private static final byte[] TOPIC_NAME = "topic".getBytes(); @RequestMapping(value = "/redis", produces = MediaType.TEXT_EVENT_STREAM_VALUE) private Flux<String> redis() throws InterruptedException { Flowable<String> rxjava = Flowable.create(sub -> { this.connection.subscribe((message, pattern) -> sub.onNext(message.toString()), TOPIC_NAME); }, BackpressureStrategy.BUFFER); return Flux.defer(() -> rxjava); }}

RxJava 2 → Flux

Flux → SSEReturn a Flux

Page 127: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

@RestControllerpublic class HelloController { private static final byte[] TOPIC_NAME = "topic".getBytes(); @RequestMapping(value = "/redis", produces = MediaType.TEXT_EVENT_STREAM_VALUE) private Publisher<String> redis() throws InterruptedException { return Flowable.create(sub -> { this.connection.subscribe((message, pattern) -> sub.onNext(message.toString()), TOPIC_NAME); }, BackpressureStrategy.BUFFER); }

Page 128: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

@RestControllerpublic class HelloController { private static final byte[] TOPIC_NAME = "topic".getBytes(); @RequestMapping(value = "/redis", produces = MediaType.TEXT_EVENT_STREAM_VALUE) private Publisher<String> redis() throws InterruptedException { return Flowable.create(sub -> { this.connection.subscribe((message, pattern) -> sub.onNext(message.toString()), TOPIC_NAME); }, BackpressureStrategy.BUFFER); }

Publisher

Page 129: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service
Page 130: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Reactor use Java 8 while RxJava 2 use Java 6

Page 131: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Reactor use Java 8 while RxJava 2 use Java 6

Page 132: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Asynchronous Execution context management

Page 133: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Schedulers

Rethink cette partie là

Page 134: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Scheduler (Thread Pool)

Task 1

Task 2

Task 3

Task 4

Page 135: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Task 1

Task 2

Task 3

Task 4

Scheduler (Thread Pool)

Page 136: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Task 1 Task 2

Task 3

Task 4

Scheduler (Thread Pool)

Page 137: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Task 1 Task 2

Task 3

Task 4

Scheduler (Thread Pool)

Page 138: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Task 1 Task 2

Task 3 Task 4

Scheduler (Thread Pool)

Page 139: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Task 1

Task 2

Task 3 Task 4

Scheduler (Thread Pool)

Page 140: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Task 1

Task 2

Task 3

Task 4

Scheduler (Thread Pool)

Page 141: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Task 1

Task 2

Task 3

Task 4

Scheduler (Thread Pool)

Page 142: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Task 1

Task 2

Task 3

Task 4

Scheduler (Thread Pool)

Page 143: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

java.lang.IllegalStateException:

Not on the main thread

NetworkOnMainThreadException

Page 144: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

JavaFx.fromClick(btn) .observeOn(Schedulers.io()) .switchMap(evt -> remoteApi.getData()) .observeOn(Schedulers.computation()) .flatMap(data -> intensiveComputation(data)) .observeOn(javaFx()) .doOnNext(value -> btn.setText("Data: " + value)) .subscribe();

Page 145: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

JavaFx.fromClick(btn) .observeOn(Schedulers.io()) .switchMap(evt -> remoteApi.getData()) .observeOn(Schedulers.computation()) .flatMap(data -> intensiveComputation(data)) .observeOn(javaFx()) .doOnNext(value -> btn.setText("Data: " + value)) .subscribe();

i/o

computation

UI Thread

Page 146: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor Description

io() io() elastic() Thread pool which grow up if needed

computation() computation() parallel() Limited thread pool

single() single() single() Pool of 1 thread

immediate() immediate() Execute the task immediately

trampoline() trampoline() Queue the current task

Page 147: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor Description

io() io() elastic() Thread pool which grow up if needed

computation() computation() parallel() Limited thread pool

single() single() single() Pool of 1 thread

immediate() immediate() Execute the task immediately

trampoline() trampoline() Queue the current task

Page 148: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor Description

io() io() elastic() Thread pool which grow up if needed

computation() computation() parallel() Limited thread pool

single() single() single() Pool of 1 thread

immediate() immediate() Execute the task immediately

trampoline() trampoline() Queue the current task

Page 149: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Reactor Technical naming

Page 150: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava Functional naming

Page 151: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Performance

Page 152: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Generation

2 3 4 5+

Fusion

RxJava 2 Reactor

Page 153: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Warning Conceptuel slides

Page 154: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Without fusion

Page 155: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Transform ( ⃞ → ⃝ )

Transform ( ⃝ → ⃞)

Page 156: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Transform ( ⃞ → ⃝ )

Transform ( ⃝ → ⃞)

Page 157: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Transform ( ⃞ → ⃝ )

Transform ( ⃝ → ⃞)

Page 158: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Transform ( ⃞ → ⃝ )

Transform ( ⃝ → ⃞)

Page 159: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Transform ( ⃞ → ⃝ )

Transform ( ⃝ → ⃞)

Page 160: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Transform ( ⃞ → ⃝ )

Transform ( ⃝ → ⃞)

Page 161: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

With fusion

Page 162: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Transform ( ⃞ → ⃝ )

Transform ( ⃝ → ⃞)

Page 163: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Transform ( ⃞ → ⃝ )

Transform ( ⃝ → ⃞)

Page 164: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Transform ( ⃞ → ⃝ )

Transform ( ⃝ → ⃞)

Page 165: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Transform ( ⃞ → ⃝ )

Transform ( ⃝ → ⃞)

Page 166: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Transform ( ⃞ → ⃝ )

Transform ( ⃝ → ⃞)

Page 167: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Fusion decreases

memory consumption and increases

performances

Page 168: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

for (int x = 0; x < 10_000; x++) { Observable.interval(10, TimeUnit.MILLISECONDS) .takeWhile(i -> take.get()) .flatMap(i -> Observable.range(1, 100)) .subscribe();}

Page 169: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava 2 RxJavaReactor

Page 170: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

http://akarnokd.blogspot.fr/2016/12/the-reactive-scrabble-benchmarks.html

Java 8 Stream

RxJava 2 / Reactor

RxJava

December 2016

Page 171: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Java 8 Stream

RxJava 2 / Reactor

RxJava

https://twitter.com/akarnokd/status/844555409012740096

March 2017

Page 172: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Ecosystem

Page 173: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor

Retrofit Yes Yes No

RxAndroid Yes Yes No

Realm Yes No No

Hystrix Yes No No

Couchbase Yes No No

MongoDB Yes No No

Spring Data 2.0 Yes No Yes

Reactor IPC No No Yes

WebFlux No Yes Yes

Android

Spring

Page 174: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor

Retrofit Yes Yes No

RxAndroid Yes Yes No

Realm Yes No No

Hystrix Yes No No

Couchbase Yes No No

MongoDB Yes No No

Spring Data 2.0 Yes No Yes

Reactor IPC No No Yes

WebFlux No Yes Yes

Android

Spring

Page 175: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

We are aggressively

migrating our internal code to

RxJava 2 https://github.com/uber/AutoDispose

Page 176: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor

Retrofit Yes Yes No

RxAndroid Yes Yes No

Realm Yes No No

Hystrix Yes No No

Couchbase Yes No No

MongoDB Yes No No

Spring Data 2.0 Yes No Yes

Reactor IPC No No Yes

WebFlux No Yes Yes

Android

Spring

Page 177: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Inertia to migrate

Page 178: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

RxJava RxJava 2 Reactor

Retrofit Yes Yes No

RxAndroid Yes Yes No

Realm Yes No No

Hystrix Yes No No

Couchbase Yes No No

MongoDB Yes No No

Spring Data 2.0 Yes No Yes

Reactor IPC No No Yes

WebFlux No Yes Yes

Android

Spring

Page 179: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Spring 5 will accelerate the adoption of Reactor

Page 180: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Android RxJava 2

Page 181: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Backend RxJava 2

Page 182: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Spring Reactor

Page 183: Got a question during this session? Post it on sli.do ( # ... · Future future = ex.submit(() -> longProcessing()); ... Completable [0] No Added afterward Web service

Thanks for your attention

We stay in touch?

[email protected]

@dwursteisen

http://blog.soat.fr

Post your question on sli.do ( #K100 )