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

Going Reactive with RxJava - @ TopConf Linz 2016

Going Reactive with RxJava - @ TopConf Linz 2016

Reactive programming is here to stay, so you might as well learn it!

It has already become a favourite tool in the toolboxes of many Swift or C# developers, and now with major companies such as Netflix and Microsoft backing it up, it is time to make it's mark on Java.

In this lecture we'll talk about RxJava library and how it can be used together with all the new nifty features from Java 8.

But more importantly, we'll try to get to the bottom of what it actually means to use the concepts of Reactive programming in our applications.
Once we understand that, RxJava will be yet another great tool in our toolbox.

Hrvoje Crnjak

February 02, 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. • 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 } });
  4. • • • • observableInstance.filter(element -> element < 10) observableInstance.timeout(100,

    TimeUnit.MILLISECONDS) observableInstance.map(number -> number * number) Observable<String> mergedObservable = Observable .merge(firstObservable, secondObservable, thirdObservable);
  5. • • • onError , 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 */});
  6. • Observable • subscribeOn(Scheduler) • observeOn(Scheduler) • Schedulers • immediate

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

    error) -> {/*onError*/}, () -> {/*onCompleted*/}); // Do some logic; subscription.unsubscribe();
  8. • 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 } });