© Adam L. Davis 2019
Adam L. DavisReactive Streams in Javahttps://doi.org/10.1007/978-1-4842-4176-9_1

1. Introduction to Reactive Streams

Adam L. Davis1 
(1)
Oviedo, FL, USA
 

Reactive Streams is an initiative to provide a standard for asynchronous stream processing with non-blocking back pressure. This encompasses efforts aimed at runtime environments (JVM and JavaScript) as well as network protocols.

reactive-streams.org

At their core, Reactive Streams are an effort to provide highly responsive applications able to handle many requests per second with the ability to manage backpressure (the ability to skip or queue data that is coming too fast to be processed). Asynchronous means processing can take place in many threads, without stopping to read data from a file or a web request for example. Although many implementations already exist for asynchronous processing, such as Java’s Future, CompletableFuture, and parallel streams, most of them do not have standard support for asynchronous handling of backpressure.

Reactive Streams are a unifying standard that abstracts existing methods of concurrency. Also, by having one standard, different Reactive Streams implementations can interoperate in one application.

Java 9+

Java 9 was an important release of Java and includes Project Jigsaw which represents a huge restructuring of the core JDK (Java Development Kit) as well as a new and improved way of defining code dependencies. This provides compile-time errors when dependencies are missing as opposed to runtime errors, which is a vast improvement for software development efficiency. Java 9 also introduced a unified interface for Reactive Streams.

Java 9 includes the following key features:
  • Language updates

  • Support for Reactive Streams

  • Modularity (Project Jigsaw)

  • Java REPL (jshell)

For the purposes of this book, we will focus on the second item and cover what Reactive Streams are and how they should be used. Although at the time of writing this is not the case, all implementations of Reactive Streams in Java are expected to implement the Java 9 API in the near future. We will cover changes in Java 10 and 11 in how they affect our code going forward.

Flow

Support for Reactive Streams has been added to the JDK. Several interfaces have been added in the java.util.concurrent.Flow class:
  • Publisher<T> : A producer of items (and related control messages) received by Subscribers

  • Subscriber<T> : A receiver of messages

  • Processor<T,R> : A component that acts as both a Subscriber and Publisher

  • Subscription : Message control linking a Publisher and Subscriber

No actual implementation is included in the JDK; however, several implementations already exist. Current notable implementations of the Reactive Streams specification on the Java virtual machine (JVM) are Project Reactor (which is integrated in Spring 5), Akka Streams , and RxJava , all of which we will cover in this book.

Code for This Book

The code examples used in this book are available on my repository on GitHub . Feel free to download this code which is open source and play around with it. If you do not already have a GitHub account, you can create one completely for free. It helps to have Git installed on your own machine. Then use the git clone command, as specified on the GitHub landing page, and use whatever Integrated Development Environment (IDE) you feel is compatible with you – even a text editor will do.