RxJava 
Getting Started 
David Wursteisen 16 Octobre 2014
David Wursteisen 
Direction Expertise Innovation 
@dwursteisen
David Wursteisen
Architecture distribuée 
Sync/Async/Concurrence
Architecture distribuée
Architecture distribuée
Architecture distribuée 
The Internet
Architecture distribuée 
The Internet
Synchrone
Synchrone 
Appel bloquant
Asynchrone
Asynchrone 
Appel non 
bloquant
Complexité
Complexité 
future.get();
Complexité 
future1.get(); 
future2.get(); 
future3.get(); 
future4.get(); 
future5.get(); 
future6.get(); 
future7.get();
Complexité 
future1.get(); 
future2.get(); 
future3.get(); 
future4.get(); 
future5.get(); 
future6.get(); 
future7.get();...
Complexité 
client.execute(new Callback() { 
@Override 
public void completed(HttpResponse response) { 
} 
});
Complexité 
client.execute(new Callback() { 
@Override 
public void completed(HttpResponse response) { 
client.execute(new...
Complexité 
client.execute(new Callback() { 
@Override 
public void completed(HttpResponse response) { 
client.execute(new...
Complexité 
client.execute(new Callback() { 
@Override 
public void completed(HttpResponse response) { 
client.execute(new...
Concurrence
Concurrence
Concurrence 
Concurrence
Concurrence
Concurrence 
RxJava permet de manipuler des 
évènements d’une manière synchrone 
et/ou asynchrone.
Historique 
Il était une fois...
Historique
Historique
Historique
Historique
Historique 
ReactiveX.io
Observables 
Travailler avec des flux
Flux d’évènements fini 
Évènements Fin du flux
Flux d’évènements en erreur 
Évènements Erreur
Flux d’évènements infini 
Évènements
Flux d’évènements 
Observable.never() 
Observable.empty() 
Observable.just(1) 
Observable.from(1, 2, 3) 
Observable.range(...
Flux d’évènements 
Observable.never() 
Observable.empty() 
Observable.just(1) 
Observable.from(1, 2, 3) 
Observable.range(...
Flux d’évènements 
Observable.never() 
Observable.empty() 
Observable.just(1) 
Observable.from(1, 2, 3) 
Observable.range(...
Flux d’évènements 
Observable.never() 
Observable.empty() 
Observable.just(1) 
Observable.from(1, 2, 3) 
Observable.range(...
Flux d’évènements 
Observable.never() 
Observable.empty() 
Observable.just(1) 
Observable.from(1, 2, 3) 
Observable.range(...
Flux d’évènements 
Observable.never() 
Observable.empty() 
Observable.just(1) 
Observable.from(1, 2, 3) 
Observable.range(...
Flux d’évènements 
Observable.never() 
Observable.empty() 
Observable.just(1) 
Observable.from(1, 2, 3) 
Observable.range(...
Flux d’évènements 
Observable<json> json json json 
λ ϕ φ 
Observable<Integer> 
Observable<Click>
Observer 
Push des données
Observer 
OnNext* (OnCompleted|OnError)?
Observer 
OnNext* (OnCompleted|OnError)?
Observer 
OnNext* (OnCompleted|OnError)?
Observer 
OnNext* (OnCompleted|OnError)?
Observer 
Observable.range(1, 4400)
Observer 
Observable.range(1, 4400).subscribe()
Observer 
Observable.range(1, 4400).subscribe(onNext)
Observer 
Observable.range(1, 4400).subscribe(System.out::println)
Observer 
Observable.range(1, 4400).subscribe(System.out::println, 
onError)
Observer 
Observable.range(1, 4400).subscribe(System.out::println, 
System.err::println)
Observer 
Observable.range(1, 4400).subscribe(System.out::println, 
System.err::println, 
onCompleted)
Observer 
Observable.range(1, 4400).subscribe(System.out::println, 
System.err::println, 
() -> System.out.println(“finish...
Manipulation d’évènements 
Quand l’écoute ne suffit plus
Manipulation d’évènements 
0 1
Manipulation d’évènements 
0 1 
map
Manipulation d’évènements 
0 1 
map 
A B
Manipulation d’évènements 
1 4
Manipulation d’évènements 
1 4 
filter ( x <= 2 )
Manipulation d’évènements 
1 4 
1 
filter ( x <= 2 )
Manipulation d’évènements 
1 2
Manipulation d’évènements 
1 2 
delay ( 5, SECONDS )
Manipulation d’évènements 
1 2 
delay ( 5, SECONDS ) 
1 
2
Manipulation d’évènements 
Observable.from(0, 1, 2, 3, 4) 
Observable<Integer>
Manipulation d’évènements 
Observable.from(0, 1, 2, 3, 4) 
.filter((i) -> i < 3) 
Observable<Integer> 
Observable<Integer>
Manipulation d’évènements 
Observable.from(0, 1, 2, 3, 4) 
.filter((i) -> i < 3) 
.delay(5, TimeUnit.SECONDS) 
Observable<...
Manipulation d’évènements 
Observable.from(0, 1, 2, 3, 4) 
.filter((i) -> i < 3) 
.delay(5, TimeUnit.SECONDS) 
.map((i) ->...
Manipulation d’évènements 
Observable.from(0, 1, 2, 3, 4) 
.filter((i) -> i < 3) 
.delay(5, TimeUnit.SECONDS) 
.map((i) ->...
Manipulation d’évènements 
Observable.from(0, 1, 2, 3, 4) 
.filter((i) -> i < 3) 
.delay(5, TimeUnit.SECONDS) 
.map((i) ->...
Manipulation d’évènements
Manipulation d’évènements
Manipulation d’évènements
Composition 
Création d’un flux à partir d’un ensemble de flux
Composition
Composition
Composition
Mise en pratique de composition 
Learn from the trenches
Composition
Composition
Composition
Composition
Composition
Composition 
merge
Composition 
merge
Composition
Composition
Composition 
zip
Composition 
zip
Subscription 
Et gestion de l’unsubscribe
Subscription 
Observable.create(new OnSubscribe<T>() { … });
Subscription 
Observable.create(new OnSubscribe<T>() { 
@Override 
public void call(Subscriber<? super T> s) { 
s.onNext(1...
Subscription 
Observable.create(new OnSubscribe<T>() { 
@Override 
public void call(Subscriber<? super T> s) { 
s.onNext(1...
Subscription 
Observable.create(new OnSubscribe<T>() { 
@Override 
public void call(Subscriber<? super T> subscriber) { 
/...
Subscription 
Observable.create(new OnSubscribe<T>() { 
@Override 
public void call(Subscriber<? super T> subscriber) { 
/...
Subscription 
Observable.create(new OnSubscribe<T>() { 
@Override 
public void call(Subscriber<? super T> subscriber) { 
/...
Subscription 
Observable.create(new OnSubscribe<T>() { 
@Override 
public void call(Subscriber<? super T> subscriber) { 
/...
Subscription 
Observable.create(new OnSubscribe<T>() { 
@Override 
public void call(Subscriber<? super T> subscriber) { 
/...
Subscription 
La souscription permet d’uniformiser des API 
différentes par leurs natures (Callback, etc...)
Unsubscribe 
Subscription subscription = Observable.interval(1, TimeUnit.SECONDS).subscribe();
Unsubscribe 
Souscription 
Subscription subscription = Observable.interval(1, TimeUnit.SECONDS).subscribe();
Unsubscribe 
Handler sur la souscription 
Subscription subscription = Observable.interval(1, TimeUnit.SECONDS).subscribe()...
Unsubscribe 
Subscription subscription = Observable.interval(1, TimeUnit.SECONDS).subscribe(); 
/* ... */ 
subscription.un...
Unsubscribe 
Subscription subscription = Observable.interval(1, TimeUnit.SECONDS).subscribe(); 
/* ... */ 
subscription.un...
Schedulers
Schedulers 
observableReadingSynchronously(“strings.txt”) 
.take(10) 
.delay(1, SECONDS) 
.map(parse()) 
.map(n -> “=> ” +...
Schedulers 
main
Schedulers 
observableReadingSynchronously(“strings.txt”) 
.take(10) 
.delay(1, SECONDS) 
.map(parse()) 
.map(n -> “=> ” +...
Schedulers 
main 
IO 
Computation 
UI
Schedulers 
main 
IO 
Computation 
UI
Schedulers 
main 
IO 
Computation 
UI
Schedulers 
main 
IO 
Computation 
UI
Schedulers 
main 
IO 
Computation 
UI
Schedulers 
main 
IO 
Computation 
UI
Schedulers 
observableReadingSynchronously(“strings.txt”) 
.subscribeOn(scheduler) 
.take(10) 
.delay(1, SECONDS, schedule...
Schedulers 
observableReadingSynchronously(“strings.txt”) 
.subscribeOn(Schedulers.io()) 
.take(10) 
.delay(1, SECONDS, Sc...
Schedulers 
Computation 
UI
Schedulers 
Computation 
UI
Schedulers 
Computation 
UI
Schedulers 
Computation 
UI
Schedulers 
Computation 
UI
Hot & Cold Observable
Cold Observable 
Observable<Integer> obs = Observable.from(1, 2, 3, 4); 
obs.subscribe(System.out::println); 
obs.subscrib...
Cold Observable 
Observable<Integer> obs = Observable.from(1, 2, 3, 4); 
obs.subscribe(System.out::println); 
obs.subscrib...
Cold Observable 
Observable<Integer> obs = Observable.from(1, 2, 3, 4); 
obs.subscribe(System.out::println); 
Subscribe 
o...
Cold Observable 
Observable<Integer> obs = Observable.from(1, 2, 3, 4); 
obs.subscribe(System.out::println); 
obs.subscrib...
Cold Observable 
Observable<Integer> obs = Observable.from(1, 2, 3, 4); 
obs.subscribe(System.out::println); 
obs.subscrib...
Hot Observable 
ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); 
obs.subscribe(System.out::pri...
Hot Observable 
ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); 
obs.subscribe(System.out::pri...
Hot Observable 
ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); 
obs.subscribe(System.out::pri...
Hot Observable 
ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); 
obs.subscribe(System.out::pri...
Hot Observable 
ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); 
obs.subscribe(System.out::pri...
Hot Observable 
ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); 
obs.subscribe(System.out::pri...
Hot Observable 
ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); 
obs.subscribe(System.out::pri...
Hot Observable 
ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); 
obs.subscribe(System.out::pri...
Hot Observable 
ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); 
obs.connect(); 
Souscription ...
Cold & Hot Observable 
Cold Observable = flux passif 
Hot Observable = flux actif
Back Pressure 
Contrôler la production
Back Pressure 
Production
Back Pressure 
Production 
buffer
Back Pressure 
Production 
Pression arrière
Back Pressure 
Demande de production de n élements 
Production 
Pression arrière
Retour d’expérience
Opérateurs 
Observable.from(1, 2, 3, 4) 
.reduce(new LinkedList<Integer>(), (seed, value) -> { 
seed.add(value); 
return s...
Opérateurs 
Observable.from(1, 2, 3, 4) 
.reduce(new LinkedList<Integer>(), (seed, value) -> { 
seed.add(value); 
return s...
Opérateurs 
Observable.from(1, 2, 3, 4) 
.toList() 
.first() 
.subscribe(System.out::println);
Opérateurs 
Observable.from(1, 2, 3, 4) 
.toList() 
.first() 
.subscribe(System.out::println);
Opérateurs 
Observable.from(1, 2, 3, 4) 
.toList() 
.first() 
.subscribe(System.out::println); 
Différence entre first() /...
Gestion des erreurs 
Observable.from(1, 2, 3, 4) 
.toList() 
.single() 
.subscribe(System.out::println);
Gestion des erreurs 
Observable.from(1, 2, 3, 4) 
.toList() 
.single() 
.subscribe(System.out::println); 
Notification des...
Gestion des erreurs 
static { 
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() { 
@Override 
pub...
Lambda & Java 8 
Observable.from(1, 2, 3, 4) 
.filter(new Func1<Integer, Boolean>() { 
@Override 
public Boolean call(Inte...
Lambda & Java 8 
Observable.from(1, 2, 3, 4) 
.filter(new Func1<Integer, Boolean>() { 
@Override 
public Boolean call(Inte...
Lambda & Java 8 
Observable.from(1, 2, 3, 4) 
.filter(i -> i > 3) 
.subscribe(System.out::println);
Lambda & Java 8 
Observable.from(1, 2, 3, 4) 
.filter(new Func1<Integer, Boolean>() { 
@Override 
public Boolean call(Inte...
Concurrence 
obs1.mergeWith(obs2) 
.take(5) 
.subscribe(System.out::println);
Concurrence 
obs1.mergeWith(obs2) 
.take(5) 
.subscribe(System.out::println);
Concurrence 
obs1.mergeWith(obs2) 
Doit gérer la concurrence 
.take(5) 
.subscribe(System.out::println);
Concurrence 
obs1.mergeWith(obs2) 
.take(5) 
.subscribe(System.out::println);
Concurrence 
obs1.mergeWith(obs2) 
.take(5) 
.subscribe(System.out::println); 
non concurrence
Sync & Async 
Observable<Integer> generator();
Sync & Async 
Observable<Integer> generator(); 
Sync ?
Sync & Async 
Sync ? Async ? 
Observable<Integer> generator();
Sync & Async 
Sync ? Async ? 
Observable<Integer> generator(); 
Computation ?
Sync & Async 
Sync ? Async ? 
Observable<Integer> generator(); 
Computation ? Acteur ?
“Leak” des Observables 
everything is an event
“Leak” des Observables 
ViewPager
“Leak” des Observables 
ViewPager 
Observable 
Preferences
“Leak” des Observables 
ViewPager 
Observable 
Preferences 
Observable 
UserService
Future<RxJava>
Future<RxJava> 
Support RxJava
Reactive Streams
Reactive Streams 
Reactive Streams is an initiative to 
provide a standard for asynchronous 
stream processing with non-bl...
Reactive Streams 
RxJava | Akka Streams | Reactor Composable | Ratpack
Reactive Streams 
RxJava | Akka Streams | Reactor Composable | Ratpack 
> 700 Ko | Java | Android
Reactive Streams 
RxJava | Akka Streams | Reactor Composable | Ratpack 
> 2.5 Mo | Scala | Akka
Reactive Streams 
RxJava | Akka Streams | Reactor Composable | Ratpack 
~1Mo | Java | RingBufferDispatcher
Reactive Streams 
RxJava | Akka Streams | Reactor Composable | Ratpack 
Java 8 | Http
Si il ne fallait retenir qu’une chose
Si il ne fallait retenir qu’une chose 
RxJava est un modèle de programmation pour 
écrire des applications asynchrones 
Fr...
Questions ? 
david.wursteisen@soat.fr 
@dwursteisen
RxJava, Getting Started - David Wursteisen - 16 Octobre 2014
Prochain SlideShare
Chargement dans…5
×

RxJava, Getting Started - David Wursteisen - 16 Octobre 2014

1 792 vues

Publié le

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.

Publié dans : Technologie
0 commentaire
16 j’aime
Statistiques
Remarques
  • Soyez le premier à commenter

Aucun téléchargement
Vues
Nombre de vues
1 792
Sur SlideShare
0
Issues des intégrations
0
Intégrations
38
Actions
Partages
0
Téléchargements
24
Commentaires
0
J’aime
16
Intégrations 0
Aucune incorporation

Aucune remarque pour cette diapositive

RxJava, Getting Started - David Wursteisen - 16 Octobre 2014

  1. 1. RxJava Getting Started David Wursteisen 16 Octobre 2014
  2. 2. David Wursteisen Direction Expertise Innovation @dwursteisen
  3. 3. David Wursteisen
  4. 4. Architecture distribuée Sync/Async/Concurrence
  5. 5. Architecture distribuée
  6. 6. Architecture distribuée
  7. 7. Architecture distribuée The Internet
  8. 8. Architecture distribuée The Internet
  9. 9. Synchrone
  10. 10. Synchrone Appel bloquant
  11. 11. Asynchrone
  12. 12. Asynchrone Appel non bloquant
  13. 13. Complexité
  14. 14. Complexité future.get();
  15. 15. Complexité future1.get(); future2.get(); future3.get(); future4.get(); future5.get(); future6.get(); future7.get();
  16. 16. Complexité future1.get(); future2.get(); future3.get(); future4.get(); future5.get(); future6.get(); future7.get(); Ordonnancement optimal ?
  17. 17. Complexité client.execute(new Callback() { @Override public void completed(HttpResponse response) { } });
  18. 18. Complexité client.execute(new Callback() { @Override public void completed(HttpResponse response) { client.execute(new Callback() { @Override public void completed(HttpResponse response) { } }); } });
  19. 19. 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) { } }); } });
  20. 20. 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
  21. 21. Concurrence
  22. 22. Concurrence
  23. 23. Concurrence Concurrence
  24. 24. Concurrence
  25. 25. Concurrence RxJava permet de manipuler des évènements d’une manière synchrone et/ou asynchrone.
  26. 26. Historique Il était une fois...
  27. 27. Historique
  28. 28. Historique
  29. 29. Historique
  30. 30. Historique
  31. 31. Historique ReactiveX.io
  32. 32. Observables Travailler avec des flux
  33. 33. Flux d’évènements fini Évènements Fin du flux
  34. 34. Flux d’évènements en erreur Évènements Erreur
  35. 35. Flux d’évènements infini Évènements
  36. 36. Flux d’évènements Observable.never() Observable.empty() Observable.just(1) Observable.from(1, 2, 3) Observable.range(1, 4400) Observable.timer(5, TimeUnit.SECONDS) Observable.create(...)
  37. 37. Flux d’évènements Observable.never() Observable.empty() Observable.just(1) Observable.from(1, 2, 3) Observable.range(1, 4400) Observable.timer(5, TimeUnit.SECONDS) Observable.create(...)
  38. 38. Flux d’évènements Observable.never() Observable.empty() Observable.just(1) Observable.from(1, 2, 3) Observable.range(1, 4400) Observable.timer(5, TimeUnit.SECONDS) Observable.create(...) 1
  39. 39. Flux d’évènements Observable.never() Observable.empty() Observable.just(1) Observable.from(1, 2, 3) Observable.range(1, 4400) Observable.timer(5, TimeUnit.SECONDS) Observable.create(...) 1 2 3
  40. 40. Flux d’évènements Observable.never() Observable.empty() Observable.just(1) Observable.from(1, 2, 3) Observable.range(1, 4400) Observable.timer(5, TimeUnit.SECONDS) Observable.create(...) 1 4400
  41. 41. Flux d’évènements Observable.never() Observable.empty() Observable.just(1) Observable.from(1, 2, 3) Observable.range(1, 4400) Observable.timer(5, TimeUnit.SECONDS) Observable.create(...) 0 t t + 5 secondes
  42. 42. Flux d’évènements Observable.never() Observable.empty() Observable.just(1) Observable.from(1, 2, 3) Observable.range(1, 4400) Observable.timer(5, TimeUnit.SECONDS) Observable.create(...) A C
  43. 43. Flux d’évènements Observable<json> json json json λ ϕ φ Observable<Integer> Observable<Click>
  44. 44. Observer Push des données
  45. 45. Observer OnNext* (OnCompleted|OnError)?
  46. 46. Observer OnNext* (OnCompleted|OnError)?
  47. 47. Observer OnNext* (OnCompleted|OnError)?
  48. 48. Observer OnNext* (OnCompleted|OnError)?
  49. 49. Observer Observable.range(1, 4400)
  50. 50. Observer Observable.range(1, 4400).subscribe()
  51. 51. Observer Observable.range(1, 4400).subscribe(onNext)
  52. 52. Observer Observable.range(1, 4400).subscribe(System.out::println)
  53. 53. Observer Observable.range(1, 4400).subscribe(System.out::println, onError)
  54. 54. Observer Observable.range(1, 4400).subscribe(System.out::println, System.err::println)
  55. 55. Observer Observable.range(1, 4400).subscribe(System.out::println, System.err::println, onCompleted)
  56. 56. Observer Observable.range(1, 4400).subscribe(System.out::println, System.err::println, () -> System.out.println(“finished”))
  57. 57. Manipulation d’évènements Quand l’écoute ne suffit plus
  58. 58. Manipulation d’évènements 0 1
  59. 59. Manipulation d’évènements 0 1 map
  60. 60. Manipulation d’évènements 0 1 map A B
  61. 61. Manipulation d’évènements 1 4
  62. 62. Manipulation d’évènements 1 4 filter ( x <= 2 )
  63. 63. Manipulation d’évènements 1 4 1 filter ( x <= 2 )
  64. 64. Manipulation d’évènements 1 2
  65. 65. Manipulation d’évènements 1 2 delay ( 5, SECONDS )
  66. 66. Manipulation d’évènements 1 2 delay ( 5, SECONDS ) 1 2
  67. 67. Manipulation d’évènements Observable.from(0, 1, 2, 3, 4) Observable<Integer>
  68. 68. Manipulation d’évènements Observable.from(0, 1, 2, 3, 4) .filter((i) -> i < 3) Observable<Integer> Observable<Integer>
  69. 69. 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>
  70. 70. 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>
  71. 71. 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>
  72. 72. 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>
  73. 73. Manipulation d’évènements
  74. 74. Manipulation d’évènements
  75. 75. Manipulation d’évènements
  76. 76. Composition Création d’un flux à partir d’un ensemble de flux
  77. 77. Composition
  78. 78. Composition
  79. 79. Composition
  80. 80. Mise en pratique de composition Learn from the trenches
  81. 81. Composition
  82. 82. Composition
  83. 83. Composition
  84. 84. Composition
  85. 85. Composition
  86. 86. Composition merge
  87. 87. Composition merge
  88. 88. Composition
  89. 89. Composition
  90. 90. Composition zip
  91. 91. Composition zip
  92. 92. Subscription Et gestion de l’unsubscribe
  93. 93. Subscription Observable.create(new OnSubscribe<T>() { … });
  94. 94. Subscription Observable.create(new OnSubscribe<T>() { @Override public void call(Subscriber<? super T> s) { s.onNext(123); s.onCompleted(); } });
  95. 95. Subscription Observable.create(new OnSubscribe<T>() { @Override public void call(Subscriber<? super T> s) { s.onNext(123); s.onCompleted(); } }); Émission synchronne
  96. 96. 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); } }); } });
  97. 97. 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); } }); } });
  98. 98. 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); } }); } });
  99. 99. 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
  100. 100. 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
  101. 101. Subscription La souscription permet d’uniformiser des API différentes par leurs natures (Callback, etc...)
  102. 102. Unsubscribe Subscription subscription = Observable.interval(1, TimeUnit.SECONDS).subscribe();
  103. 103. Unsubscribe Souscription Subscription subscription = Observable.interval(1, TimeUnit.SECONDS).subscribe();
  104. 104. Unsubscribe Handler sur la souscription Subscription subscription = Observable.interval(1, TimeUnit.SECONDS).subscribe();
  105. 105. Unsubscribe Subscription subscription = Observable.interval(1, TimeUnit.SECONDS).subscribe(); /* ... */ subscription.unsubscribe();
  106. 106. Unsubscribe Subscription subscription = Observable.interval(1, TimeUnit.SECONDS).subscribe(); /* ... */ subscription.unsubscribe(); Arrêt de la souscription
  107. 107. Schedulers
  108. 108. Schedulers observableReadingSynchronously(“strings.txt”) .take(10) .delay(1, SECONDS) .map(parse()) .map(n -> “=> ” + n)) .subscribe(to_the_view())
  109. 109. Schedulers main
  110. 110. Schedulers observableReadingSynchronously(“strings.txt”) .take(10) .delay(1, SECONDS) .map(parse()) .map(n -> “=> ” + n)) .subscribe(to_the_view())
  111. 111. Schedulers main IO Computation UI
  112. 112. Schedulers main IO Computation UI
  113. 113. Schedulers main IO Computation UI
  114. 114. Schedulers main IO Computation UI
  115. 115. Schedulers main IO Computation UI
  116. 116. Schedulers main IO Computation UI
  117. 117. Schedulers observableReadingSynchronously(“strings.txt”) .subscribeOn(scheduler) .take(10) .delay(1, SECONDS, scheduler) .map(parse()) .observeOn(scheduler) .map(n -> “=> ” + n)) .subscribe(to_the_view())
  118. 118. Schedulers observableReadingSynchronously(“strings.txt”) .subscribeOn(Schedulers.io()) .take(10) .delay(1, SECONDS, Schedulers.computation()) .map(parse()) .observeOn(Schedulers.from(uiExecutor())) .map(n -> “=> ” + n)) .subscribe(to_the_view())
  119. 119. Schedulers Computation UI
  120. 120. Schedulers Computation UI
  121. 121. Schedulers Computation UI
  122. 122. Schedulers Computation UI
  123. 123. Schedulers Computation UI
  124. 124. Hot & Cold Observable
  125. 125. Cold Observable Observable<Integer> obs = Observable.from(1, 2, 3, 4); obs.subscribe(System.out::println); obs.subscribe(System.out::println);
  126. 126. Cold Observable Observable<Integer> obs = Observable.from(1, 2, 3, 4); obs.subscribe(System.out::println); obs.subscribe(System.out::println);
  127. 127. Cold Observable Observable<Integer> obs = Observable.from(1, 2, 3, 4); obs.subscribe(System.out::println); Subscribe obs.subscribe(System.out::println);
  128. 128. Cold Observable Observable<Integer> obs = Observable.from(1, 2, 3, 4); obs.subscribe(System.out::println); obs.subscribe(System.out::println); Subscribe
  129. 129. Cold Observable Observable<Integer> obs = Observable.from(1, 2, 3, 4); obs.subscribe(System.out::println); obs.subscribe(System.out::println); Souscription différente
  130. 130. Hot Observable ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); obs.subscribe(System.out::println); obs.subscribe(System.out::println); obs.connect();
  131. 131. Hot Observable ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); obs.subscribe(System.out::println); obs.subscribe(System.out::println); obs.connect();
  132. 132. Hot Observable ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); obs.subscribe(System.out::println); obs.subscribe(System.out::println); obs.connect();
  133. 133. Hot Observable ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); obs.subscribe(System.out::println); obs.subscribe(System.out::println); obs.connect();
  134. 134. Hot Observable ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); obs.subscribe(System.out::println); obs.subscribe(System.out::println); obs.connect();
  135. 135. Hot Observable ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); obs.subscribe(System.out::println); obs.subscribe(System.out::println); Subscribe obs.connect();
  136. 136. Hot Observable ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); obs.subscribe(System.out::println); obs.subscribe(System.out::println); obs.connect(); Partage la même souscription
  137. 137. Hot Observable ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); obs.subscribe(System.out::println); obs.subscribe(System.out::println); obs.connect();
  138. 138. Hot Observable ConnectableObservable<Integer> obs = Observable.from(1, 2, 3, 4).publish(); obs.connect(); Souscription obs.subscribe(System.out::println); obs.subscribe(System.out::println);
  139. 139. Cold & Hot Observable Cold Observable = flux passif Hot Observable = flux actif
  140. 140. Back Pressure Contrôler la production
  141. 141. Back Pressure Production
  142. 142. Back Pressure Production buffer
  143. 143. Back Pressure Production Pression arrière
  144. 144. Back Pressure Demande de production de n élements Production Pression arrière
  145. 145. Retour d’expérience
  146. 146. Opérateurs Observable.from(1, 2, 3, 4) .reduce(new LinkedList<Integer>(), (seed, value) -> { seed.add(value); return seed; }) .first() .subscribe(System.out::println);
  147. 147. Opérateurs Observable.from(1, 2, 3, 4) .reduce(new LinkedList<Integer>(), (seed, value) -> { seed.add(value); return seed; }) .first() .subscribe(System.out::println);
  148. 148. Opérateurs Observable.from(1, 2, 3, 4) .toList() .first() .subscribe(System.out::println);
  149. 149. Opérateurs Observable.from(1, 2, 3, 4) .toList() .first() .subscribe(System.out::println);
  150. 150. Opérateurs Observable.from(1, 2, 3, 4) .toList() .first() .subscribe(System.out::println); Différence entre first() / single() / take(1) ?
  151. 151. Gestion des erreurs Observable.from(1, 2, 3, 4) .toList() .single() .subscribe(System.out::println);
  152. 152. Gestion des erreurs Observable.from(1, 2, 3, 4) .toList() .single() .subscribe(System.out::println); Notification des erreurs absentes
  153. 153. Gestion des erreurs static { RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() { @Override public void handleError(Throwable e) { e.printStackTrace(); } }); }
  154. 154. 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); } });
  155. 155. 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
  156. 156. Lambda & Java 8 Observable.from(1, 2, 3, 4) .filter(i -> i > 3) .subscribe(System.out::println);
  157. 157. 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
  158. 158. Concurrence obs1.mergeWith(obs2) .take(5) .subscribe(System.out::println);
  159. 159. Concurrence obs1.mergeWith(obs2) .take(5) .subscribe(System.out::println);
  160. 160. Concurrence obs1.mergeWith(obs2) Doit gérer la concurrence .take(5) .subscribe(System.out::println);
  161. 161. Concurrence obs1.mergeWith(obs2) .take(5) .subscribe(System.out::println);
  162. 162. Concurrence obs1.mergeWith(obs2) .take(5) .subscribe(System.out::println); non concurrence
  163. 163. Sync & Async Observable<Integer> generator();
  164. 164. Sync & Async Observable<Integer> generator(); Sync ?
  165. 165. Sync & Async Sync ? Async ? Observable<Integer> generator();
  166. 166. Sync & Async Sync ? Async ? Observable<Integer> generator(); Computation ?
  167. 167. Sync & Async Sync ? Async ? Observable<Integer> generator(); Computation ? Acteur ?
  168. 168. “Leak” des Observables everything is an event
  169. 169. “Leak” des Observables ViewPager
  170. 170. “Leak” des Observables ViewPager Observable Preferences
  171. 171. “Leak” des Observables ViewPager Observable Preferences Observable UserService
  172. 172. Future<RxJava>
  173. 173. Future<RxJava> Support RxJava
  174. 174. Reactive Streams
  175. 175. 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/
  176. 176. Reactive Streams RxJava | Akka Streams | Reactor Composable | Ratpack
  177. 177. Reactive Streams RxJava | Akka Streams | Reactor Composable | Ratpack > 700 Ko | Java | Android
  178. 178. Reactive Streams RxJava | Akka Streams | Reactor Composable | Ratpack > 2.5 Mo | Scala | Akka
  179. 179. Reactive Streams RxJava | Akka Streams | Reactor Composable | Ratpack ~1Mo | Java | RingBufferDispatcher
  180. 180. Reactive Streams RxJava | Akka Streams | Reactor Composable | Ratpack Java 8 | Http
  181. 181. Si il ne fallait retenir qu’une chose
  182. 182. Si il ne fallait retenir qu’une chose RxJava est un modèle de programmation pour écrire des applications asynchrones Frontend | Backend | Crossplatform
  183. 183. Questions ? david.wursteisen@soat.fr @dwursteisen

×