Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Going Reactive with RxJava EXTENDED - @ jPrime ...

Going Reactive with RxJava EXTENDED - @ jPrime Sofia 2016

Reactive programming is here to stay, so you might as well learn it!
It already made its mark on languages like Swift or C#, and now with major companies such as Netflix and Microsoft backing it up, it's time to do the same in Java. In this lecture we'll talk about RxJava library and how it can be used to make our applications more Reactive.
We'll peak into RxJava API a little bit, but more importantly, we'll focus on the concepts and the ideas that drive RxJava, and the value it adds to our applications.
Once we understand that, RxJava will be yet another great tool in our toolbox.

Hrvoje Crnjak

May 26, 2016
Tweet

More Decks by Hrvoje Crnjak

Other Decks in Programming

Transcript

  1. • Observable.from • Observable.just • • Observable.interval , Observable.range ,

    Observable.empty Observable<Long> intervalObservable = Observable.interval(500L, TimeUnit.MILLISECODS); Observable<Integer> rangeObservable = Observable.range(1, 10); Observable<Character> justObservable = Observable.just('R', 'x', 'J', 'a', 'v', 'a'); List<String> list = Arrays.asList("blue", "red", "green", "yellow", "orange"); Observable<String> listObservable = Observable.from(list)
  2. • Observable.create public static Observable<SomeDataType> getData(String someParameter) { return Observable.create(subscriber

    -> { try { SomeDataType result = SomeService.getData(someParameter); subscriber.onNext(result); subscriber.onCompleted(); } catch (Exception e) { subscriber.onError(e); } }); }
  3. • Observable.create public static Observable<SomeDataType> getData(String someParameter) { return Observable.create(subscriber

    -> { try { SomeDataType result = SomeService.getData(someParameter); subscriber.onNext(result); subscriber.onCompleted(); } catch (Exception e) { subscriber.onError(e); } }); }
  4. • observableInstance.subscribe observableInstance.subscribe(new Observer<SomeDataType>() { @Override public void onNext(SomeDataType message)

    { // Do something on each Message received } @Override public void onError(Throwable error) { // Do something on Error } @Override public void onCompleted() { // Do something when Observable completes } });
  5. • filter(Func1<T, Boolean> predicate) • skip(int num) • take(int num)

    • elementAt(int index) • distinct() • ... observableInstance.filter(element -> element < 10) observableInstance.elementAt(5) observableInstance.take(5) observableInstance.skip(5)
  6. • throttleFirst(long duration, TimeUnit unit) • throttleLast(long duration, TimeUnit unit)

    • timeout(long duration, TimeUnit unit) • ... observableInstance.throttleFirst(100, TimeUnit.MILLISECONDS) observableInstance.timeout(100, TimeUnit.MILLISECONDS) observableInstance.throttleLast(100, TimeUnit.MILLISECONDS)
  7. • map(Func1<T,R> func) • flatMap(Func1<T,Observable<R>> func) • ... observableInstance.map(number ->

    number * number) observableInstance.flatMap(line -> Observable.from(line.split(“\\.”)))
  8. • zip(Iterable<Observable> seq, FuncN<R> func) • merge(Iterable<Observable> seq) • amb(Iterable<Observable>

    seq) • ... Observable<String> timedObservable = Observable.zip( Observable.from(Arrays.asList("Z", "I", "P", "P")), Observable.interval(300L, TimeUnit.MILLISECONDS), (value, i) -> value); Observable<String> mergedObservable = Observable .merge(firstObservable, secondObservable, thirdObservable); Observable<String> fasterObservable = Observable .amb(firstObservable, secondObservable);
  9. • • • onErrorReturn , onErrorResumeNext, retry mainObservable.onErrorReturn(throwable -> {

    System.out.println("The original feed failed with" + throwable); return oneMoreMessage; }).subscribe(data -> {/* doSomething */}); mainObservable.onErrorResumeNext(throwable -> { System.out.println("The original feed failed with" + throwable); return backupObservable; }).subscribe(data -> {/* doSomething */});
  10. • Observable • subscribeOn(Scheduler) • observeOn(Scheduler) • Schedulers • immediate

    – • newThread – • trampoline – • io – • computation –
  11. • Subscription subscription = observableInstance.subscribe( (Long message) -> {/*onNext*/}, (Throwable

    error) -> {/*onError*/}, () -> {/*onCompleted*/}); // Do some logic; subscription.unsubscribe();
  12. • observableInstance.subscribe(new Subscriber<Long>() { @Override public void onNext(Long message) {

    // Do something on each Message received unsubscribe(); } @Override public void onError(Throwable e) { // Do something on Error } @Override public void onCompleted() { // Do something when Observable completes } });