Learning RxJava
eBook - ePub

Learning RxJava

Build concurrent applications using reactive programming with the latest features of RxJava 3, 2nd Edition

Nick Samoylov, Thomas Nield

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

Learning RxJava

Build concurrent applications using reactive programming with the latest features of RxJava 3, 2nd Edition

Nick Samoylov, Thomas Nield

Book details
Book preview
Table of contents
Citations

About This Book

Updated with the latest Maven coordinates, Java programming features, and API changes, this book is your guide to solving problems in writing asynchronous and event-based programs

Key Features

  • Explore a variety of tools and techniques used to solve problems in implementing concurrency and parallelization
  • Learn about core operators in RxJava that enable you to express your code logic productively
  • Apply RxJava with Kotlin to create responsive Android apps with better user experience

Book Description

RxJava is not just a popular library for building asynchronous and event-based applications; it also enables you to create a cleaner and more readable code base. In this book, you'll cover the core fundamentals of reactive programming and learn how to design and implement reactive libraries and applications.

Learning RxJava will help you understand how reactive programming works and guide you in writing your first example in reactive code. You'll get to grips with the workings of Observable and Subscriber, and see how they are used in different contexts using real-world use cases. The book will also take you through multicasting and caching to help prevent redundant work with multiple Observers. You'll then learn how to create your own RxJava operators by reusing reactive logic. As you advance, you'll explore effective tools and libraries to test and debug RxJava code. Finally, you'll delve into RxAndroid extensions and use Kotlin features to streamline your Android apps.

By the end of this book, you'll become proficient in writing reactive code in Java and Kotlin to build concurrent applications, including Android applications.

What you will learn

  • Discover different ways to create Observables, Observers, and Subscribers
  • Multicast in order to push data to multiple destinations and cache and replay them
  • Express RxJava idiomatically with the help of Kotlin features such as extension functions and data classes
  • Become familiar with various operators available in RxJava to perform common transformations and tasks
  • Explore RxJava's reactive types, including Flowable, Single, Maybe, and Completable
  • Demystify Observables and how they express data and events as sequences

Who this book is for

This book is for Java developers who want to leverage reactive programming to develop more resilient and concurrent applications. If you're an RxJava user looking to get to grips with the latest features and updates in RxJava 3, this book is for you. Fundamental knowledge of core Java features and object-oriented programming will assist you in understanding the key concepts covered in this book.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
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 here.
Is Learning RxJava an online PDF/ePUB?
Yes, you can access Learning RxJava by Nick Samoylov, Thomas Nield in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Java. We have over one million books available in our catalogue for you to explore.

Information

Year
2020
ISBN
9781789952216
Edition
2

Section 1: Foundations of Reactive Programming in Java

This module sets the stage for the book. It introduces reactive programming in general and quickly brings the reader up to speed on all the basic stream processing functionality.
The following chapters are included in this module:
  • Chapter 1, Thinking Reactively
  • Chapter 2, Observable and Observer
  • Chapter 3, Basic Operators

Thinking Reactively

We assume that you are fairly comfortable with Java and know how to use classes, interfaces, methods, properties, variables, static/non-static scopes, and collections. If you are not familiar with concurrency or multithreading, that is okay. RxJava makes these advanced topics much more accessible.
Have your favorite Java development environment ready, be it IntelliJ IDEA, Eclipse, NetBeans, or any other environment of your choosing. We will be using IntelliJ IDEA, although it should not matter or have an impact on the examples in this book. We recommend that you have a project building framework such as Gradle or Maven, which we will explain how to use shortly.
In this chapter, before diving deeper into RxJava, we will cover some core topics:
  • A brief history of Reactive Extensions and RxJava
  • Thinking reactively
  • Leveraging RxJava
  • Setting up your first RxJava project
  • Building your first reactive applications
  • The differences between RxJava 1.x, 2.x, and 3.0

A brief history of ReactiveX and RxJava

As developers, we tend to think in counterintuitive ways. Modeling our world with code has never been short of challenges. It was not long ago that object-oriented programming was seen as the silver bullet to solve this problem. Making blueprints of what we interact with in real life was a revolutionary idea, and this core concept of classes and objects still impacts how we code today. However, business and user demands continued to grow in complexity. As 2010 approached, it became clear that object-oriented programming solved only part of the problem.
Classes and objects do a great job of representing an entity with properties and methods, but they become messy when they need to interact with each other in increasingly complex and often unplanned ways. Decoupling patterns and paradigms emerged, but this yielded an unwanted side effect of growing amounts of boilerplate code. In response to these problems, functional programming began to make a comeback, not to replace object-oriented programming, but rather to complement it and address the challenges.
Reactive programming, a functional event-driven programming approach, began to receive special attention. A couple of reactive frameworks emerged, including Akka and Sodium. But at Microsoft, a computer scientist named Erik Meijer created a reactive programming framework for .NET called Reactive Extensions. In a matter of years, Reactive Extensions (also known as ReactiveX or Rx) was ported to several languages and platforms, including JavaScript, Python, C++, Swift, and Java, of course. Soon, ReactiveX became a cross-language standard of reactive programming.
RxJava, the ReactiveX port for Java, was created in large part by Ben Christensen from Netflix and David Karnok. RxJava 1.0 was released in November 2014, followed by RxJava 2.0 in November 2016. RxJava is the backbone for other ReactiveX JVM ports, such as RxScala, RxKotlin, and RxGroovy. It has become a core technology for Android development and has also found its way into Java backend development.
Many RxJava-supporting libraries, such as RxAndroid (https://github.com/ReactiveX/RxAndroid), RxJava-JDBC (https://github.com/davidmoten/rxjava-jdbc), RxNetty (https://github.com/ReactiveX/RxNetty), and RxJavaFX (https://github.com/ReactiveX/RxJavaFX) adapted several Java frameworks to become reactive and work with RxJava out of the box. This all shows that RxJava is more than a library. It is part of a greater ReactiveX ecosystem that represents an entire approach to programming. The fundamental idea behind ReactiveX is that events are data and data are events. This is a powerful concept that we will explore later in this chapter, but first, let's step back and look at the world through the reactive lens.

Thinking reactively

Suspend everything you know about Java (and programming in general) for a moment, and let's make some observations about our world. These may sound like obvious statements, but as developers, we can easily overlook them. Try to become aware of the fact that everything is in motion. Traffic, weather, people, conversations, financial transactions, and so on are constantly changing or moving through stages.
Technically, even something as stationary as a rock is in motion, as its spatial location changes constantly due to the earth's rotation and orbiting through space. When you consider the possibility that everything can be modeled as being in motion, you may find it a bit overwhelming as a developer.
Another observation to note is that these different events are happening concurrently. Multiple activities are happening at the same time. Sometimes, they act independently, but at other times, they can converge and interact. For instance, a car can drive with no impact on a person jogging. They are two separate streams of events. However, they may converge at some point and the car will stop when it encounters the jogger.
If this is how our world works, why do we not model our code this way? Why do we not model code as multiple concurrent streams of events or data? It is not uncommon for developers to spend more time managing the states of objects and doing this in an imperative and sequential manner. You may structure your code to execute two independent processes, Process 1 and Process 2, and then Process 3, which depends on Process 1 and Process 2. Why not kick off Process 1 and Process 2 simultaneously, and then kick off Process 3 after the completion of these two processes? Of course, you can use callbacks and Java concurrency tools, but RxJava makes this much easier and safer to express.
Let's make one last observation. A book or music CD is static. A book is an unchanging sequence of words and a CD is a collection of tracks. There is nothing dynamic about them. However, when we read a book, we read one word at a time. Those words are effectively put in motion as a stream being consumed by our eyes. It is no different with a music track on a CD, where each track is put in motion as sound waves and your ears consume each track. Static items can, in fact, be put in motion too. This is an abstract but powerful idea because we create from each of these static items a series of events. When we level the playing field between data and events by treating them both the same, we unleash the power of functional programming and unlock abilities we previously might have thought impractical.
The fundamental idea behind reactive programming is that events are data and data are events. This may not seem intuitive, but it really does not take long to grasp when you consider our real-world examples. The runner and car both have properties and states, but they are also in motion. The book and CD are put in motion when they are consumed. Merging the event and data allows the code to feel organic and represent the world we are modeling.

Why should I learn RxJava?

ReactiveX and RxJava address many problems that programmers face daily, allowing them to express business logic and spend less time engineering code. Have you ever struggled with concurrency, event handling, obsolete ...

Table of contents