Play2 Non blocking, composable reactive web programming with Iteratees and Promises in Play2 Monday, June 18, 12
@sadache @hguergachi @guillaumebort Monday, June 18, 12
www.zenexity.com Web Oriented Architectures Monday, June 18, 12
What is Play? • The Play framework makes it easier to build web applications with Java & Scala. • Play is based on a lightweight, stateless, web- friendly architecture for highly-scalable applications - thanks to its reactive model, based on Futures and Iteratee IO. Monday, June 18, 12
The Web Evolved Monday, June 18, 12
The Web Evolved • Data distribution and Information Systems Partitioning • Streams of Data Monday, June 18, 12
Execution Thread get x get y Execution Thread get x get y Problem Execution Thread get x get y Execution Thread get x get y Execution Thread get x get y Monday, June 18, 12
Opportunity get x get x get x get x get x Execution Thread Execution Thread Execution Thread Execution Thread Execution Thread get y get y get y get y get y Monday, June 18, 12
Opportunity • What if the callee can notify me on completeness of his task • A callback Monday, June 18, 12
Callback Hell! Monday, June 18, 12
An Opportunity and a Model • The service we are calling should support the Non Blocking Style (ex: NIO) • A model of programming: Reactive with Futures and Promises Monday, June 18, 12
Futures and Promises • val f : Future[A] = ... • f.map( a => getB(a)) // Eventually transforming A to B • f.flatMap( a => eventuallyGetB(a)) Monday, June 18, 12
Play2’s Action takes the future! Action { rq => AsyncResult(f.map(a => Ok(a)) } Monday, June 18, 12
Play2’s Action takes the future! Monday, June 18, 12
Play2’s Action takes the future! • The service we are calling should support the Non Blocking Style (ex: NIO) • Webcalls (WS), network, filesystem, datastores (MongoDb), SQL (async MySQL), disruptor, actors, …) Monday, June 18, 12
Streams of Data • Streams of data are everywhere • File uploads, tweets, finance data streams, ... Monday, June 18, 12
InputStream is outdated/too low level public abstract int read () throws IOException Reads the next byte of data from the input stream. [...] This method blocks until input data is available Monday, June 18, 12
Problem of classic model • Blocking read and blocking write • Resources consumption (memory, threads, disk). • Pro-active waiting. • Input/Output Stream Monday, June 18, 12
Going Reactive • Inversion of control. • Without loosing control. • Iteratee/Enumerator IO. Monday, June 18, 12
Iteratee Enumerator[E] Iteratee[E,A] Input[E] is either El[E] or EOF Step[E,A] ? Enumeratee[EIn,EOut] is an adapter Done[E,A] Error[E] Cont[E,A] Input[E] => Iteratee[E,A] Step[E,A] ? Done[E,A] Cont[E,A] Error[E] Monday, June 18, 12
What if the Iteratee’s Input processing is reactive? an opportunity! Iteratee[E,A] Promise [Step[E,A]] ? Done[E,A] Error[E] Cont[E,A] Monday, June 18, 12
Reactive File Upload Monday, June 18, 12
A programming model • Tools for creating, consuming and adapting streams • map, filter, fold, unfold, collect, group, scan, foreach, ... • All in a reactive manner. • Iteratee, Enumerator and Enumeratee. Monday, June 18, 12
Parsing CSV like file upload https://gist.github.com/2939230 Monday, June 18, 12
How to construct Enumerators (Streams)? • Pure (from a list of elements) • generateM and unfoldM • Push Monday, June 18, 12
Streams Monday, June 18, 12
More • www.playframework.com • Check and play with the provided samples • Ask • Thanks to @sgodbillon for the MongoDb example Monday, June 18, 12
Recommend
More recommend