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
Jersey supports RxJava Observable, Java 8 CompletionStage and CompletableFuture, Guava ListenableFuture and Futures, JSR-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:
- Reactive Jersey Client – Motivation
- Reactive Jersey Client – Usage and Supported Reactive Libraries
- Reactive Jersey Client – Customization (SPI)
Jersey source code contains also examples for supported reactive libraries, see modules:
- examples/rx-client-webapp - Jersey Reactive Client Extension WebApp Example.
- examples/rx-client-java8-webapp - Jersey Reactive Client Extension (Java8) WebApp Example.
Hystrix
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.
Code
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; ... @Override protected Observable<Forecast> run() { return RxObservable.from(forecastWebTarget) .resolveTemplate("destination", destination.getDestination()) .request().rx() .get(Forecast.class); } @Override 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!
Links
- https://github.com/Netflix/Hystrix - Hystrix GitHub
- https://github.com/Netflix/Hystrix/wiki - Hystrix GitHub Wiki
- http://netflix.github.io/Hystrix/javadoc/ - Hystrix Javadoc
- https://jersey.java.net/documentation/latest/rx-client.html - Jersey User Guide: Reactive Jersey Client API
- http://reactivex.io/RxJava/javadoc/ - RxJava Javadoc
- http://blogs.microsoft.co.il/bnaya/2010/02/25/rx-for-beginners-toc/ - Microsoft Blog: Rx for beginners
- https://speakerdeck.com/benjchristensen/reactive-streams-with-rx-at-javaone-2014 - Ben Christensen's Reactive Streams with Rx at JavaOne 2014 slides
- https://www.parleys.com/play/543f8d5be4b06e1184ae4106/about - Ben Christensen's Reactive Streams with Rx at JavaOne 2014 presentation
The article is good. Thankyou for sharing a good one. Keep sharing more like this article.
ReplyDeleteDrupal Training in Chennai
Drupal Classes
Drupal Training in Velachery
Photoshop Classes in Chennai
Best Place to Learn Photoshop in Chennai
Photoshop Training
Manual Testing Training in Chennai
Mobile Testing Training in Chennai
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.
ReplyDeleteThe article is so informative. This is more helpful for our
ReplyDeletebest software testing training in chennai
best software testing training institute in chennai with placement
software testing training
courses
software testing training and placement
software testing training online
software testing class
software testing classes in chennai
best software testing courses in chennai
automation testing courses in chennai
Thanks for sharing.
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.
ReplyDeleteLearn Best Digital Marketing Course Training in Chennai in professional institute to get reputed job.
Great Article. Thank you for sharing! Really an awesome post for every one.
ReplyDeleteProject Centers in Chennai
JavaScript Training in Chennai
Final Year Project Domains for IT
JavaScript Training in Chennai
Thanks for sharing this useful information..
ReplyDeletespoken english classes in bangalore
Spoken English Classes in Chennai
spoken english class in coimbatore
spoken english class in madurai
Data Science Courses in Bangalore
devops training in bangalore
English Coaching Classes in Chennai
spoken english course in coimbatore
spoken english classes in btm
spoken english classes in marathahalli
Thanks for this wonderful blog. keep update more information about this
ReplyDeleteEthical Hacking Course in Chennai
Hacking Course in Chennai
Ethical hacking course in bangalore
Ethical hacking course in coimbatore
Ethical Hacking Training in Chennai
Certified Ethical Hacking Course in Chennai
Ethical Hacking Training Institute in Chennai
Ethical hacking Training institute in bangalore
Software Testing Training in Chennai
ielts coaching centre in coimbatore
I appreciate you for this blog. More informative, thanks for sharing with us.
ReplyDeleteSalesforce Training in Chennai
salesforce training in bangalore
Salesforce Course in Chennai
salesforce institute in bangalore
salesforce developer training in chennai
salesforce course in bangalore
Python Training in Coimbatore
Angularjs Training in Bangalore
The blog you shared is very good. I expect more information from you like this blog. Thankyou.
ReplyDeletePython Training in bangalore
Python Course in Bangalore
Angularjs course Bangalore
Angularjs Training in Bangalore
Web Designing Course in bangalore
Web Development courses in bangalore
Salesforce Course in Bangalore
salesforce training in bangalore
Big Data Training in Bangalore
Hadoop Training in Bangalore
This post shares some valuable information.
ReplyDeleteAWS Training in Bangalore
AWS Training in Chennai
AWS Training in BTM
AWS Training in Marathahalli
Best AWS Training in Marathahalli
Data Science Courses in Bangalore
DevOps Training in Bangalore
PHP Training in Bangalore
DOT NET Training in Bangalore
Spoken English Classes in Bangalore
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!
ReplyDeleteThere's definately loads to apprehend approximately this problem. I certainly like all the factors you made guide.
ReplyDeleteLovely post and i eagerly waiting for your new updates about this title.
ReplyDeleteSpark Training in Chennai
Spark Training
Pega Training in Chennai
Primavera Training in Chennai
Unix Training in Chennai
Linux Training in Chennai
Social Media Marketing Courses in Chennai
Graphic Design Courses in Chennai
Placement Training in Chennai
Advanced Excel Training in Chennai
Aivivu chuyên vé máy bay, tham khảo
ReplyDeleteVé máy bay đi Mỹ
có chuyến bay từ mỹ về việt nam chưa
chuyến bay thương mại từ canada về việt nam
Lịch bay từ Hàn Quốc về Việt Nam tháng 7
Fantastic blog found to be very exciting to come across such an excellent blog. Keep sharing blogs.
ReplyDeleteapp trends
hadoop technologies
benefits of cloud
different cloud technologies
basic networking interview questions and answers
node js interview questions medium
The above content is very interesting to read.
ReplyDeletedigital marketing executive interview questions and answers for freshers
basic networking questions and answers