Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams
()
About this ebook
Get an easy introduction to reactive streams in Java to handle concurrency, data streams, and the propagation of change in today's applications. This compact book includes in-depth introductions to RxJava, Akka Streams, and Reactor, and integrates the latest related features from Java 9 and 11, as well as reactive streams programming with the Android SDK.
Reactive Streams in Java explains how to manage the exchange of stream data across an asynchronous boundary—passing elements on to another thread or thread-pool—while ensuring that the receiving side is not forced to buffer arbitrary amounts of data which can reduce application efficiency. After reading and using this book, you'll be proficient in programming reactive streams for Java in order to optimize application performance, and improve memory management and data exchanges.
What You Will Learn
- Discover reactive streamsand how to use them
- Work with the latest features in Java 9 and Java 11
- Apply reactive streams using RxJava
- Program using Akka Streams
- Carry out reactive streams programming in Android
Who This Book Is For
Experienced Java programmers.
Read more from Adam L. Davis
Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript Rating: 0 out of 5 stars0 ratingsLearning Groovy 3: Java-Based Dynamic Scripting Rating: 0 out of 5 stars0 ratingsSpring Quick Reference Guide: A Pocket Handbook for Spring Framework, Spring Boot, and More Rating: 0 out of 5 stars0 ratingsBeginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 Rating: 0 out of 5 stars0 ratings
Related to Reactive Streams in Java
Related ebooks
Rapid Java Persistence and Microservices: Persistence Made Easy Using Java EE8, JPA and Spring Rating: 0 out of 5 stars0 ratingsJava: Programming: Your Step by Step Guide to Easily Learn Java in 7 Days Rating: 4 out of 5 stars4/5Scala for Java Developers Rating: 5 out of 5 stars5/5Beginning Scala Rating: 3 out of 5 stars3/5Building React Apps with Server-Side Rendering: Use React, Redux, and Next to Build Full Server-Side Rendering Applications Rating: 0 out of 5 stars0 ratingsJAVA 9.0 To 13.0 New Features: Learn, Implement and Migrate to New Version of Java. Rating: 0 out of 5 stars0 ratingsLearning Reactive Programming with Java 8 Rating: 0 out of 5 stars0 ratingsJavaScript Frameworks for Modern Web Development: The Essential Frameworks, Libraries, and Tools to Learn Right Now Rating: 0 out of 5 stars0 ratingsJavaScript Next: Your Complete Guide to the New Features Introduced in JavaScript, Starting from ES6 to ES9 Rating: 0 out of 5 stars0 ratingsR2DBC Revealed: Reactive Relational Database Connectivity for Java and JVM Programmers Rating: 0 out of 5 stars0 ratingsJAVA for Beginner's Crash Course: Java for Beginners Guide to Program Java, jQuery, & Java Programming Rating: 4 out of 5 stars4/5Code with Java 21: A practical approach for building robust and efficient applications (English Edition) Rating: 0 out of 5 stars0 ratingsJava for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5(Part 1) Absolute Beginner: Java 4 Selenium WebDriver: Come Learn How To Program For Automation Testing Rating: 5 out of 5 stars5/5Introducing Vala Programming: A Language and Techniques to Boost Productivity Rating: 0 out of 5 stars0 ratingsBeginning Java EE 7 Rating: 4 out of 5 stars4/5JAVA: Java Programming for beginners teaching you basic to advanced JAVA programming skills! Rating: 0 out of 5 stars0 ratingsHacking with Spring Boot 2.3: Reactive Edition Rating: 3 out of 5 stars3/5DWR Java AJAX Applications Rating: 0 out of 5 stars0 ratingsJava Persistence with NoSQL: Revolutionize your Java apps with NoSQL integration (English Edition) Rating: 0 out of 5 stars0 ratingsBeginning Jakarta EE: Enterprise Edition for Java: From Novice to Professional Rating: 0 out of 5 stars0 ratingsClojure for Java Developers Rating: 0 out of 5 stars0 ratingsLearning Concurrent Programming in Scala - Second Edition Rating: 0 out of 5 stars0 ratingsGroovy Programming: An Introduction for Java Developers Rating: 5 out of 5 stars5/5Practical Enterprise React: Become an Effective React Developer in Your Team Rating: 0 out of 5 stars0 ratingsLearn Java for Android Development: Migrating Java SE Programming Skills to Mobile Development Rating: 0 out of 5 stars0 ratingsJava Programming Rating: 0 out of 5 stars0 ratingsFrom Zero to Java Hero: Master the Art of Java Programming Rating: 0 out of 5 stars0 ratings
Programming For You
Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsGrokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Hacking: Ultimate Beginner's Guide for Computer Hacking in 2018 and Beyond: Hacking in 2018, #1 Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5The Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsPYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratingsTeach Yourself C++ Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Web Designer's Idea Book, Volume 4: Inspiration from the Best Web Design Trends, Themes and Styles Rating: 4 out of 5 stars4/5The Little SAS Book: A Primer, Sixth Edition Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Pokemon Go: Guide + 20 Tips and Tricks You Must Read Hints, Tricks, Tips, Secrets, Android, iOS Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5
Reviews for Reactive Streams in Java
0 ratings0 reviews
Book preview
Reactive Streams in Java - Adam L. Davis
© 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. Davis¹
(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
Subscriber
Processor
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.
© Adam L. Davis 2019
Adam L. DavisReactive Streams in Javahttps://doi.org/10.1007/978-1-4842-4176-9_2
2. Existing Models of Concurrency in Java
Adam L. Davis¹
(1)
Oviedo, FL, USA
As multicore processors become more and more standard, different models of concurrent programming have become more popular in Java. Although the core model of concurrency in Java is the Thread, multiple levels of abstraction have been built to enable simpler development.
Each of these models has a different approach toward protecting values from being modified by more than one thread at one time as we will cover in this chapter.
Prominent Models for Concurrency
There are several tried and true models of concurrency in Java and the JVM. Over time, higher level models have been introduced to make concurrency simpler. Some of these models are the following:
Synchronize and suffer (using synchronize keyword in Java)
Futures and the ExecutorService
Software transactional memory (STM) (Clojure)
Actor-based model (Akka)
Reactive Streams (RxJava, Reactor, etc.)
Synchronize in Java
The original style of concurrent programming in Java involves using the synchronized keyword whenever shared resources are modified. The runtime behavior of this style of programming is very unpredictable and difficult to test. You must deal with the following problems:
No warnings or errors are given at compile time.
Deadlocks can occur if you’re not careful.
It’s very difficult to make sure you’ve done everything right, and errors can occur randomly.
In conclusion, the synchronize keyword is too low level to use (just don’t use it!).¹
Java Futures
You may have heard of the java.util.concurrent.Future interface in Java. Maybe you’ve even used it. This interface was added in Java 1.5, and it holds the result of an asynchronous computation. It contains methods to check if the asynchronous computation is complete or still in progress, to wait for the completion of the computation, to block the call until the completion of the computation (with optional timeout), and to retrieve the result of the computation.
Drawbacks of the Future Interface
There are tons of problems with this interface:
When using Java’s Future, we tend to loop on isDone(), which ties up the thread, or call get() which blocks the thread