Hands-On Reactive Programming with Reactor
eBook - ePub

Hands-On Reactive Programming with Reactor

Build reactive and scalable microservices using the Reactor framework

  1. 250 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On Reactive Programming with Reactor

Build reactive and scalable microservices using the Reactor framework

About this book

Discover how project Reactor enhances the reactive programming paradigm and allows you to build scalable asynchronous applications

Key Features

  • Use reactive APIs, Flux, and Mono to implement reactive extensions
  • Create concurrent applications without the complexity of Java's concurrent API
  • Understand techniques to implement event-driven and reactive applications

Book Description

Reactor is an implementation of the Java 9 Reactive Streams specification, an API for asynchronous data processing. This specification is based on a reactive programming paradigm, enabling developers to build enterprise-grade, robust applications with reduced complexity and in less time. Hands-On Reactive Programming with Reactor shows you how Reactor works, as well as how to use it to develop reactive applications in Java.

The book begins with the fundamentals of Reactor and the role it plays in building effective applications. You will learn how to build fully non-blocking applications and will later be guided by the Publisher and Subscriber APIs. You will gain an understanding how to use two reactive composable APIs, Flux and Mono, which are used extensively to implement Reactive Extensions. All of these components are combined using various operations to build a complete solution.

In addition to this, you will get to grips with the Flow API and understand backpressure in order to control overruns. You will also study the use of Spring WebFlux, an extension of the Reactor framework for building microservices.

By the end of the book, you will have gained enough confidence to build reactive and scalable microservices.

What you will learn

  • Explore benefits of the Reactive paradigm and the Reactive Streams API
  • Discover the impact of Flux and Mono implications in Reactor
  • Expand and repeat data in stream processing
  • Get to grips with various types of processors and choose the best one
  • Understand how to map errors to make corrections easier
  • Create robust tests using testing utilities offered by Reactor
  • Find the best way to schedule the execution of code

Who this book is for

If you're looking to develop event- and data-driven applications easily with Reactor, this book is for you. Sound knowledge of Java fundamentals is necessary to understand the concepts covered in the book.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Data and Stream Processing

In the previous chapter, we generated streams of data by using a Reactor Flux and then consumed it in a subscriber. Reactor also provides a diverse set of operators that can be used to manipulate data. These operators take a stream as input and then generate another stream of another type of data. In a nutshell, these operators provide a powerful way to compose readable data pipelines. There are various operators for filtering, mapping, and collecting data. All of them will be covered in this chapter.
This chapter will cover the following topics:
  • Filtering data
  • Converting data

Technical requirements

  • Java Standard Edition, JDK 8 or above
  • IntelliJ IDEA IDE, 2018.1 or above
The GitHub link for this chapter is https://github.com/PacktPublishing/Hands-On-Reactive-Programming-with-Reactor/tree/master/Chapter03.

Generating data

Before we jump into working with various operators, let's first generate a stream of data. In order to do this, let's revisit our Fibonacci series from Chapter 1, Getting Started with Reactive Streams.
In number theory, Fibonacci numbers are characterized by the fact that every number after the first two numbers is the sum of the two preceding ones (that is, 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 ,21 , 34 , 55 , 89 , 144, and so on).
The Flux generated API enables us to build a generator. These generators start the series from 0 and 1. All numbers are printed to the console by a subscriber, which listens to all of the generated events. This is shown in the following code:
Flux<Long> fibonacciGenerator = Flux.generate(() -> Tuples.<Long, Long>of(0L, 1L),(state, sink) -> {
if (state.getT1() < 0)
sink.complete();
else
sink.next(state.getT1());
return Tuples.of(state.getT2(), state.getT1() + state.getT2());
});
fibonacciGenerator.subscribe(t -> {
System.out.println(t);
});
Let's recap what is happening here, as follows:
  • We create the Fibonacci series as Flux<Long> by using the Flux.generate() call. The API has a state and sink.
  • The API takes a seed as Tuple [0 , 1]. It then emits the first argument of the pair by using the Sink.next() call.
  • The API also generates the next Fibonacci number, by aggregating the pair.
  • The publisher marks the stream as complete when we generate negative numbers. This is due to their being out of range of the long data type.
  • We subscribe to the published numbers, then print the received number to the console. This is shown in the following screenshot:

Filtering data

Let's start with the most simple operator for selecting data. There are different analogies of data filtration, as follows:
  • Select or reject data based on a given condition
  • Select or reject a subset of the generated data
The preceding information is depicted in the following diagram:

The filter() operator

The filter() operator enables selection of the data on the passed condition. The API takes a Boolean predicate, which is evaluated for every emitted value, in order to determine whether it is selected. Filtering is quite common. Let's suppose that we want to select dates based on a month range, or we want to select employee data based on employee IDs. In those cases, the Boolean predicate passed to the filter holds the selection logic. This can be quite flexible, and can be adapted to different needs.
Let's extend our Fibonacci generator to only select even numbers, as follows:
fibonacciGenerator.filter(a -> a%2 == 0).subscribe(t -> {  System.out.println(t); });
In the preceding code, the predicate performs a divisibility chec...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Packt Upsell
  4. Contributors
  5. Preface
  6. Getting Started with Reactive Streams
  7. The Publisher and Subscriber APIs in a Reactor
  8. Data and Stream Processing
  9. Processors
  10. SpringWebFlux for Microservices
  11. Dynamic Rendering
  12. Flow Control and Backpressure
  13. Handling Errors
  14. Execution Control
  15. Testing and Debugging
  16. Assessments
  17. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Hands-On Reactive Programming with Reactor by Rahul Sharma in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming. We have over one million books available in our catalogue for you to explore.