reactive programming using rx java & akka actors - pdx-scala - june 2014

Post on 09-May-2015

3.295 Views

Category:

Technology

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Reactive Programmingusing RxJava & Akka Actors

Thomas Lockney (@tlockney)PDXScala

June 14, 2014

What is reactive programming?

Reactive programming is…

● A programming paradigm● About data-flow● Not really anything new

Reactive programming is...

● Event-driven● Scalable● Resilient● Responsive

(the definition via the manifesto, anyway)

Reorganizing our thoughts a bit...

● Think in terms of discrete events● Code in terms of behaviors

○ reacting to events● The system state changes over time based

on the flow of events

A brief digression...

Amdahl’s law (paraphrased):

The maximum increase in speed of a program is limited by the sequential portions of the code.

A brief digression...

Amdahl’s law (paraphrased):

If you have a program that runs for 4 hours, but 1 hour of runtime is necessarily serial, your

program will have at most a 4x speedup with parallelization.

Amdahl’s law

The thing about Amdahl’s law

This is death by a thousand cuts.

Every line of code is subject to this rule!

Some principles...

● Keep your data immutable● Share nothing● Blocking is bad!

Another brief digression...

● Immutable data structures● Referential transparency● First-class functions

… this gives you ...

Once you have:

Functional Programming

well...

Functional Programmingin the small

RxJava“a library for composing asynchronous and event-based programs using observable

sequences for the Java VM”

My translation:

RxJavaA library for composing behaviors

applied to event streams.

note: when I say behaviors, think referentially-transparent functions.

Marble Diagrams

Observable

● The “dual” of Iterable● Or an Observer for sequences● Somewhat similar to a Future

○ but, again, supports sequences

Observable

Basic mechanics● onNext()● onError()● onComplete()

Functional Composition

● map()● flatMap()● filter()

map()

flatMap()

filter()

Creating Observables

● Observable.from(...), Observable.just(...), Observable.range(...), etc.

● Implement the Observable interface○ explicit calls to onNext(), onError(), onComplete()

Observer & Subscriber

Observer● interface for handling Observables onNext,

onError, onCompleteSubscriber● represents a subscription to an observable

Demo time!

RxJava is Reactive

● event-based (data-flows)● scalable*● resilient* (???)● responsive* (???)

* - requires some additional work on your end??? - the story here is not entirely clear or potentially even available in current releases

Akka& the actor model

Actors (the Actor model)

Defined by three traits● a behavior (reacts to events)● an state model● send/receive (immutable) messages

Actors

● messages are processed sequentially● multiple actors can be working at the same

time● actors are not threads● an actor can block (but should avoid it)● no shared state

Actor System

● actors interact● actors exist together within an actor system● the system defines the outermost bounds

Akka

● Scala-based actor model toolkit● Provides a lot of extras● We won’t be talking about those

Akka

● Supervisors○ provide exception handling○ “let it crash”○ orthogonal to actor behavior

● Location transparency○ abstracts the idea of “where” an actor lives

Akka

● Mailboxes● Routing

○ e.g., round-robin, smallest mailbox, etc.● Dispatching

○ essentially how to allocate thread resources

Demo time!

Akka is Reactive

● event-based● resilient/fault-tolerant● easily scalable*● responsive*

* - requires some additional work on your end

Reactive Streams

Reactive Streams… I wish I had time … ;~)

top related