Return to site

Reactive APIs of Java 9

by Venkat Subramaniam, President, Agile Developer, Inc.

· video,mashup,java

What's Reactive programming

Reactive programming has been used for an extremely long time.
Reactive programming brings four things together:
1) Elastic: ability to scale
2) Message-driven: no central database driving. "Don't expose your database, instead expose your data"
3) Responsive
4) Resilient (fail fast and gracefully)

RP is Data flow

Reactive programming is an improved kind of functional programming.
In FP, we do function composition & lazy evaluation; and RP is built on top of these two concepts.
RP is dataflow computing say a stream of data.

Java 8 Streams vs Reactive Streams
  • Pipeline: in Java 8 Stream & in reactive streams too.
  • Lazy: Java 8 streams are lazy and reactive streams are lazy too.
  • Push of data: Java 8 is push of "data only", reactive stream is push of "data also". It means that while Java 8 Streams have only one channel: the data channel; Reactive streams have 3 channels: data/errors/completion. The data channel is in charge of getting data assuming there are no errors, the completion channel will eventually send a complete status complete which makes the data channel to close. An if there is an error the data channel close and you get the error.
  • Java 8 streams have only one pipeline, you can't fork it. Reactive streams can have multiple subscribers say it can fork.
  • Java 8 streams are in a "push at will" mode, while reactive streams are in a "backpressure" mode say it adapts to the subscribers.
  • In Java 8 streams, we speak of sequential vs parallel streams. While in reactive streams we speak about sync vs async streams.
There are many java reactive tools:
Reactive Streams API
  • Involved stakeholders: Eclipse foundation, Pivotal, Netflix, Lightbend...
  • Publisher: publishes/emits data (Flowable), it is a data source of potentially unbounded stream of data
  • Subscriber: subscibres/receives data (subscribe), it is a receiver of data from a publisher
  • Subscription:  kind of session with the publisher, so you can have multiple relationships (sessions) with the publisher if you want, it is like a session between a publisher and a subscriber
  • Processors: both publisher and subscribers (map/filter), it is the processing stage: acts as both publisher and subsriber
Backpressure
  • Backpressure it is when you have an observable which emits items so fast that consumer can't keep up with the flow leading to the existence of emitted but unconsumed items.
  • There are different backpressure strategies: BUFFER (keep all), DROP(keep the available), LATEST(keep the last)
Reactive Stream in Java SE

A standardization of reactive stream have been done near a decade after the first tools appearance.
It consists of the java.util.concurrent.Flow classes and its 4 inner interfaces: Publisher, Subscriber, Processor and Subscription.
https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/Flow.html
It only defines the interfaces, you have to use the existing tools: RxJava, AKKA, Reactor... to build your application.