RxJava (ReactiveX Java)
In our previous article, we told you about Reactive programming, that explained Reactive Programming and Reactive Extensions (ReactiveX or Rx). This article will throw some light RxJava & its components
What is RxJava?
RxJava is a Java VM implementation of Reactive Extensions: “a library for composing asynchronous(like memory leaks and concurrency limitations) and event-based programs by using observable sequences”
ReactiveX is a combination of
Netflix has created (Feb 2013) open-source Reactive Extensions for Java. It has been designed to be used from other JVM languages as well(Groovy, Clojure, Scala, and JRuby).
Netflix created RxJava to simplify server-side concurrency. Their goal was to allow the client to invoke a single “heavy” client request that is executed in parallel on the server.
RxJava supports Java 6 or higher and JVM-based languages such as Groovy, Clojure, JRuby, Kotlin and Scala.
RxJava for Gradle:
compile "io.reactivex.rxjava2:rxjava:2.1.14"
RxJava for Maven:
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.1.14</version>
</dependency>
Rxjava Main Components: basic building blocks of RxJava.
1. Observable: a class that emits a stream of data or events, Observable will emit a data only when it has at least one Subscriber, it has many static methods called operators. Operators create observable object & modify data or it will convert into Observable Object. Any Observable can have multiple Operators.
Observable<String> sampleObservable = Observable.just("Hello RxJava");
2. Subscriber (Observer): a class that watches observable by subscribing to them. Subscriber(Observer) is notified when an Observable emits a value or when an error occurred or when there is no more value to be emitted, Subscriber class implements the Observer interface and provides some additional useful methods along with with the default onNext(), onCompleted() and onError() methods. Some of them are onStart(), isUnsubscribed(), unsubscribe().
Subscriber<String> sampleSubscriber = new Subscriber() {
@Override
public void onCompleted() {
System.out.println("Emitting Complete!");
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String value) {
System.out.println("onNext method: " + value);
}
}
sampleObservable.subscribe(sampleSubscriber);
Hello there, You’ve done a great job. I will definitely digg it and personally recommend
to my friends. I am sure they’ll be benefited from
this web site.
I’d like to find out more? I’d care to find out some additional information.
I’m extremely impressed with your writing skills and also with the layout on your blog.
Is this a paid theme or did you customize
it yourself? Either way keep up the nice quality writing, it’s rare to see a great blog like this one these
days.