18
An Introduction to Akka Mikołaj Koziarkiewicz

An Introduction to Akka

Embed Size (px)

Citation preview

Page 1: An Introduction to Akka

An Introduction to Akka

Mikołaj Koziarkiewicz

Page 2: An Introduction to Akka

Akka - what is it? 1/2

a platform,

"for building highly concurrent, distributed, and resilient message-drivenapplications on the JVM."[1],

based on the actor model.

Page 3: An Introduction to Akka

Akka - what is it? 2/2

platform has a lot of specialized extensions

all based on the actor core

goal of the presentation:

explain the core

Page 4: An Introduction to Akka

Actor model - what is it?

actor: unit of computation

actors build actor systems

quite "old" (1973).[2]

Page 5: An Introduction to Akka

Anatomy of an Akka Actor - intro 1/2

class MyActor extends Actor { def receive = { case Something => //... }}

Page 6: An Introduction to Akka

Anatomy of an Akka Actor - intro 2/2

Page 7: An Introduction to Akka

My first Akka Actor™

class HaiActor extends Actor { def receive: Receive = { case target: ActorRef => target ! "Hello!" case "Hello!" => sender ! "OHAI" case "OHAI" => println(s"$self: got OHAI from $sender") }}

Where:

type Receive = PartialFunction[Any, Unit]

Page 8: An Introduction to Akka

My first Akka Actor™ - example

Page 9: An Introduction to Akka

Anatomy of an Akka Actor - hierarchy

Page 10: An Introduction to Akka

Akka actor model - lifecycle & hierarchy

Purpose:

division of labor

supervision:

each actor has lifecycle

special hooks - preStart, preRestart, postStop

allow for e.g. self-init

"parent" controls how it reacts to failure

Page 11: An Introduction to Akka

Hierarchy - example

Page 12: An Introduction to Akka

Akka - guarantees

an actor processes one message at a time,

a message will be delivered at most 1 times,

messages are ordered by sender

Page 13: An Introduction to Akka

Guarantee takeaways

don’t pass mutable state,

use services to ensure send:

contex.scheduler

persistence

Page 14: An Introduction to Akka

Testing

Akka Testkit

Features:

can run special 1-thread dispatcher

asserts for getting msgs

TestProbe

Page 15: An Introduction to Akka

Remoting

Simple remoting:

e.g. "akka.tcp://[email protected]:1337/user/actorName"

remote creation as well

Page 16: An Introduction to Akka

Talking to actor system

Custom:

ask pattern

ask(actoRef, request)(timeout): Future

Many others (also builtin)

Page 17: An Introduction to Akka

Summary

Akka - actor model on the JVM

alternative way to handle

concurrency

distribution

declared guarantees

make reasoning easier

Page 18: An Introduction to Akka

THANK YOU!

The End

Doc link:

http://doc.akka.io/docs/akka/current/scala.html

Bibliography

http://akka.io , retrieved 2015-04-20

https://en.wikipedia.org/wiki/History_of_the_Actor_model , retrieved2015-04-12