Akka: The Smash and Grab Intro - Meetup

6 downloads 182 Views 2MB Size Report
Jeremy Pierre. Yet Another Developer j14159. The Basics of Sane Concurrency. Akka: The Smash and Grab Intro twitter.com/hootsuite facebook.com/hootsuite.
Akka: The Smash and Grab Intro The Basics of Sane Concurrency

Jeremy Pierre Yet Another Developer @ j14159

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Akka What? Don’t call it a framework

So...?

A toolkit for sane concurrency. • Not just about actors • Blocking is bad, event-based is good • Crashing is not the end of the world

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

The Overview What I’ll Cover:

• Actors and ActorSystems

• Supervision(in passing) • Futures and Promises • Some interesting stuff with execution contexts, event buses and schedulers

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

The Overview What I’ll Cover:

• Actors and ActorSystems

• Supervision(in passing) • Futures and Promises • Some interesting stuff with execution contexts, event buses and schedulers What I’m Not Covering

• Remoting

• FSM • Routing/Dispatchers • IO twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

To help it all make sense... We use the Librarian Problem: • A Library has one Librarian but many Interns. • Interns do not know where to find specific books. • The Librarian can’t go find books for customers because they can then only help one at a time.

• If the Librarian tells the Interns where to find stuff, many book requests can be handled at the same time.

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Akka Actors The logical extreme of OOP and Information Hiding

• There’s only one method

to make it do stuff(tell / !) • They’re purely eventbased • They live in ActorSystems

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

ActorSystem

• They make ActorRefs • They’re the root of a hierarchy(think file system)

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

ActorSystem

• They make ActorRefs • They’re the root of a hierarchy(think file system)

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

ActorSystem

• They make ActorRefs • They’re the root of a hierarchy(think file system)

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Futures... ...let us do asynchronous stuff without an Actor

But where’s the result?

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Futures... ...let us do asynchronous stuff without an Actor

But where’s the result?

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Promises The “write” side of Futures

But that looks kind of boring...

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Promises The “write” side of Futures

But that looks kind of boring...

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

One more Promise

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Using the results of Future/Promise As you’d expect, this gives you another Future:

This callback is for side-effecting:

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Event Bus and Scheduler EventBus is (very) simple pub-sub for Actors:

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Event Bus and Scheduler EventBus is (very) simple pub-sub for Actors:

Scheduler?

"Thread.sleep is never fine. It's like drowning a kitten." - @viktorklang twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Event Bus and Scheduler EventBus is (very) simple pub-sub for Actors:

Scheduler makes stuff happen when you want it to:

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Asking and Piping import akka.pattern.ask

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Asking and Piping import akka.pattern.ask

import akka.pattern.pipe

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

ExecutionContext Futures and Promises need it

• It basically hides a java.util.concurrent.Executor • Your ActorSystem’s dispatcher is one • You can create them via configuration or programmatically

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Mix Them!

Never block your ActorSystem’s threads for DB calls! twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com

Thank You! Jeremy Pierre Yet Another Developer @j14159

twitter.com/hootsuite

facebook.com/hootsuite

slideshare.com/hootsuite

blog.hootsuite.com