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.
APACHE SLING & FRIENDS TECH MEETUP 
BERLIN, 22-24 SEPTEMBER 2014 
OSGi Asynchronous Services: more than RPC 
Michael Dürig...
Some typical code 
socket.getInputStream().read(buffer);
Timings on typical hardware... 
Execute CPU instruction 1ns 
Fetch from L2 cache 7 ns 
Fetch from memory 100 ns 
Read from...
...in human terms 
Execute CPU instruction 1 s 
Fetch from L2 cache 7 s 
Fetch from memory 1.6 min 
Read from disk 13.2 we...
Outline 
 OSGi RFC 206: Asynchronous Services 
 Promise 
 Example
Goals 
 Improve resource utilisation 
 Parallelism 
 Non blocking IO 
 Automatic thread management 
 Runtime vs. hard...
OSGi RFC 206: Asynchronous Services 
 Asynchronous method calls 
 Return Promise<T> instead of T 
 Direct implementatio...
Mediating a service 
Async async = ... 
ServiceReference<Weather> ref = ... 
Weather weather = async.mediate(ref, Weather....
Promise<T>?
Promise 
 Encapsulation of a value that 
 maybe available at some later time 
 can be read multiple times 
 immutable ...
Promise callback 
sunnyPromise.then(...
Promise callback: success 
weather.isSunny().then( 
success -> { 
if (success.getValue()) { 
println("Sun fun and nothing ...
Promise callback: failure 
weather.isSunny().then( 
success -> { 
if (success.getValue()) { 
println("Sun fun and nothing ...
Effects 
 Promises capture the effects of 
 latency: when the call back happens 
 error: which call back happens
Promise<T>!
Vendor Service 
interface Vendor { 
Promise<Offer> getOffer(String item); 
} 
class Offer { 
public Offer(Vendor vendor, 
...
Exchange Service 
Promise<Offer> convertToEuro1(Offer offer); 
Promise<Offer> convertToEuro2(Offer offer);
Goals 
 Get offer from vendor 
 Convert to € with recovery 
 Fail gracefully 
 Non blocking!
getOffer... 
Promise<Offer> getOffer(Vendor vendor, String item) { 
return vendor 
.getOffer(item); 
}
flatMap 
Promise<R> flatMap(T -> Promise<R>)
flatMap 
Promise<R> flatMap(Offer -> Promise<R>)
flatMap 
Promise<Offer> flatMap(Offer -> Promise<Offer>)
flatMap 
Promise<Offer> flatMap(Offer -> Promise<Offer>) 
Promise<Offer> convertToEuro1(Offer);
getOffer: convert to € 
Promise<Offer> getOffer(Vendor vendor, String item) { 
return vendor 
.getOffer(item) 
.flatMap(of...
recoverWith 
Promise<R> recoverWith(Promise<?> -> Promise<R>)
recoverWith 
Promise<Offer> recoverWith(Promise<?> -> Promise<Offer>)
recoverWith 
Promise<Offer> recoverWith(Promise<?> -> Promise<Offer>) 
Promise<Offer> convertToEuro2(Offer);
getOffer: fallback 
Promise<Offer> getOffer(Vendor vendor, String item) { 
return vendor 
.getOffer(item) 
.flatMap(offer ...
recover 
Promise<R> recover(Promise<?> -> R)
recover 
Promise<Offer> recover(Promise<?> -> Offer)
getOffer: fail gracefully 
Promise<Offer> getOffer(Vendor vendor, String item) { 
return vendor 
.getOffer(item) 
.flatMap...
Non blocking 
Act on Promise<T>instead of T
Demo
Summary 
 Promises capture latency and error 
 Non blocking 
 Focus on main path 
 Decouple parallelisation 
 Better ...
Resources 
http://goo.gl/pB9fza
Appendix
Deferred 
 Encapsulation of a value that 
 should be made available at some later time 
 can be written once 
 immutab...
What about j.u.concurrent.Future? 
 Blocking semantics 
 No callbacks 
 Not composable 
 No map/flatMap/filter/...
Resources 
 Support material: https://github.com/mduerig/async-support/wiki 
 Session slides 
 Runnable demo 
 Links t...
Resources 
 RxJava: https://github.com/ReactiveX/RxJava 
 Observables: taking Promises one step further 
 Akka: http://...
Upcoming SlideShare
Loading in …5
×

OSGi Asynchronous Services: more than RPC

750 views

Published on

The Asynchronous Services Specification of the OSGi Alliance is an effort to better support parallelism on the OSGi platform. While it allows services to be called asynchronously it is more that just another RPC mechanism.

This sessions shows how to use Asynchronous Services to structure highly concurrent programs for maximised resource utilisation and throughput and how this approach results in less blocking and lock contention.

While not entirely new, the approach shown resembles a shift in paradigms in the sense that explicit thread management moves from client code to the environment much like it had happened for explicit memory management in the 90ies.

Published in: Software
  • DOWNLOAD FULL BOOKS INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL 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
  • Be the first to like this

OSGi Asynchronous Services: more than RPC

  1. 1. APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research
  2. 2. Some typical code socket.getInputStream().read(buffer);
  3. 3. Timings on typical hardware... Execute CPU instruction 1ns Fetch from L2 cache 7 ns Fetch from memory 100 ns Read from disk 8 ms Network round-trip Europe-US 150 ms source: http://norvig.com/21-days.html#answers
  4. 4. ...in human terms Execute CPU instruction 1 s Fetch from L2 cache 7 s Fetch from memory 1.6 min Read from disk 13.2 weeks Network round-trip Europe-US 4.8 years source: http://norvig.com/21-days.html#answers
  5. 5. Outline  OSGi RFC 206: Asynchronous Services  Promise  Example
  6. 6. Goals  Improve resource utilisation  Parallelism  Non blocking IO  Automatic thread management  Runtime vs. hard coded
  7. 7. OSGi RFC 206: Asynchronous Services  Asynchronous method calls  Return Promise<T> instead of T  Direct implementations  Mediation through Async
  8. 8. Mediating a service Async async = ... ServiceReference<Weather> ref = ... Weather weather = async.mediate(ref, Weather.class); Promise<Boolean> sunnyPromise = async.call(weather.isSunny());
  9. 9. Promise<T>?
  10. 10. Promise  Encapsulation of a value that  maybe available at some later time  can be read multiple times  immutable once resolved
  11. 11. Promise callback sunnyPromise.then(...
  12. 12. Promise callback: success weather.isSunny().then( success -> { if (success.getValue()) { println("Sun fun and nothing to do"); } else { println("Singing in the rain"); }}, ...
  13. 13. Promise callback: failure weather.isSunny().then( success -> { if (success.getValue()) { println("Sun fun and nothing to do"); } else { println("Singing in the rain"); }}, failure -> { failure.getFailure().printStackTrace();});
  14. 14. Effects  Promises capture the effects of  latency: when the call back happens  error: which call back happens
  15. 15. Promise<T>!
  16. 16. Vendor Service interface Vendor { Promise<Offer> getOffer(String item); } class Offer { public Offer(Vendor vendor, String item, double price, String currency) { ...
  17. 17. Exchange Service Promise<Offer> convertToEuro1(Offer offer); Promise<Offer> convertToEuro2(Offer offer);
  18. 18. Goals  Get offer from vendor  Convert to € with recovery  Fail gracefully  Non blocking!
  19. 19. getOffer... Promise<Offer> getOffer(Vendor vendor, String item) { return vendor .getOffer(item); }
  20. 20. flatMap Promise<R> flatMap(T -> Promise<R>)
  21. 21. flatMap Promise<R> flatMap(Offer -> Promise<R>)
  22. 22. flatMap Promise<Offer> flatMap(Offer -> Promise<Offer>)
  23. 23. flatMap Promise<Offer> flatMap(Offer -> Promise<Offer>) Promise<Offer> convertToEuro1(Offer);
  24. 24. getOffer: convert to € Promise<Offer> getOffer(Vendor vendor, String item) { return vendor .getOffer(item) .flatMap(offer -> convertToEuro1(offer)); }
  25. 25. recoverWith Promise<R> recoverWith(Promise<?> -> Promise<R>)
  26. 26. recoverWith Promise<Offer> recoverWith(Promise<?> -> Promise<Offer>)
  27. 27. recoverWith Promise<Offer> recoverWith(Promise<?> -> Promise<Offer>) Promise<Offer> convertToEuro2(Offer);
  28. 28. getOffer: fallback Promise<Offer> getOffer(Vendor vendor, String item) { return vendor .getOffer(item) .flatMap(offer -> convertToEuro1(offer) .recoverWith(failed -> convertToEuro2(offer))); }
  29. 29. recover Promise<R> recover(Promise<?> -> R)
  30. 30. recover Promise<Offer> recover(Promise<?> -> Offer)
  31. 31. getOffer: fail gracefully Promise<Offer> getOffer(Vendor vendor, String item) { return vendor .getOffer(item) .flatMap(offer -> convertToEuro1(offer) .recoverWith(failed -> convertToEuro2(offer)) .recover(failed -> Offer.NONE)); }
  32. 32. Non blocking Act on Promise<T>instead of T
  33. 33. Demo
  34. 34. Summary  Promises capture latency and error  Non blocking  Focus on main path  Decouple parallelisation  Better resource utilisation  Application scalability
  35. 35. Resources http://goo.gl/pB9fza
  36. 36. Appendix
  37. 37. Deferred  Encapsulation of a value that  should be made available at some later time  can be written once  immutable once resolved
  38. 38. What about j.u.concurrent.Future?  Blocking semantics  No callbacks  Not composable  No map/flatMap/filter/...
  39. 39. Resources  Support material: https://github.com/mduerig/async-support/wiki  Session slides  Runnable demo  Links to further reading  RFC 206: https://github.com/osgi/design/tree/master/rfcs/rfc0206  Public draft of OSGi RFC 206: Asynchronous Services  OSGi Alliance: http://www.osgi.org/
  40. 40. Resources  RxJava: https://github.com/ReactiveX/RxJava  Observables: taking Promises one step further  Akka: http://akka.io/  Toolkit for concurrent and distributed applications on the JVM

×