Introduction

Who Should Read This Book

Reactive Programming with RxJava targets intermediate and advanced Java programmers. You should be fairly comfortable with Java; however, prior knowledge of reactive programming is not required. Many concepts in this book relate to functional programming, but you do not need to be familiar with it either. There are two distinct groups of programmers that can benefit from this book:

  • Craftsmen who seek improved performance on the server or more maintainable code on mobile devices. If you fall into this category, you will find ideas and solutions to real problems as well as practical advice. In this case, RxJava is just another tool that this book will help to master.

  • Curious developers who’ve heard about reactive programming or RxJava in particular and want to get a fair understanding of it. If this is you, and if you are not planning to take advantage of RxJava in production code, you will significantly broaden your horizons.

Additionally, if you are a hands-on software architect, this book will likely help you. RxJava influences the overall architecture of entire systems, so it is worth knowing. But even if you are just beginning your adventure with programming, try to go through the first few chapters, which explain the basics. Underlying concepts like transformations and composition are quite universal and not related to reactive programming.

Note from Ben Christensen

In 2012, I was working on a new architecture for the Netflix API. Along the way it became clear that we needed to embrace concurrency and asynchronous network requests to achieve our goals. During the process of exploring approaches, I ran into Jafar Husain who tried to sell me on an approach he learned while at Microsoft called “Rx.” At that time, I was pretty comfortable with concurrency but still thought about it imperatively, and very much in a Java-centric manner, as Java has been my predominant breadwinner, and thus where I have spent the most time.

So, as Jafar tried selling me on the approach, it was difficult to grasp the concepts due to their functional programming style, and I pushed back on it. Months of arguments and discussions followed as the overall system architecture continued maturing, during which Jafar and I continued our many whiteboard sessions until I grasped the theoretical principles and subsequently the elegance and power of what Reactive Extensions could offer.

We decided to embrace the Rx programming model in the Netflix API and ultimately created the Java implementation of Reactive Extensions called RxJava, following the naming convention started by Microsoft with Rx.Net and RxJS.

In the roughly three years that I worked on RxJava, most of it done in the open on GitHub, I had the privilege to work with a growing community and 120-plus contributors to turn RxJava into a mature product used in many production systems both server and client side. It has succeeded enough to get more than 15,000 stars on GitHub, making it one of the top 200 projects, and third highest of projects using Java.

George Campbell, Aaron Tull, and Matt Jacobs at Netflix were essential in maturing RxJava from early builds to what it became, including the addition of lift, Subscriber, backpressure, and JVM-polyglot support. Dávid Karnok became involved in the project and has now surpassed me in commits and lines of code. He has been a significant factor in the project’s success and has now taken over as the project lead.

I have to thank Erik Meijer who created Rx while he was at Microsoft. Since he left that company, I had the opportunity to collaborate with him at Netflix on RxJava, and now I’m lucky enough to be working directly with him at Facebook. I consider it a real honor to be able to spend so many hours at a whiteboard with him discussing and learning. It makes a real difference having a mentor like Erik to level-up one’s thinking.

Along the way I also got to speak at many conferences about RxJava and reactive programming, and through that process met many people who have helped me learn far more about code and architecture than I would have ever done on my own.

Netflix was phenomenal in supporting my time and efforts on the project as well as providing support for technical documentation that I could have never written by myself. Open source of this maturity and scope does not succeed without being able to do it during your “day job” and with the involvement of many people with different skillsets.

The first chapter is my attempt at introducing why reactive programming is a useful programming approach, and how RxJava in particular provides a concrete implementation of those principles.

The rest of this book is written by Tomasz, who has done an amazing job. I had the opportunity to review and provide suggestions, but this is his book, and he will teach the details from Chapter 2 onward.

Note from Tomasz Nurkiewicz

I first came across RxJava around 2013 while working for a financial institution. We were dealing with large streams of market data processed in real-time. By then, the data pipeline consisted of Kafka delivering messages, Akka processing trades, Clojure transforming data, and a custom-built language for propagating changes throughout the system. RxJava was a very compelling choice because it had a uniform API that worked very well for different sources of data.

Over time, I tried reactive programming in more scenarios for which scalability and throughput were essential. Implementing systems in a reactive fashion is definitely more demanding. But the benefits are far more important, including better hardware utilization and thus energy savings. To fully appreciate the advantages of this programming model, developers must have relatively easy-to-use tooling. We believe that Reactive Extensions are in the sweet spot between abstraction, complexity, and performance.

This book covers RxJava 1.1.6, unless stated otherwise. Even though RxJava supports Java 6 and later, almost all of the examples use lambda syntax from Java 8. Some examples in the chapter in which we discuss Android (Chapter 8) show how to deal with verbose syntax prior to lambda expressions. That being said, we do not always use the shortest possible syntax (like method references) to improve readability where it makes sense.

Navigating This Book

The book was structured such that you will derive the most from it by reading it from cover to cover. If you can not afford that much time, however, feel free to cherry pick only the parts that are most interesting. If there is a concept that was introduced earlier, you will most likely find a back reference to it. Following is an overview of each chapter:

  • Chapter 1 very briefly goes through the inception of RxJava, basic concepts, and ideas (Ben).

  • Chapter 2 explains how RxJava can appear in your application and how to interact with it. This chapter is very basic, but understanding concepts like hot versus cold sources is tremendously important (Tomasz).

  • Chapter 3 is a whirlwind tour of the many operators provided by RxJava. We will introduce you to the expressive and powerful functions that are a foundation of this library (Tomasz).

  • Chapter 4 is more practical, showing how to embed RxJava in various places across your codebase. It also touches on concurrency (Tomasz).

  • Chapter 5 is advanced, explaining how to implement reactive applications from top to bottom (Tomasz).

  • Chapter 6 explains an important problem of flow control and backpressure mechanisms in RxJava that support it (Tomasz).

  • Chapter 7 shows techniques of unit testing, maintaining, and troubleshooting Rx-based applications (Tomasz).

  • Chapter 8 shows a few selected applications of RxJava, especially in distributed systems (Tomasz).

  • Chapter 9 highlights future plans for RxJava 2.x (Ben).

Online Resources

All marble diagrams throughout this book are taken from official RxJava documentation published under Apache License Version 2.0.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This icon signifies a tip, suggestion, or general note.

Note

This icon signifies a general note.

Warning

This icon indicates a warning or caution.

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals.

Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/reactive-prog-with-rxjava.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

From Ben

This book wouldn’t exist without Tomasz, who wrote most of it, and Nan Barber, our editor who was incredibly helpful and patient in getting us to the end. Thank you Tomasz for responding to my message on Twitter looking for an author and making this book a reality!

I also am very appreciative of the support Netflix Open Source and Daniel Jacobson provided to me personally, and the project generally over the years. They were great sponsors of the project and the immense amount of time I put into the community. Thank you!

And thank you Erik for creating Rx, teaching me so much, and taking the time to write the foreword to this book.

From Tomasz

First and foremost, I would like to thank my parents who gave me my first computer almost 20 years ago (486DX2 with 8 MB of RAM: you never forget that). This is how my journey with programming began. Several people contributed to the making of this book. Beginning with Ben who agreed to write the first and last chapters as well as review my content.

Speaking of reviewers, Venkat Subramaniam put a lot of effort into structuring this book in a meaningful and consistent way. He often suggested a different order of sentences, paragraphs, and chapters, or even removing entire pages of irrelevant content. Our other reviewer was the extremely knowledgable and experienced Dávid Karnok. Being the project lead of RxJava, he spotted dozens of bugs, race conditions, inconsistencies, and other problems. Both reviewers provided hundreds of comments that significantly improved the quality of this book. In the very early stages of this book, many of my colleagues read the manuscript and gave very valuable feedback, as well. I would like to thank: Dariusz Baciński, Szymon Homa, Piotr Pietrzak, Jakub Pilimon, Adam Wojszczyk, Marcin Zajączkowski, and Maciej Ziarko.

Get Reactive Programming with RxJava now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.