Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Reactive Programming in Java 8
with Rx-Java
Kasun Indrasiri
Software Architect, WSO2
January 2016
Why Reactive Programming?
• Why another programming paradigm?
• Things are different now!
• Conventional software applicat...
What it actually means to be Reactive?
• “Readily responsive to a stimulus” - merriam-webster
• Reactive Systems :
• respo...
Reactive Programming
• A programming paradigm that helps you to build
‘Reactive Systems’. E.g.: Google Spreadsheet
• In a ...
Why Rx-Java?
• The Netflix story..
The Netflix API
• How to reducing network chattiness?
• Granular API.
• client applications to make multiple calls that ne...
The Netflix API
• Discrete requests from devices, should be collapsed into
a single request
• Server-side concurrency is n...
Embrace Concurrency
• Blocking calls?... Not anymore.
• Server-side concurrency is a must.
• Low level primitive of concur...
Java Futures
• Represents the result of an asynchronous computation.
• Can be used to retrieve the result asynchronously a...
Callbacks
• A callback is a piece of executable code that is passed as
an argument to other code, which is expected to cal...
Callbacks
• Use case :
• Client send a request to the Application
• Application calls service A
• Get the response from Se...
Callbacks
• Callback Hell!
• Callbacks based applications get extremely complex when we have nested
callbacks.
• Leads to ...
Reactive Extensions - Rx
• ReactiveX is a library for composing asynchronous and
event-based programs by using observable ...
Observer Pattern
• ‘Subject’ notifies the ‘observers’ when a change has
happened in the subject.
Observable
• Rx-Java introduces ‘Observable’ data type by extending
the traditional Observer Pattern.
Observers
Observable
Observable vs Observer Pattern
• The producer signal that there is no more data available:
the
• onCompleted() event
• The...
Observable and Subscriber
• The producer signal that there is no more data available:
the
ObservableSubscriber
subscribe
o...
Observable and Subscriber – In Action
Creating Observable
• Observable.create()
• Observable.just()
• Observable.from()
Observable and Operators
• Observable -> Operator -> Subscriber
Filtering Observables
• Selectively emit items from a source Observable
Filtering Observables
• Filtering Operators
Transforming Observables
• Transform items that are emitted by an Observable
Transforming Observables
• Transforming Operators
Filtering and Transforming – In Action
Transforming Observables
• FlatMap
• Observable.flatMap() takes the emissions of one Observable and returns the
emissions ...
FlatMap – In Action
More Operators
• Combining Observables
• Error Handling Operators
• Observable Utility Operators
• Conditional and Boolean...
Rx-Java in Action
• Use case : Service Orchestration
Source Code : https://github.com/kasun04/rnd/tree/master/rx-
java/src...
Scheduler
• Rx is single threaded by default.
• Schedulers are the easiest way to bring multi-threading
into your apps.
• ...
Scheduler
• Schedulers.io(): meant for I/O-bound work
• Schedulers.computation(): meant for computational
work such as eve...
Rx-Java in Action
• Use case : Service Orchestration with Schedulers
Source Code : https://github.com/kasun04/rnd/tree/mas...
Conclusion
• Get rid of blocking code!
• Think ‘non-blocking’
• Think reactive
• Rx-Java – steep learning curve
• Develop ...
Questions?
References
• http://reactivex.io/documentation
• http://blog.danlew.net/2014/09/15/grokking-rxjava-
part-1/
• http://techb...
Thank You
Upcoming SlideShare
Loading in …5
×

Reactive Programming in Java 8 with Rx-Java

28,571 views

Published on

Demystifying Rx-Java

Published in: Software
  • DOWNLOAD FULL eBOOK INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookeBOOK Crime, eeBOOK Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • .DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... .DOWNLOAD PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... .DOWNLOAD EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... .DOWNLOAD doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... .DOWNLOAD PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... .DOWNLOAD EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... .DOWNLOAD doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Hi All, We are planning to start new Salesforce Online batch on this week... If any one interested to attend the demo please register in our website... For this batch we are also provide everyday recorded sessions with Materials. For more information feel free to contact us : siva@keylabstraining.com. For Course Content and Recorded Demo Click Here : http://www.keylabstraining.com/salesforce-online-training-hyderabad-bangalore
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Hi All, We are planning to start new devops online batch on this week... If any one interested to attend the demo please register in our website... For this batch we are also provide everyday recorded sessions with Materials. For more information feel free to contact us : siva@keylabstraining.com. For Course Content and Recorded Demo Click Here : http://www.keylabstraining.com/devops-online-training-tutorial
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Reactive Programming in Java 8 with Rx-Java

  1. 1. Reactive Programming in Java 8 with Rx-Java Kasun Indrasiri Software Architect, WSO2 January 2016
  2. 2. Why Reactive Programming? • Why another programming paradigm? • Things are different now! • Conventional software applications won’t be able to fulfill the modern enterprise needs. • Its time to rethink about our software architecture patterns and programming techniques.
  3. 3. What it actually means to be Reactive? • “Readily responsive to a stimulus” - merriam-webster • Reactive Systems : • responds in a timely manner • stays responsive in the face of failure • stays responsive under varying workload • rely on asynchronous message-passing
  4. 4. Reactive Programming • A programming paradigm that helps you to build ‘Reactive Systems’. E.g.: Google Spreadsheet • In a reactive world, we can't just wait for a function result, a network call, or a database query to return. • Every moment we wait for something, we lose the opportunity to do other things in parallel. • Reactive programming can be done in several ways • Akka Actors • Reactive Extensions (Rx) – reactive + functional
  5. 5. Why Rx-Java? • The Netflix story..
  6. 6. The Netflix API • How to reducing network chattiness? • Granular API. • client applications to make multiple calls that need to be assembled in order to render a single user experience
  7. 7. The Netflix API • Discrete requests from devices, should be collapsed into a single request • Server-side concurrency is needed to effectively reduce network chattiness. • Nested, conditional, parallel execution of backend network calls.
  8. 8. Embrace Concurrency • Blocking calls?... Not anymore. • Server-side concurrency is a must. • Low level primitive of concurrency. • Concurrent programming is hard!
  9. 9. Java Futures • Represents the result of an asynchronous computation. • Can be used to retrieve the result asynchronously at a later point in time • Cons: • Retrieving value from future is a blocking call : Future.get() • Messy with Nested Futures -> complex to implement/maintain.
  10. 10. Callbacks • A callback is a piece of executable code that is passed as an argument to other code, which is expected to call back the argument at some convenient time.
  11. 11. Callbacks • Use case : • Client send a request to the Application • Application calls service A • Get the response from ServiceA and send it to service B • Get the response from ServiceB and send it to Service C • Get the response from ServiceC and send it to the Client Service A Service B Service C 1 2 3 ApplicationClient
  12. 12. Callbacks • Callback Hell! • Callbacks based applications get extremely complex when we have nested callbacks. • Leads to messy code, hard to troubleshoot. • Example : https://github.com/kasun04/rnd/tree/master/rx- java/src/main/java/org/kasun/rnd/rxjava/serviceinvoker/callback
  13. 13. Reactive Extensions - Rx • ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences. • ‘The Observer pattern done right.’ • Initially implemented for C# by Microsoft, later ported to many languages by Netflix Rx. • Rx-Java is the Java implementation.
  14. 14. Observer Pattern • ‘Subject’ notifies the ‘observers’ when a change has happened in the subject.
  15. 15. Observable • Rx-Java introduces ‘Observable’ data type by extending the traditional Observer Pattern. Observers Observable
  16. 16. Observable vs Observer Pattern • The producer signal that there is no more data available: the • onCompleted() event • The producer can signal that an error occurred • onError() event
  17. 17. Observable and Subscriber • The producer signal that there is no more data available: the ObservableSubscriber subscribe onNext* onCompleted
  18. 18. Observable and Subscriber – In Action
  19. 19. Creating Observable • Observable.create() • Observable.just() • Observable.from()
  20. 20. Observable and Operators • Observable -> Operator -> Subscriber
  21. 21. Filtering Observables • Selectively emit items from a source Observable
  22. 22. Filtering Observables • Filtering Operators
  23. 23. Transforming Observables • Transform items that are emitted by an Observable
  24. 24. Transforming Observables • Transforming Operators
  25. 25. Filtering and Transforming – In Action
  26. 26. Transforming Observables • FlatMap • Observable.flatMap() takes the emissions of one Observable and returns the emissions of another Observable to take its place. Observable<List<String>> query(String text){...} query("Hello, world!") .flatMap(new Func1<List<String>, Observable<String>>() { @Override public Observable<String> call(List<String> urls) { return Observable.from(urls); } }) .subscribe(url -> System.out.println(url));
  27. 27. FlatMap – In Action
  28. 28. More Operators • Combining Observables • Error Handling Operators • Observable Utility Operators • Conditional and Boolean Operators http://reactivex.io/documentation/operators.html
  29. 29. Rx-Java in Action • Use case : Service Orchestration Source Code : https://github.com/kasun04/rnd/tree/master/rx- java/src/main/java/org/kasun/rnd/rxjava/serviceinvoker/rx/nonblocking Service A Service B Service C 1 2 3 ApplicationClient
  30. 30. Scheduler • Rx is single threaded by default. • Schedulers are the easiest way to bring multi-threading into your apps. • subscribeOn • observerOn
  31. 31. Scheduler • Schedulers.io(): meant for I/O-bound work • Schedulers.computation(): meant for computational work such as event-loops • Schedulers.newThread(): creates a new thread for each unit of work
  32. 32. Rx-Java in Action • Use case : Service Orchestration with Schedulers Source Code : https://github.com/kasun04/rnd/tree/master/rx- java/src/main/java/org/kasun/rnd/rxjava/serviceinvoker/rx/nonblocking Service A Service B Service C 1 2 3 ApplicationClient
  33. 33. Conclusion • Get rid of blocking code! • Think ‘non-blocking’ • Think reactive • Rx-Java – steep learning curve • Develop use cases • Leverage Java 8
  34. 34. Questions?
  35. 35. References • http://reactivex.io/documentation • http://blog.danlew.net/2014/09/15/grokking-rxjava- part-1/ • http://techblog.netflix.com/2013/02/rxjava-netflix- api.html • Source Code : https://github.com/kasun04/rnd/tree/master/rx-java
  36. 36. Thank You

×