Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

RxJava - Getting Started

David
October 16, 2014

RxJava - Getting Started

Les applications sont de plus en plus interconnectées. Une architecture type WOA (Web Oriented Archiecture) et l’utilisation des micros-services nécessitent de faire de plus en plus appel à différents services web. Comment composer un résultat à partir de ces différents services, sans avoir à ce soucier de l’ordre dans lequel les serveurs vont répondre ?

RxJava offre une manière élégante de faire de l’asynchrone et de la composition au sein de son application. David vous exposera les concepts de Rx (Reactive eXtension) avant de vous montrer une mise en application avec des exemples de code venant d’une application Android.

David Wursteisen / Soat / 16 Octobre 2014

David

October 16, 2014
Tweet

More Decks by David

Other Decks in Programming

Transcript

  1. Complexité client.execute(new Callback() { @Override public void completed(HttpResponse response) {

    client.execute(new Callback() { @Override public void completed(HttpResponse response) { } }); } });
  2. Complexité client.execute(new Callback() { @Override public void completed(HttpResponse response) {

    client.execute(new Callback() { @Override public void completed(HttpResponse response) { client.execute(new Callback() { @Override public void completed(HttpResponse response) { } }); } });
  3. Complexité client.execute(new Callback() { @Override public void completed(HttpResponse response) {

    client.execute(new Callback() { @Override public void completed(HttpResponse response) { client.execute(new Callback() { @Override public void completed(HttpResponse response) { } }); } }); Callback hell
  4. Manipulation d’évènements Observable.from(0, 1, 2, 3, 4) .filter((i) -> i

    < 3) .delay(5, TimeUnit.SECONDS) Observable<Integer> Observable<Integer> Observable<Integer>
  5. Manipulation d’évènements Observable.from(0, 1, 2, 3, 4) .filter((i) -> i

    < 3) .delay(5, TimeUnit.SECONDS) .map((i) -> Character.toString((char)(i + 'A'))) Observable<Integer> Observable<Integer> Observable<Integer> Observable<String>
  6. Manipulation d’évènements Observable.from(0, 1, 2, 3, 4) .filter((i) -> i

    < 3) .delay(5, TimeUnit.SECONDS) .map((i) -> Character.toString((char)(i + 'A'))) .subscribe(System.out::println); Observable<Integer> Observable<Integer> Observable<Integer> Observable<String>
  7. Manipulation d’évènements Observable.from(0, 1, 2, 3, 4) .filter((i) -> i

    < 3) .delay(5, TimeUnit.SECONDS) .map((i) -> Character.toString((char)(i + 'A'))) .subscribe(System.out::println); 0, 1, 2, 3, 4 => A, B, C Observable<Integer> Observable<Integer> Observable<Integer> Observable<String>
  8. Subscription Observable.create(new OnSubscribe<T>() { @Override public void call(Subscriber<? super T>

    subscriber) { // ... httpClient.execute(httpRequest, new FutureCallback<HttpResponse>() { @Override public void completed(String content) { subscriber.onNext(content); subscriber.onCompleted(); } @Override public void failed(Exception e) { subscriber.onError(e); } }); } });
  9. Subscription Observable.create(new OnSubscribe<T>() { @Override public void call(Subscriber<? super T>

    subscriber) { // ... httpClient.execute(httpRequest, new FutureCallback<HttpResponse>() { @Override public void completed(String content) { subscriber.onNext(content); subscriber.onCompleted(); } @Override public void failed(Exception e) { subscriber.onError(e); } }); } });
  10. Subscription Observable.create(new OnSubscribe<T>() { @Override public void call(Subscriber<? super T>

    subscriber) { // ... httpClient.execute(httpRequest, new FutureCallback<HttpResponse>() { @Override public void completed(String content) { subscriber.onNext(content); subscriber.onCompleted(); } @Override public void failed(Exception e) { subscriber.onError(e); } }); } });
  11. Subscription Observable.create(new OnSubscribe<T>() { @Override public void call(Subscriber<? super T>

    subscriber) { // ... httpClient.execute(httpRequest, new FutureCallback<HttpResponse>() { @Override public void completed(String content) { subscriber.onNext(content); subscriber.onCompleted(); } @Override public void failed(Exception e) { subscriber.onError(e); } }); } }); Émission asynchronne
  12. Subscription Observable.create(new OnSubscribe<T>() { @Override public void call(Subscriber<? super T>

    subscriber) { // ... httpClient.execute(httpRequest, new FutureCallback<HttpResponse>() { @Override public void completed(String content) { subscriber.onNext(content); subscriber.onCompleted(); } @Override public void failed(Exception e) { subscriber.onError(e); } }); } }); Propagation des erreurs
  13. Hot Observable ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); obs.connect();

    obs.subscribe(System.out::println); obs.subscribe(System.out::println); Souscription
  14. Opérateurs Observable.from(1, 2, 3, 4) .reduce(new LinkedList<Integer>(), (seed, value) ->

    { seed.add(value); return seed; }) .first() .subscribe(System.out::println);
  15. Opérateurs Observable.from(1, 2, 3, 4) .reduce(new LinkedList<Integer>(), (seed, value) ->

    { seed.add(value); return seed; }) .first() .subscribe(System.out::println);
  16. Lambda & Java 8 Observable.from(1, 2, 3, 4) .filter(new Func1<Integer,

    Boolean>() { @Override public Boolean call(Integer i) { return i > 3; } }) .subscribe(new Action1<Integer>() { @Override public void call(Integer x) { System.out.println(x); } });
  17. Lambda & Java 8 Observable.from(1, 2, 3, 4) .filter(new Func1<Integer,

    Boolean>() { @Override public Boolean call(Integer i) { return i > 3; } }) .subscribe(new Action1<Integer>() { @Override public void call(Integer x) { System.out.println(x); } }); Bruit
  18. Lambda & Java 8 Observable.from(1, 2, 3, 4) .filter(i ->

    i > 3) .subscribe(System.out::println);
  19. Lambda & Java 8 Observable.from(1, 2, 3, 4) .filter(new Func1<Integer,

    Boolean>() { @Override public Boolean call(Integer i) { return i > 3; } }) .subscribe(new Action1<Integer>() { @Override public void call(Integer x) { System.out.println(x); } }); Observable.from(1, 2, 3, 4) .filter(i -> i > 3) .subscribe(System.out::println); Retrolambda
  20. Reactive Streams Reactive Streams is an initiative to provide a

    standard for asynchronous stream processing with non-blocking back pressure on the JVM. http://www.reactive-streams.org/
  21. Reactive Streams RxJava | Akka Streams | Reactor Composable |

    Ratpack ~1Mo | Java | RingBufferDispatcher
  22. Si il ne fallait retenir qu’une chose RxJava est un

    modèle de programmation pour écrire des applications asynchrones Frontend | Backend | Crossplatform