Book Image

Reactive Programming With Java 9

By : Tejaswini Mandar Jog
Book Image

Reactive Programming With Java 9

By: Tejaswini Mandar Jog

Overview of this book

<p>Reactive programming is an asynchronous programming model that helps you tackle the essential complexity that comes with writing such applications.</p> <p>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.</p> <p>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.</p>
Table of Contents (12 chapters)
Free Chapter
1
Introduction to Reactive Programming

The test() operator

The RxJava library now has a test() operator which enables internal testing. The operator test() returns an instance of TestSubscriber or TestObserver. We can use the test() operator as shown next:

    public void testOperator() { 
 
      TestSubscriber<Long> test_Subscriber = 
        Flowable.rangeLong(10, 5).test(); 
      TestObserver<Integer> testObserver = 
         Observable.just(12, 89, 67).test(); 
      TestObserver<String> testObserver2 = Single.just("hello").test(); 
      TestObserver<String> testObserver3 = Maybe.just("Mango").test(); 
    } 

The test() operator provides a convenient way to obtain an instance of the TestObserver and TestSubscriber. Now we can assert the obtained result using the instance of the consumers. Let's use it to test Observer as shown by the following code:

    @Test...