Reactive Programming with RxJava

Book description

In today’s app-driven era, when programs are asynchronous and responsiveness is so vital, reactive programming can help you write code that’s more reliable, easier to scale, and better-performing. With this practical book, Java developers will first learn how to view problems in the reactive way, and then build programs that leverage the best features of this exciting new programming paradigm.

Authors Tomasz Nurkiewicz and Ben Christensen include concrete examples that use the RxJava library to solve real-world performance issues on Android devices as well as the server. You’ll learn how RxJava leverages parallelism and concurrency to help you solve today’s problems. This book also provides a preview of the upcoming 2.0 release.

  • Write programs that react to multiple asynchronous sources of input without descending into "callback hell"
  • Get to that aha! moment when you understand how to solve problems in the reactive way
  • Cope with Observables that produce data too quickly to be consumed
  • Explore strategies to debug and to test programs written in the reactive style
  • Efficiently exploit parallelism and concurrency in your programs
  • Learn about the transition to RxJava version 2

Publisher resources

View/Submit Errata

Table of contents

  1. Foreword
  2. Introduction
    1. Who Should Read This Book
    2. Note from Ben Christensen
    3. Note from Tomasz Nurkiewicz
    4. Navigating This Book
    5. Online Resources
    6. Conventions Used in This Book
    7. Safari® Books Online
    8. How to Contact Us
    9. Acknowledgments
      1. From Ben
      2. From Tomasz
  3. 1. Reactive Programming with RxJava
    1. Reactive Programming and RxJava
    2. When You Need Reactive Programming
    3. How RxJava Works
      1. Push versus Pull
      2. Async versus Sync
      3. Concurrency and Parallelism
      4. Lazy versus Eager
      5. Duality
      6. Cardinality
    4. Mechanical Sympathy: Blocking versus Nonblocking I/O
    5. Reactive Abstraction
  4. 2. Reactive Extensions
    1. Anatomy of rx.Observable
    2. Subscribing to Notifications from Observable
      1. Capturing All Notifications by Using Observer<T>
    3. Controlling Listeners by Using Subscription and Subscriber<T>
    4. Creating Observables
      1. Mastering Observable.create()
      2. Infinite Streams
      3. Timing: timer() and interval()
      4. Hot and Cold Observables
    5. Use Case: From Callback API to Observable Stream
      1. Manually Managing Subscribers
    6. rx.subjects.Subject
    7. ConnectableObservable
      1. Single Subscription with publish().refCount()
      2. ConnectableObservable Lifecycle
    8. Summary
  5. 3. Operators and Transformations
    1. Core Operators: Mapping and Filtering
      1. 1-to-1 Transformations Using map()
      2. Wrapping Up Using flatMap()
      3. Postponing Events Using the delay() Operator
      4. Order of Events After flatMap()
      5. Preserving Order Using concatMap()
    2. More Than One Observable
      1. Treating Several Observables as One Using merge()
      2. Pairwise Composing Using zip() and zipWith()
      3. When Streams Are Not Synchronized with One Another: combineLatest(), withLatestFrom(), and amb()
    3. Advanced Operators: collect(), reduce(), scan(), distinct(), and groupBy()
      1. Scanning Through the Sequence with Scan and Reduce
      2. Reduction with Mutable Accumulator: collect()
      3. Asserting Observable Has Exactly One Item Using single()
      4. Dropping Duplicates Using distinct() and distinctUntilChanged()
    4. Slicing and Dicing Using skip(), takeWhile(), and Others
      1. Ways of Combining Streams: concat(), merge(), and switchOnNext()
      2. Criteria-Based Splitting of Stream Using groupBy()
      3. Where to Go from Here?
    5. Writing Customer Operators
      1. Reusing Operators Using compose()
      2. Implementing Advanced Operators Using lift()
    6. Summary
  6. 4. Applying Reactive Programming to Existing Applications
    1. From Collections to Observables
    2. BlockingObservable: Exiting the Reactive World
    3. Embracing Laziness
    4. Composing Observables
      1. Lazy paging and concatenation
    5. Imperative Concurrency
    6. flatMap() as Asynchronous Chaining Operator
    7. Replacing Callbacks with Streams
    8. Polling Periodically for Changes
    9. Multithreading in RxJava
      1. What Is a Scheduler?
      2. Declarative Subscription with subscribeOn()
      3. subscribeOn() Concurrency and Behavior
      4. Batching Requests Using groupBy()
      5. Declarative Concurrency with observeOn()
      6. Other Uses for Schedulers
    10. Summary
  7. 5. Reactive from Top to Bottom
    1. Beating the C10k Problem
      1. Traditional Thread-Based HTTP Servers
      2. Nonblocking HTTP Server with Netty and RxNetty
      3. Benchmarking Blocking versus Reactive Server
      4. Reactive HTTP Servers Tour
    2. HTTP Client Code
      1. Nonblocking HTTP Client with RxNetty
    3. Relational Database Access
      1. NOTIFY AND LISTEN on PostgreSQL Case Study
    4. CompletableFuture and Streams
      1. A Short Introduction to CompletableFuture
      2. Interoperability with CompletableFuture
    5. Observable versus Single
      1. Creating and Consuming Single
      2. Combining Responses Using zip, merge, and concat
      3. Interoperability with Observable and CompletableFuture
      4. When to Use Single?
    6. Summary
  8. 6. Flow Control and Backpressure
    1. Flow Control
      1. Taking Periodic Samples and Throttling
      2. Buffering Events to a List
      3. Moving window
      4. Skipping Stale Events by Using debounce()
    2. Backpressure
      1. Backpressure in RxJava
      2. Built-in Backpressure
      3. Producers and Missing Backpressure
      4. Honoring the Requested Amount of Data
    3. Summary
  9. 7. Testing and Troubleshooting
    1. Error Handling
      1. Where Are My Exceptions?
      2. Declarative try-catch Replacement
      3. Timing Out When Events Do Not Occur
      4. Retrying After Failures
    2. Testing and Debugging
      1. Virtual Time
      2. Schedulers in Unit Testing
    3. Unit Testing
    4. Monitoring and Debugging
      1. doOn…() Callbacks
      2. Measuring and Monitoring
    5. Summary
  10. 8. Case Studies
    1. Android Development with RxJava
      1. Avoiding Memory Leaks in Activities
      2. Retrofit with Native RxJava Support
      3. Schedulers in Android
      4. UI Events as Streams
    2. Managing Failures with Hystrix
      1. The First Steps with Hystrix
      2. Nonblocking Commands with HystrixObservableCommand
      3. Bulkhead Pattern and Fail-Fast
      4. Batching and Collapsing Commands
      5. Monitoring and Dashboards
    3. Querying NoSQL Databases
      1. Couchbase Client API
      2. MongoDB Client API
    4. Camel Integration
      1. Consuming Files with Camel
      2. Receiving Messages from Kafka
    5. Java 8 Streams and CompletableFuture
      1. Usefulness of Parallel Streams
      2. Choosing the Appropriate Concurrency Abstraction
      3. When to Choose Observable?
    6. Memory Consumption and Leaks
      1. Operators Consuming Uncontrolled Amounts of Memory
    7. Summary
  11. 9. Future Directions
    1. Reactive Streams
    2. Observable and Flowable
    3. Performance
    4. Migration
  12. A. More HTTP Server Examples
    1. fork() Procedure in C Language
    2. Thread per Connection
    3. Thread Pool of Connections
  13. B. A Decision Tree of Observable Operators
  14. Index

Product information

  • Title: Reactive Programming with RxJava
  • Author(s): Tomasz Nurkiewicz, Ben Christensen
  • Release date: October 2016
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491931608