January 17, 2015

Reactive Jersey Client using Java 8, RxJava and Netflix Hystrix

In this post I will show you how to use Jersey Client Reactive API, it's RxJava Observable support together with Netflix Hystrix, latency and fault tolerance library. All sources are placed in GitHub jersey--examples--rx-client-java8-webapp project.

Jersey framework contains Reactive Client API. Reactive Jersey Client API is a generic API allowing end users to utilize the popular reactive programming model when using Jersey Client. Reactive programming is very popular these days. It helps you to implement asynchronous code with solved callback hell issue.


Jersey supports RxJava ObservableJava 8 CompletionStage and CompletableFutureGuava ListenableFuture and FuturesJSR-166e CompletableFuture. It also provides SPI to be extended by custom reactive libraries.

There is excellent 3 part blog post series written by Michal Gajdoš about Reactive Jersey Client:
  1. Reactive Jersey Client – Motivation
  2. Reactive Jersey Client – Usage and Supported Reactive Libraries
  3. Reactive Jersey Client – Customization (SPI)

Jersey source code contains also examples for supported reactive libraries, see modules:


Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.

You usually use Hystrix to isolate dependent system call into HystrixCommand class. It allows to be executed synchronously, asynchronously (returns java.util.concurrent.Future) and also asynchronously via RxJava Observable.


In case dependent system already supports RxJava Observable API you can use specific HystrixObservableCommand class. And thanks to Jersey Reactive Client API and its RxJava support it allows us to write resilient JAX-RS client code.

private class ForecastCommand extends HystrixObservableCommand<Forecast> {
    private final Destination destination;
    protected Observable<Forecast> run() {
        return RxObservable.from(forecastWebTarget)
                .resolveTemplate("destination", destination.getDestination())
    protected Observable<Forecast> getFallback() {
        return Observable.just(new Forecast(destination.getDestination(), "N/A"));

The code above creates new Hystrix RxJava Observable ready Command that invokes remote REST API using Jersey Reactive Client API. Whenever API call fails or times out a getFallback() method is invoked. And in this case we return Forecast instance with N/A forecast value.

Whole example web application enhanced by code using Hystrix is placed into my GitHub jersey--examples--rx-client-java8-webapp repository. Shown and little but simplified code snipped is from HystrixObservableAgentResource class. The example module is clone of rx-client-java8-webapp.

To run the example:
    mvn clean package jetty:run

At beginning of HystrixObservableAgentResource class there are TIMEOUT_* constants. You can play with values and you will see that sometimes you don't get all domain objects' values and sometimes you see default values (N/A for Forecast and -1 for Calculation). You can also play with Thread.sleep intervals but because of Hystrix timeout support the overall JAX-RS resource method processing time is never (too much) longer than TIMEOUT_OVERALL (currently 900 ms).

You can also check unit test RxClientsTest. Processing time is expected between 850 and 950 ms!



  1. Ace4sure is the website that deals in preparation material for the exam for many years. According to my exposure and research, this is the right platform where you can get exact 70-486 Dumps Questions.

  2. This is the first & best article to make me satisfied by presenting good content. I feel so happy and delighted.By Learn Digital Marketing Course Training in Chennai it will help to get Digital Marketing Training with Placement Institute in Chennai. If you Learn Social Media Marketing Training with Placement Institute in Chennai, you will get job soon.

    Learn Best Digital Marketing Course Training in Chennai in professional institute to get reputed job.

  3. When I originally commented I seem to have clicked the -Notify me when new comments are added- checkbox and from now on each time a comment is added I recieve 4 emails with the exact same comment. theme Perhaps there is an easy method you are able to remove me from that service? Thanks a lot!

  4. There's definately loads to apprehend approximately this problem. I certainly like all the factors you made guide.

  5. Thanks for Sharing a Very Informative Post & I read Your Article & I must say that is very helpful post for us. Primavera P6 Certification Training in Chennai | Primavera Training in India

  6. Thanks for sharing a wonderful post.

    Looking for website development white label website buildersitegalore has the best website developers in India.

  7. I got too much interesting stuff on your blog. I guess I am not the only one having all the enjoyment here! Keep up the good work.
    havanese dogs for sale
    havanese puppies for sale

  8. Totally loved your article. Looking forward to see more more from you. Meanwhile feel free to surf through my website while i give your blog a read.
    welcome to Newsome frenchies
    where to buy french bulldog puppies
    frenchies bulldog puppies
    in Australia

    Nice & Informative Blog ! We offer welcome to Mila bulldogs. Check it out!...
    puppies for sale near me
    British bulldog puppies for sale
    where to buy

  9. Thanks for sharing this article. It is very helpful for us to develop my skills in the right way.
    Ubs inventory Singapore
    Peppol invoicenow Singapore
    Psg grant Singapore

  10. I Would like to thank you for this article. From this article, I got more and more useful information. This is so helpful to me. Keep updating more articles.
    Best Free Ecommerce Platform in Chennai
    Free Multi Vendor Marketplace in chennai
    Free Online Store Builder in chennai

  11. This post is so interactive and informative.keep update more information...
    Selenium Training in Bangalore
    Selenium Training in Pune

  12. you will need support or suggestions, write me privately.
    I interested in your implementation/use case.
    the best situs slot terbaik

  13. İnstagram takipçi satın al! İnstagram takipçi sitesi ile takipçi satın al sende sosyal medyada fenomen olmaya bir adım at. Sende hemen instagram takipçi satın almak istiyorsan tıkla:

    1- takipçi satın al

    2- takipçi satın al

    3- takipçi satın al

  14. https://designingcourses.in/graphic-designing-courses-in-bangalore/

    Learn graphic designing courses in bangalore we provide best graphic designing training and 100% placement assistance