RxJava (ReactiveX Java)

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 combination of

1. Observer Pattern

2. Iterator Pattern

3. Functional Programming.

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 are no more value to be emitted,Subscriber class implements the Observer interface and provides some additional useful methods along 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);

1 thought on “RxJava (ReactiveX Java)

  • whoah this blog is excellent i really like reading your articles. Keep up the good work! You recognize, lots of individuals are searching round for this info, you can help them greatly.

Leave a Reply

Your email address will not be published. Required fields are marked *