Reactive Programming With Java 9

Book description

This book will teach you how to build robust asynchronous and event-driven applications with ease.

About This Book

  • Learn about Java 9's Flow API, Reactive programming along with Kafka and Mockito, 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, with the help of Spring
  • Explore a wide variety of code examples to easily get used to all the features and tools provided by RxJava and Akka

Who This Book Is For

This book targets existing Java developers who want to understand Reactive programming and build responsive and resilient asynchronous applications using Reactive stream implementations.

What You Will Learn

  • Understand the Reactive Manifesto
  • Grasp the Reactive Streams types introduced in Java 9 in the form of the Flow API
  • Use RxJava, a Reactive Streams implementation, to build asynchronous applications
  • Build responsiveness and resilience into applications using RxJava operators
  • Demonstrate the usage of Hystrix, a latency and fault tolerance library from Netflix that uses RxJava
  • Implement Reactive web applications using Spring Framework 5 and RxJava

In Detail

Reactive programming is an asynchronous programming model that helps you tackle the essential complexity that comes with writing such applications.

Using Reactive programming to start building applications is not immediately intuitive to a developer who has been writing programs in the imperative paradigm. To tackle the essential complexity, Reactive programming uses declarative and functional paradigms to build programs. This book sets out to make the paradigm shift easy.

This book begins by explaining what Reactive programming is, the Reactive manifesto, and the Reactive Streams specifi cation. It uses Java 9 to introduce the declarative and functional paradigm, which is necessary to write programs in the Reactive style. It explains Java 9's Flow API, an adoption of the Reactive Streams specifi cation. From this point on, it focuses on RxJava 2.0, covering topics such as creating, transforming,fi ltering, combining, and testing Observables. It discusses how to use Java's popular framework, Spring, to build event-driven, Reactive applications. You will also learn how to implement resiliency patterns using Hystrix. By the end, you will be fully equipped with the tools and techniques needed to implement robust, event-driven, Reactive applications.

Style and approach

This book is a tutorial about Reactive programming in Java using APIs as well as the RxJava library. Packed with a lot of well-described examples, it explains Reactive programming concepts in plain and readable language.

Publisher resources

Download Example Code

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Introduction to Reactive Programming
    1. Asynchronous programming
      1. Concurrency
      2. Parallel programming
      3. Streams
        1. Features of Streams
          1. The sequential elements
          2. Source of a stream
          3. Performing operations
          4. Performing automatic operations
      4. Reactive Streams
        1. What do Reactive Streams address?
          1. Asynchronicity
          2. Back pressure
      5. Reactive Programming
      6. Reactive Manifesto
        1. Responsiveness
        2. Resilience
        3. Elastic
        4. Message-driven
        5. Scalable
        6. Benefits of Reactive Programming
      7. Reactive Extensions
        1. RxJava
          1. Advantages of RxJava
        2. Project Reactor
          1. The features of Project Reactor
        3. Akka Streams
          1. Actor
        4. Ratpack
          1. Aim of Ratpack
        5. Quasar
        6. MongoDB
        7. Slick
        8. Vert.x
          1. The components of Vert.x
      8. Reactive Streams in Java 9
        1. The Technology Compatibility Kit (TCK)
        2. API components
      9. Reactive in and across JVM
        1. ThreadPoolExecutor
        2. ScheduledThreadPoolExecutor
      10. The Fork/Join framework
    2. Summary
  3. Programming Paradigm Shift
    1. Programming paradigm
      1. Imperative programming
      2. Procedural programming
        1. Avoiding bugs
    2. Functional programming
      1. Need for functional programming
        1. Concurrency
      2. Functional programming in JVM
        1. Functional interface
      3. Lambda expressions
        1. Advantages of using lambda expressions
        2. Functional interfaces provided by JDK 8
          1. Method reference
          2. Streams API
        3. Characteristics of functional programming
    3. Declarative programming
      1. Characteristics of declarative programming
      2. Advantages of declarative programming
    4. Operator fusion
      1. Macro fusion
      2. Micro fusion
    5. Summary
  4. Reactive Streams
    1. Discussing the Collection framework
      1. Streams in action
      2. Delving into the Flow API
        1. The building blocks
          1. Publisher
          2. The SubmissionPublisher class
          3. Subscriber
        2. The Subscription interface
        3. The behavior of the Publisher-Subscriber system
        4. Processor
        5. The behavior of the Publisher-Processor-Subscriber system
      3. Designing nonblocking interfaces
    2. Understanding backpressure
    3. Summary
  5. Reactive Types in RxJava
    1. ReactiveX
    2. Observer design pattern
    3. The Iterator design pattern
      1. Advantages of ReactiveX
    4. RxJava
      1. Observable Streams
      2. Components of RxJava
        1. Observable
        2. Observer
        3. Subscriber
    5. Hot or cold Observables
    6. Creating Observable emitting sequential data
      1. Using the just() operator
      2. Using the defer() operator
      3. Using the empty() operator
      4. Using the never() operator
      5. Using the error() operator
      6. Using the create() operator
    7. Transforming Rx non-compatible sequences into Observables
      1. Conversion using the from() operator
    8. Creating Observables for unrestricted infinite length
      1. The interval() operator
      2. The range() operator
      3. The timer() operator
    9. Types of observables
      1. The Observable<T> operator
        1. Scenarios for using an Observable
      2. The Single<T> operator
      3. The Flowable<T> operator
        1. Scenarios to use Flowable
      4. The Maybe<T> operator
      5. Completable
    10. Understanding the Disposable interface
      1. Disposable Subscribers
        1. The DefaultSubscriber class
        2. DisposableSubscriber
        3. The ResourceSubscriber interface
        4. CompositeDisposable
    11. Subject
      1. Flavors of Subject
        1. The AsyncSubject class
        2. The BehaviorSubject class
        3. ReplaySubject
        4. PublishSubject
    12. Summary
  6. Operators
    1. Demystifying RxMarbles using an Observable
    2. Transforming Observables using operators
      1. Using the buffer() operator
      2. Using the flatMap() operator
      3. Using the groupBy() operator
      4. Using the map() operator
      5. Using the scan() operator
      6. Using the window() operator
    3. Filtering Observable
      1. Using the debounce() operator
      2. Using the distinct() operator
        1. Using the distinctUntilChanged() operator
      3. Using the elementAt() operator
      4. Using the filter() operator
      5. Using the first(), never(), and empty() operators
      6. Using the last() operator
      7. Using the ignoreElements() operator
      8. Using the sample() operator
      9. Using the skip() operator
      10. Using the skipLast() operator
      11. Using the take() operator
      12. Using the takeLast() operator
    4. Combining Observables
      1. Using the merge() operator for combining observables
      2. Using the combineLatest() operator
      3. Using the startWith() operator
      4. Using the and(), then(), and when() operators
      5. Using the switchIfEmpty() operator
      6. Using the zip() operator
      7. Using the join() operator
    5. Conditional operators
      1. Using the all() operator
      2. Using the amb() operator
      3. Using the contains() operator
      4. Using the defaultIfEmpty() operator
      5. Using the sequenceEqual() operator
      6. Using the takeWhile() operator
    6. The mathematical and aggregate operators
      1. Using the average() operator
      2. Using the concat() operator
      3. Using the count() operator
      4. Using the max() operator
      5. Using the min() operator
      6. Using the reduce() operator
      7. Using the sum() operator
    7. Summary
  7. Building Responsiveness
    1. Concurrency
      1. Comparing asynchronicity with concurrency
    2. Scheduling
      1. Scheduler
        1. The subscribeOn() operator
        2. The observeOn() operator
      2. Schedulers
        1. The computation() method
        2. The from() operator
        3. Th io() operator
        4. The single() operator
        5. The newThread() operator
        6. The trampoline() operator
      3. Operators and thread safety
        1. Operators are not thread safe
          1. The serialize() operator
    3. Latency
    4. Summary
  8. Building Resiliency
    1. Resilience
      1. Reliable applications
      2. Resiliency
    2. Error handling in RxJava
      1. Exploring the doOnError() operator
      2. Reacting to an error
        1. Recovering from an error
          1. Absorb the error and switch over to the backup Observable which allows continuing the sequence using the onErrorResumeNext() operator
          2. Absorb the error and emit a specified default item using the onErrorReturn() operator
          3. Absorb the error and then try to restart the Observable that has failed using the onExceptionResumeNext() operator
          4. Absorb the error and then try to restart the Observable which has failed using the retry() operator without delay
          5. Absorb the error and then try to restart the Observable that has failed using the retryWhen() operator with delay
    3. Exceptions specific to RxJava
      1. CompositeException
      2. MissingBackpressureException
      3. OnErrorNotImplementedException
      4. OnErrorFailedException
      5. OnErrorThrowable
    4. Error handler
      1. Handling specific undeliverable exceptions
      2. Exceptions introduced for tracking
      3. Exception wrapping
    5. Summary
  9. Testing
    1. Testing the need and role of a developer
    2. The traditional way of testing an Observable
    3. The modern approach for testing an Observable
      1. The BaseTestConsumer class
      2. The TestObserver class
      3. The TestSubscriber class
      4. Testing time-based Observable
        1. The TestScheduler class
    4. Testing the Subscriber
    5. The test() operator
    6. Testing notifications
      1. Demonstration 1 - updating code for the just() operator
      2. Demonstration 2 - updating the code for the never() operator
      3. Demonstration 3 - updating the code for Flowable
      4. Demonstration 4 - updating the code for the error() operator
      5. Demonstration 5 - updating the code for the interval() operator
    7. Mockito testing
    8. Summary
  10. Spring Reactive Web
    1. An introduction to Spring Framework 
      1. Plumbing code
        1. Scalability
        2. Boilerplate code
        3. Unit testing of the application
      2. Features of the Spring Framework
        1. POJO-based development
        2. Loose coupling through Dependency Injection (DI)
        3. Declarative programming
        4. Boilerplate code reduction using aspects and templates
    2. The Spring architecture
      1. Core modules
        1. Data access and integration modules
        2. Web MVC and remoting modules
        3. AOP modules
        4. Instrumentation modules
        5. Test modules
    3. Project Reactor
      1. Operators
    4. Spring web Reactive Programming
    5. Server-side support
      1. Annotation-based support
      2. The functional programming model
        1. HandlerFunction
          1. ServerRequest
          2. ServerResponse
        2. RouterFunction
    6. Client side
      1. Creating a WebClient instance
      2. Using the WebClient instance to prepare a request
      3. Reading the response
      4. Deploying the application
        1. Deploying the application from Eclipse IDE
        2. Creating and deploying WAR file on server
      5. Testing the application
        1. Working with WebTestClient
    7. Server-Sent Events (SSE)
    8. Summary
  11. Implementing Resiliency Patterns Using Hystrix
    1. Hystrix- an introduction
      1. How Hystrix works
      2. Demonstrating HystrixCommand
    2. Demonstrating HystrixObservableCommand
    3. Handling fallback
      1. Demonstrating HystrixCommand with a fallback
      2. Demonstrating HystrixObservableCommand with fallback
    4. Design patterns in resiliency
      1. Bulkhead pattern
        1. Partition the services
        2. Partition the resources
        3. Benefits
      2. Circuit breaker
        1. Proxy states
          1. Closed state
          2. Open state
          3. Half-Open state
      3. Retry pattern
        1. Retry
        2. Retrying after delay
        3. Cancel
    5. Queue-based load leveling pattern
    6. Patterns used by Hystrix
      1. Fail-fast
      2. Fail-silent
      3. Static fallback
      4. Stubbed fallback
      5. Cache via network fallback
      6. Dual-mode fallback
      7. Isolation
        1. Using threads and thread pools for isolation
          1. Reasons for using thread pools in Hystrix
          2. Benefits of using thread and thread pools
          3. Drawbacks of the thread pool
        2. Semaphores
    7. Request collapsing or request batching
      1. The working
    8. Request caching
      1. Difference between request collapsing and request caching
    9. Summary
  12. Reactive Data Access
    1. Spring Data
      1. Modules provided by Spring Data
      2. Features of Spring Data
    2. Spring Data repositories
      1. Using the repository
    3. Spring Data and Reactive Programming
      1. ReactiveCrudRepository
      2. RxJava2CrudRepository
      3. Spring Data Reactive and MongoDB
      4. Features added to Spring Data MongoDB 2.0
        1. Using MongoDB with Spring
          1. RecativeMongoTemplate
          2. Spring Data Repositories and MongoDB
      5. ReactiveMongoRepository
    4. Spring Data Reactive and Redis
      1. Connecting to Redis
        1. Jedis Connector
        2. Lettuce connector
      2. RedisTemplate
      3. Spring Data Repositories and Redis
    5. Kafka
      1. Reactive API for Kafka
        1. Reactor Kafka components for handling reactive pipelines
        2. Sender
          1. Creating the Sender
          2. Creating messages
          3. Sending messages
          4. Closing the Sender
        3. Receiver
          1. Creating a Receiver instance
          2. Reading the messages
    6. Running the Kafka application
    7. Summary

Product information

  • Title: Reactive Programming With Java 9
  • Author(s): Tejaswini Mandar Jog
  • Release date: September 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781787124233