Book Image

Learning Reactive Programming with Java 8

By : Nickolay Tzvetinov
Book Image

Learning Reactive Programming with Java 8

By: Nickolay Tzvetinov

Overview of this book

Table of Contents (15 chapters)
Learning Reactive Programming with Java 8
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Reactive programming has been around for decades. There has been a few implementations of reactive programming from the time Smalltalk was a young language. However, it has only become popular recently and it is now becoming a trend. Why now you ask? Because it is good for writing fast, real-time applications and current technologies and the Web demand this.

I got involved in it back in 2008, when the team I was part of was developing a multimedia book creator called Sophie 2. It had to be fast and responsive so we created a framework called Prolib, which provided objects with properties which could depend on each other (in other words, we implemented bindings for Swing and much more—transformations, filtering, and so on). It felt natural to wire the model data to the GUI like this.

Of course, this was far away from the functional-like approach that comes with RX. In 2010, Microsoft released RX and, after that, Netflix ported it to Java—RxJava. However, Netflix released RxJava to the open source community and the project became a huge success. Many other languages have their port of RX and many alternatives to it. Now, you can code using reactive programming on your Java backend and wire it to your RxJava's frontend.

This book tries to explain to you what reactive programming is all about and how to use it with RxJava. It has many small examples and it explains concepts and API details in small steps. After reading this book, you will have an idea of RxJava, functional programming, and the reactive paradigm.

What this book covers

Chapter 1, An Introduction to Reactive Programming, will introduce you to the concept of reactive programming and will tell you why you should learn about it. This chapter contains examples that demonstrate how RxJava incorporates the reactive programming concept.

Chapter 2, Using the Functional Constructions of Java 8, will teach you how to use the new lambda constructions of Java 8. It will explain some functional programming concepts and will show you how to use them with RxJava in your reactive programs.

Chapter 3, Creating and Connecting Observables, Observers, and Subjects, will show you the basic building blocks of the RxJava library called the Observables. You will learn the difference between 'hot' and 'cold' Observables and how to subscribe to and unsubscribe from them using a subscription instance.

Chapter 4, Transforming, Filtering, and Accumulating Your Data, will walk you through the basic reactive operators, which you will learn how to use to achieve step-by-step computations. This chapter will give you an idea of how to transform the events the Observables emit, how to filter only the data we need, and how to group, accumulate, and process it.

Chapter 5, Combinators, Conditionals, and Error Handling, will present you with more complex reactive operators, which will allow you to master observable chaining. You will learn about the combining and conditional operators and how the Observables interact with each other. This chapter demonstrates the different approaches to error handling.

Chapter 6, Using Concurrency and Parallelism with Schedulers, will guide you through the process of writing concurrent and parallel programs with RxJava. This will be accomplished by the RxJava Schedulers. The types of Schedulers will be introduced and you will come to know when and why to use each one of them. This chapter will present you with a mechanism that will show you how to avoid and apply backpressure.

Chapter 7, Testing Your RxJava Application, will show you how to unit test your RxJava applications.

Chapter 8, Resource Management and Extending RxJava, will teach you how to manage the resources used as data sources by your RxJava applications. We will write our own Observable operators here.

What you need for this book

In order to run the examples, you will need:

Who this book is for

If you are a Java developer who knows how to write software and would like to learn how to apply your existing skills to reactive programming, this book is for you.

This book can be helpful to anybody no matter if they are beginners, advanced programmers, or even experts. You don't need to have any experience with either Java 8's lambdas and streams or with RxJava.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

Observable
  .just('R', 'x', 'J', 'a', 'v', 'a')
  .subscribe(
    System.out::print,
    System.err::println,
    System.out::println
  );

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

Observable<Object> obs = Observable
  .interval(40L, TimeUnit.MILLISECONDS)
  .switchMap(v ->
    Observable
      .timer(0L, 10L, TimeUnit.MILLISECONDS)
      .map(u -> "Observable <" + (v + 1) + "> : " + (v + u)))
  );
subscribePrint(obs, "switchMap");

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Interfaces of this type are called functional interfaces."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.