Reactive Programming - Implementations of the specification in Java
Reactive Programming - Implementations of the specification in Java
How does a programmer implement reactive programming in Java?
Reactive Streams specification
In Java, reactive programming is achieved through the use of Reactive Streams. Reactive Stream is a specification that defines a collection of interfaces and classes for building reactive applications. Thanks to this standard, several libraries and frameworks have been developed in Java to implement the Reactive Streams specification, including RxJava, Project Reactor, and Akka. These libraries provide developers with APIs for writing reactive code and creating reactive applications that are both scalable and responsive.
Reactive Streams is an initiative to provide a standard for asynchronous stream processing with non-blocking back pressure.
It is a design approach that uses asynchronous programming to manage real-time updates to content.
- https://www.reactive-streams.org/
- https://github.com/reactive-streams/reactive-streams-jvm/blob/v1.0.4/README.md
- https://github.com/reactive-streams/reactive-streams-jvm
Participants in the Reactive Streams Specification
Reactive Streams specification introduces four interfaces that should be used and overridden when creating a reactive stream.

Publisher interface
-
package org.reactivestreams; public interface Publisher<T> { void subscribe(Subscriber<? super T> var1); }
- Used to register the subscriber to the publisher.
- The subscribe method of this interface accepts the subscriber object and registers it.
-
Subscriber interface
-
package org.reactivestreams; public interface Subscriber<T> { void onSubscribe(Subscription var1); void onNext(T var1); void onError(Throwable var1); void onComplete(); }
- void onSubscribe(Subscription s) -
- Will be called by the publisher when subscribing to the Subscribe object.
- Invoked after calling Publisher.subscribe(Subscriber).
- void onNext(T t) -
- Will be called when the next data will be published to the subscriber.
- Data notification sent by the Publisher in response to requests to Subscription.request(long).
- void onError(java.lang.Throwable t) -
- Will be called when exceptions arise while publishing data to the subscriber
- Failed terminal state.
- void onComplete() -
- Will be called after the successful completion of data publishing to the subscriber
- Successful terminal state.
-
Subscription interface
-
package org.reactivestreams; public interface Subscription { void request(long var1); void cancel(); }
- The subscription object will be created when the user subscribes to the publisher.
- The subscription object will be passed to the subscriber object via the subscriber’s onSubscribe method.
- void request(long n) - No events will be sent by a Publisher until demand is signaled via this method.
- void cancel() - Request the Publisher to stop sending data and clean up resources.
-
Processor interface
-
package org.reactivestreams; public interface Processor<T, R> extends Subscriber<T>, Publisher<R> { }
- inherits the methods from both Publisher and Subscriber interfaces
-
Publisher Subscriber communication in Reactive Streams Specification
- We pass a
Subscriber instance
to thePublisher
using thePublisher's subscribe()
method - to get updates. - The
Publisher
gives aSubscription
object to theSubscriber
.void onSubscribe(Subscription s) - Invoked after calling Publisher.subscribe(Subscriber).
- The relationship between the
Publisher
and theSubscriber
is now established through theSubscription
object.- The
Subscriber
can talk to thePublisher
using theSubscription
object. - The
Subscriber
canrequest()
updates. - The
Subscriber
cancancel()
updates.
- The
- The
Subscriber
uses therequest()
method to get updates from thePublisher
. - The
Publisher
uses theonNext()
method of theSubscriber
to pass items to theSubscriber
. ThePublisher
will only give the requested number of items. Not more. - When the
Publisher
is done sending items to theSubscriber
or when there are no more items to be sent to theSubscriber
, thePublisher
will call theSubcriber's onComplete()
method.Publisher
will NOT send anything to theSubscriber
afteronComplete()
. After this, theSubscription
object will no longer work. - When there is an error in the
Publisher
while it is trying to send items to theSubscriber
, thePublisher
will call theSubcriber's onError()
method.Publisher
will NOT send anything to theSubscriber
afteronError()
. After this, theSubscription
object will no longer work.
Libraries and frameworks
All of these libraries have implementation of Java’s Reactive Streams specification interfaces.
- Akka streams
- Akka is a framework and runtime for building high-concurrency, distributed, fault-tolerant applications. Akka supports reactive programming by using the Actor model for concurrency,
- The Akka framework supports the development of reactive applications using a wide range of programming models, from functional to declarative and imperative programming. The Akka framework supports Java and Scala for building reactive applications.
- RxJava
- RxJava is a popular Reactive library for Android developers. It’s known as the “go-to” framework for making it easier to handle concurrency and asynchronous tasks that come with mobile programming. RxJava is an open-source Functional Reactive Programming (FRP) library for Android. It adds a layer of abstraction over threading to ease the implementation of complex concurrent behavior.
- This is a series of posts: https://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/
- https://www.baeldung.com/rx-java
- https://www.baeldung.com/rxjava-backpressure
- https://abhiappmobiledeveloper.medium.com/rxjava-operators-understanding-map-flatmap-switchmap-and-concatmap-d88633f83b32
- Reactive Programming - Project Reactor (support from Spring team)
- Spring Framework 5.0
- Spring Framework 5 uses Reactive streams as a way to communicate backpressure between asynchronous components and libraries in the framework. This is a specification that has been developed through industry collaboration and is also used in Java 9. The Spring Framework already supports Reactor internally, but now it’s using Reactor to extend it even more. For developers who are already familiar with Spring, it builds on Reactor and offers a similar programming model.