Chapter 1. An Introduction to Reactive Programming

Nowadays, the term reactive programming is trending. Libraries and frameworks in various programming languages are emerging. Blog posts, articles and presentations about reactive programming are being created. Big companies, such as Facebook, SoundCloud, Microsoft, and Netflix, are supporting and using this concept. So we, as programmers, are starting to wonder about it. Why are people so excited about reactive programming? What does it mean to be reactive? Would it be helpful in our projects? Should we learn how to use it?

Meanwhile, Java is popular with its multi-threading, speed, reliability, and good portability. It is used for building a wide variety of applications, from search engines, through databases to complex web applications running on server clusters. But Java has bad reputation too—it is very hard to write both concurrent and simple applications using only the built-in tools, and programming in Java requires writing a lot of boilerplate code. Also, if you need to be asynchronous (using futures, for example), you can easily get into "callback hell", which actually holds true for all programming languages.

In other words, Java is powerful and you can create great applications with it, but it won't be easy. The good news is that there is a way to change that, using the reactive style of programming.

This book will present RxJava (https://github.com/ReactiveX/RxJava), an open source Java implementation of the reactive programming paradigm. Writing code using RxJava requires a different kind of thinking, but it will give you the power to create complex logic using simple pieces of well-structured code.

In this chapter, we will cover:

  • What reactive programming is
  • Reasons to learn and use this style of programming
  • Setting up RxJava and comparing it with familiar patterns and structures
  • A simple example with RxJava

What is reactive programming?

Reactive programming is a paradigm that revolves around the propagation of change. In other words, if a program propagates all the changes that modify its data to all the interested parties (users, other programs, components, and subparts), then this program can be called reactive.

A simple example of this is Microsoft Excel. If you set a number in cell A1 and another number in cell 'B1', and set cell 'C1' to SUM(A1, B1); whenever 'A1' or 'B1' changes, 'C1' will be updated to be their sum.

Let's call this the reactive sum.

What is the difference between assigning a simple variable c to be equal to the sum of the a and b variables and the reactive sum approach?

In a normal Java program, when we change 'a' or 'b', we will have to update 'c' ourselves. In other words, the change in the flow of the data represented by 'a' and 'b', is not propagated to 'c'. Here is this illustrated through source code:

int a = 4;
int b = 5;
int c = a + b;
System.out.println(c); // 9

a = 6;
System.out.println(c);
// 9 again, but if 'c' was tracking the changes of 'a' and 'b',
// it would've been 6 + 5 = 11

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

This is a very simple explanation of what "being reactive" means. Of course, there are various implementations of this idea and there are various problems that these implementations must solve.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.18.101