Learning Reactive Programming With Java 8
Format: PDF / Kindle (mobi) / ePub
Learn how to use RxJava and its reactive Observables to build fast, concurrent, and powerful applications through detailed examples
About This Book
- Learn about Java 8's lambdas and what reactive programming is all about, and how these aspects are utilized by RxJava
- Build fast and concurrent applications with ease, without the complexity of Java's concurrent API and shared states
- Explore a wide variety of code examples to easily get used to all the features and tools provided by RxJava
Who This Book Is For
If you are a Java developer that knows how to write software and would like to learn how to apply your existing skills to reactive programming, this book is for you.
What You Will Learn
- Discover what reactive programming is and how you can benefit from it
- Get to grips with the new functional features of Java 8 and some functional theory
- Create RxJava Observable instances from virtually any data source
- Transform, filter, and accumulate your data using various RxJava operators
- Combine multiple data sources in one dataset, using custom logic
- Write concurrent programs with ease, scheduling actions on various workers
- Learn about unit testing asynchronous RxJava logic
- Extend RxJava by writing your own operators and factory methods
Whether you are a Java expert or at a beginner level, you'll benefit from this book, because it will teach you a brand new way of coding and thinking.
The book starts with an explanation of what reactive programming is, why it is so appealing, and how we can integrate it in to Java. It continues by introducing the new Java 8 syntax features, such as lambdas and function references, and some functional programming basics. From this point on, the book focuses on RxJava in depth. It goes through creating Observables, transforming, filtering, and combining them, and concurrency and testing to finish with extending the library itself.
This book is a definite tutorial in RxJava filled with a lot of well-described examples. It explains reactive programming concepts in plain and readable language, without scientific formulas and terms.
com/meddle0x53/learning-rxjava/blob/master/ src/main/java/com/packtpub/reactive/chapter02/ PureAndHigherOrderFunctions.java. Functions like these can be used to implement different behaviors that have something in common. In object-oriented programming we define classes and then extend them, overloading their methods. In functional programming, we define higher order functions as interfaces and call them with different parameters, resulting in different behaviors. These functions are first-class
parameter(s) as OnNext notifications, and after that, it emits an OnCompleted notification. For example, one letter: Observable.just('S').subscribe(System.out::println); Or a sequence of letters: Observable .just('R', 'x', 'J', 'a', 'v', 'a') .subscribe( [ 33 ] Creating and Connecting Observables, Observers, and Subjects System.out::print, System.err::println, System.out::println ); The first piece of code prints S and a new line, and the second prints the letters on a single line and adds a
error together with the name; and on OnCompleted, it prints 'ended!' prefixed with the name. This helps us debug the results. The source code of the preceding method can be found at https:// github.com/meddle0x53/learning-rxjava/blob/4a2598aa 0835235e6ef3bc3371a3c19896161628/src/main/java/com/ packtpub/reactive/common/Helpers.java#L25. Here is the code introducing the new factory methods: subscribePrint( Observable.interval(500L, TimeUnit.MILLISECONDS), "Interval Observable" ); subscribePrint(
RxNewThreadScheduler-1|Source: -->22 RxNewThreadScheduler-1|Source: --->| RxCachedThreadScheduler-1|+48 : >68 RxCachedThreadScheduler-1|+48 : ->69 RxCachedThreadScheduler-1|+48 : -->70 RxComputationThreadPool-3|Chars : RxCachedThreadScheduler-1|+48 : >D --->| RxComputationThreadPool-3|Chars : ->E RxComputationThreadPool-3|Chars : -->F RxComputationThreadPool-3|Chars : --->| We can see that the chain passes through three threads. If we do this with more elements, some of the code