17
Messaging Patterns with Akka (Remote) Actors John Nestor and Dragos Manolescu Whitepages, Inc. April 14, 2015

Messaging patterns

Embed Size (px)

Citation preview

Messaging Patterns with Akka (Remote) Actors

John Nestor and Dragos Manolescu Whitepages, Inc.

April 14, 2015

Prelude• JLine (http://jline.sourceforge.net)

• Command history, tab completion, line editing

• Custom key bindings

• PersistJson (https://github.com/nestorpersist/json)

• Fast Scala JSON parser

• JSON tree/Scala classes

• Pretty/Compact

• Demo code at https://github.com/polymorphic/akka-messaging-patterns.git

Akka Remote Actors• Design decisions:

• Symmetric communication

• Symmetric roles

• Remote scenarios:

• Lookup

• Remote creation

Akka Actor Model

Actor

BehaviorMailbox

Parent

Child

Child

M M M

Actor

BehaviorMailbox

Messages

- Configuration- Thread pools

Actor System

Akka Actor Model w/ Remoting

Actor

BehaviorMailbox

Parent

Child

Child

M M MActor

BehaviorMailbox

Messages

- Configuration- Thread pools

Actor System

Lookup via ActorSelection

Remote creation via deployment configuration

- Configuration- Thread pools

Actor System

Actor System Configuration for Remoting

akka { actor { provider = "akka.remote.RemoteActorRefProvider" } remote { enabled-transports = ["akka.remote.netty.tcp"] netty.tcp { hostname = "127.0.0.1" port = 2552 } } }

Messaging Pattern: OneWay

Client Actor

Server Actor

JSON(cmd, id, msg)

(server work)

Messaging Pattern: Request-Reply

Client Actor

Server Actor

JSON(cmd, id, msg)

(server work)

JSON("done", id, msg)

Messaging Pattern: Request-Reply with Failure

Client Actor

Server Actor

JSON(cmd, id, msg)

(server work)

JSON("fail", id, msg)

Messaging Pattern: Request-Reply with Ack

Client Actor

Server Actor

(server work)

JSON("done", id, msg)

JSON(cmd, id, msg)

JSON("ack", id, msg)

Timer

Messaging Pattern: Request-Reply with Progress

Client Actor

Server Actor

(server work)

JSON("done", id, msg)JSON(cmd, id, msg)

(server work)(server work)

(server work)

JSON("progress", id, msg)JSON("progress", id, msg)

JSON("progress", id, msg)

Messaging Pattern: Request-Reply with Query

Client Actor

Server Actor

(server work)

JSON(cmd, id, msg)

(user input)

(server work)

JSON("query", id, msg)

JSON

(server work)

JSON("done", id, msg)

Repeat 2x

Messaging Pattern: Long Polling

Client Actor

Server Actor

(server work)

JSON("StopListen", id, msg)

JSON

JSON("StartListen", id, msg)

Messaging Patterns• More patterns:

• Sender retries on timeout or failure

• A -> B, B -> C, C responds to A directly

• Routers

• Suggestions for more patterns welcome!

Summary: Remote Actors or HTTP?

• Remote actors:

• Symmetric; uniform for local/remote

• Rich interaction patterns, very simple protocol

• Problems with NAT and load balancers

• HTTP:

• Asymmetric; remote-only

• Simple interaction patterns, complex protocol

• Ubiquitous

• Originally designed for a very different purpose

For More Information• Sample code: https://github.com/polymorphic/akka-

messaging-patterns.git

• Akka remoting: http://doc.akka.io/docs/akka/2.3.9/scala/remoting.html

• JLine http://jline.sourceforge.net

• PersistJson https://github.com/nestorpersist/json

• Effective Akka, Patterns and Best Practices by Jamie Allen

Thank you