![Page 1: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/1.jpg)
APACHE SLING & FRIENDS TECH MEETUPBERLIN, 22-24 SEPTEMBER 2014
OSGi Asynchronous Services: more than RPCMichael Dürig, Adobe Research
![Page 2: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/2.jpg)
Some typical code
socket.getInputStream().read(buffer);
![Page 3: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/3.jpg)
Timings on typical hardware...
Execute CPU instruction 1nsFetch from L2 cache 7 nsFetch from memory 100 nsRead from disk 8 msNetwork round-trip Europe-US 150 ms
source: http://norvig.com/21-days.html#answers
![Page 4: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/4.jpg)
...in human terms
Execute CPU instruction 1 sFetch from L2 cache 7 sFetch from memory 1.6 minRead from disk 13.2 weeksNetwork round-trip Europe-US 4.8 years
source: http://norvig.com/21-days.html#answers
![Page 5: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/5.jpg)
Outline
OSGi RFC 206: Asynchronous Services Promise Example
![Page 6: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/6.jpg)
Goals
Improve resource utilisation Parallelism Non blocking IO
Automatic thread management Runtime vs. hard coded
![Page 7: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/7.jpg)
OSGi RFC 206: Asynchronous Services
Asynchronous method calls Return Promise<T> instead of T Direct implementations Mediation through Async
![Page 8: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/8.jpg)
Mediating a service
Async async = ...ServiceReference<Weather> ref = ...
Weather weather = async.mediate(ref, Weather.class);Promise<Boolean> sunnyPromise = async.call(weather.isSunny());
![Page 9: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/9.jpg)
Promise<T>?
![Page 10: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/10.jpg)
Promise
Encapsulation of a value that maybe available at some later time can be read multiple times immutable once resolved
![Page 11: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/11.jpg)
Promise callback
sunnyPromise.then(...
![Page 12: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/12.jpg)
Promise callback: success
weather.isSunny().then( success -> { if (success.getValue()) { println("Sun fun and nothing to do"); } else { println("Singing in the rain"); }},
...
![Page 13: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/13.jpg)
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();});
![Page 14: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/14.jpg)
Effects
Promises capture the effects of latency: when the call back happens error: which call back happens
![Page 15: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/15.jpg)
Promise<T>!
![Page 16: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/16.jpg)
Vendor Service
interface Vendor { Promise<Offer> getOffer(String item);}
class Offer { public Offer(Vendor vendor, String item, double price, String currency) { ...
![Page 17: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/17.jpg)
Exchange Service
Promise<Offer> convertToEuro1(Offer offer);
Promise<Offer> convertToEuro2(Offer offer);
![Page 18: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/18.jpg)
Goals
Get offer from vendor Convert to € with recovery Fail gracefully Non blocking!
![Page 19: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/19.jpg)
getOffer...
Promise<Offer> getOffer(Vendor vendor, String item) { return vendor .getOffer(item);}
![Page 20: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/20.jpg)
flatMap
Promise<R> flatMap(T -> Promise<R>)
![Page 21: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/21.jpg)
flatMap
Promise<R> flatMap(Offer -> Promise<R>)
![Page 22: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/22.jpg)
flatMap
Promise<Offer> flatMap(Offer -> Promise<Offer>)
![Page 23: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/23.jpg)
flatMap
Promise<Offer> flatMap(Offer -> Promise<Offer>)
Promise<Offer> convertToEuro1(Offer);
![Page 24: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/24.jpg)
getOffer: convert to €
Promise<Offer> getOffer(Vendor vendor, String item) { return vendor .getOffer(item) .flatMap(offer -> convertToEuro1(offer));}
![Page 25: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/25.jpg)
recoverWith
Promise<R> recoverWith(Promise<?> -> Promise<R>)
![Page 26: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/26.jpg)
recoverWith
Promise<Offer> recoverWith(Promise<?> -> Promise<Offer>)
![Page 27: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/27.jpg)
recoverWith
Promise<Offer> recoverWith(Promise<?> -> Promise<Offer>)
Promise<Offer> convertToEuro2(Offer);
![Page 28: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/28.jpg)
getOffer: fallback
Promise<Offer> getOffer(Vendor vendor, String item) { return vendor .getOffer(item) .flatMap(offer -> convertToEuro1(offer) .recoverWith(failed -> convertToEuro2(offer)));}
![Page 29: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/29.jpg)
recover
Promise<R> recover(Promise<?> -> R)
![Page 30: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/30.jpg)
recover
Promise<Offer> recover(Promise<?> -> Offer)
![Page 31: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/31.jpg)
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));}
![Page 32: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/32.jpg)
Non blocking
Act on Promise<T> instead of T
![Page 33: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/33.jpg)
Demo
![Page 34: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/34.jpg)
Summary
Promises capture latency and error Non blocking Focus on main path
Decouple parallelisation Better resource utilisation Application scalability
![Page 36: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/36.jpg)
Appendix
![Page 37: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/37.jpg)
Deferred
Encapsulation of a value that should be made available at some later time can be written once immutable once resolved
![Page 38: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/38.jpg)
What about j.u.concurrent.Future?
Blocking semantics No callbacks
Not composable No map/flatMap/filter/...
![Page 39: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/39.jpg)
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/
![Page 40: APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 OSGi Asynchronous Services: more than RPC Michael Dürig, Adobe Research](https://reader034.vdocuments.mx/reader034/viewer/2022051516/56649d145503460f949e82c6/html5/thumbnails/40.jpg)
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