Play2 Non blocking, composable reactive web programming with - - PowerPoint PPT Presentation

play2
SMART_READER_LITE
LIVE PREVIEW

Play2 Non blocking, composable reactive web programming with - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Play2

Non blocking, composable reactive web programming with Iteratees and Promises in Play2

Monday, June 18, 12

slide-2
SLIDE 2

@guillaumebort @sadache @hguergachi

Monday, June 18, 12

slide-3
SLIDE 3

www.zenexity.com

Web Oriented Architectures

Monday, June 18, 12

slide-4
SLIDE 4

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

slide-5
SLIDE 5

The Web Evolved

Monday, June 18, 12

slide-6
SLIDE 6

The Web Evolved

  • Data distribution and Information Systems

Partitioning

  • Streams of Data

Monday, June 18, 12

slide-7
SLIDE 7

Problem

get x get y

Execution Thread

get x get y

Execution Thread

get x get y

Execution Thread

get x get y

Execution Thread

get x get y

Execution Thread

Monday, June 18, 12

slide-8
SLIDE 8

Opportunity

get x get y

Execution Thread

get x get y

Execution Thread

get x get y

Execution Thread

get x get y

Execution Thread

get x get y

Execution Thread

Monday, June 18, 12

slide-9
SLIDE 9

Opportunity

  • What if the callee can notify me on completeness of

his task

  • A callback

Monday, June 18, 12

slide-10
SLIDE 10

Callback Hell!

Monday, June 18, 12

slide-11
SLIDE 11

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

slide-12
SLIDE 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

slide-13
SLIDE 13

Play2’s Action takes the future!

Action { rq => AsyncResult(f.map(a => Ok(a)) }

Monday, June 18, 12

slide-14
SLIDE 14

Play2’s Action takes the future!

Monday, June 18, 12

slide-15
SLIDE 15

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

slide-16
SLIDE 16

Streams of Data

  • Streams of data are everywhere
  • File uploads, tweets, finance data streams, ...

Monday, June 18, 12

slide-17
SLIDE 17

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

slide-18
SLIDE 18

Problem of classic model

  • Blocking read and blocking write
  • Resources consumption (memory, threads, disk).
  • Pro-active waiting.
  • Input/Output Stream

Monday, June 18, 12

slide-19
SLIDE 19

Going Reactive

  • Inversion of control.
  • Without loosing control.
  • Iteratee/Enumerator IO.

Monday, June 18, 12

slide-20
SLIDE 20

Iteratee

Iteratee[E,A]

Step[E,A] Done[E,A] Cont[E,A] Error[E]

?

Input[E] => Iteratee[E,A]

Step[E,A] Done[E,A] Cont[E,A] Error[E]

? Enumerator[E]

Input[E] is either El[E] or EOF

Enumeratee[EIn,EOut]

is an adapter

Monday, June 18, 12

slide-21
SLIDE 21

What if the Iteratee’s Input processing is reactive? an opportunity!

Iteratee[E,A]

Promise[Step[E,A]]

Done[E,A] Cont[E,A] Error[E]

?

Monday, June 18, 12

slide-22
SLIDE 22

Reactive File Upload

Monday, June 18, 12

slide-23
SLIDE 23

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

slide-24
SLIDE 24

Parsing CSV like file upload

https://gist.github.com/2939230

Monday, June 18, 12

slide-25
SLIDE 25

How to construct Enumerators (Streams)?

  • Pure (from a list of elements)
  • generateM and unfoldM
  • Push

Monday, June 18, 12

slide-26
SLIDE 26

Streams

Monday, June 18, 12

slide-27
SLIDE 27

More

  • www.playframework.com
  • Check and play with the provided samples
  • Ask
  • Thanks to @sgodbillon for the MongoDb

example

Monday, June 18, 12