Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
Akka Actor
Introduction
Gene
Akka
The name comes from the goddess in the Sami
(Native swedes) mythology that represents all
the wisdom and beauty in th...
Agenda
1. Akka Introduction
2. Core Operations
3. WebSocket with Actor
4. Remote Actor
Typesafe
Toolkit/Library(*.jar)
Programming Language
Web Application Framework
Scala
• Functional as well as Object-Oriented
• Scala is compatible with Java
• Scala is compiled to Java byte-codes and r...
Java
Scala
1.Akka Introduction
Akka
•一個 JVM 上 Actor Model 的實作
1. Concurrency
2. Distribution
3. Fault-tolerance
Actors
Remoting
Supervision
Object Actor
Java AKKA
Object
Java AKKA
Object MailboxMailbox
Message
Message
Message
Concurrent programming
Concurrent
Parallel
Java
Thread Thread
Shared
Memory
R/W R/W
Java
Thread Thread
Shared
Memory
Locks Waits
Java
Thread Thread
Shared
Memory
Shared
Memory
Locks
Locks
Waits
Waits
Dead Lock !
How to avoid?
Shared state
Java
Threads
Lockers
Global variables,
Static variables
Java
implements
Runnable{}
synchronized(lock);….
Global variables,
Static variables
Java
implements
Runnable{}
synchronized(lock);….
Immutable object
Wait()/Notify()/Nofif...
AKKA
Actor Actor
Actor
Message
Message
Message
Java
Global variables,
Static variables
implements
Runnable{}
synchronized(...
AKKA
Message flow
Java
Global variables,
Static variables
implements
Runnable{}
synchronized(lock);….
Immutable object
Wai...
Actor
MailboxMailbox
State
Message
Message
Message
Actor having
1. Behavior (Processing)
2. State (Storage)
3. Mailbox (Me...
Messages are in mailbox.
Actor
MailboxMailbox
Behavior
State
Message
Message
Message
Actor
MailboxMailbox
Thread is allocated to the Actor.
 It has read message and is applying
behavior (in OnReceive() fun...
AKKA
 Actor has handled message*.
Thread is deallocated.
*One message is executed at a time
& messages are processed seq...
Dispatcher
Dispatcher
4 types of dispatchers
1. Dispatcher (default)
2. Pinned dispatcher
3. Balancing dispatcher (Deprecated*)
4. Calling threa...
Router
Router Types
• RoundRobinPool & RoundRobinGroup
• RandomPool & RandomGroup
• BalancingPool- shared mailbox
• SmallestMailb...
2.Core Operations
5 Core Actor Operations
0. Define → Define Actors
1. Create → Create new Actors
2. Send → Send messages to other Actors
3....
0.DEFINE
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.japi.Pro...
package controllers;
import akka.actor.ActorRef;
import akka.actor.Props;
import play.libs.Akka;
import play.mvc.*;
public...
package controllers;
import akka.actor.ActorRef;
import akka.actor.Props;
import play.libs.Akka;
import play.mvc.*;
public...
3 ways to sending messages
1. Fire-Forget
2. Ask and Reply
3. Forward
 Tell
 Ask
 Forward
Tell
Target.tell(message, sender);
ActorRef ActorRefObject
1. null
2. ActorRef.noSender()
3. getSelf()
4. …
A Bmessage
1. ...
Target.tell(message, sender);
Tell
public void onReceive(Object message){
if (message instanceof String) {
log.info((Strin...
Tell
ActorRef ActorRefObject
A Bmessage
B.tell(new Person(“David”,”Chang”),getSelf());
EXAMPLE:
B.tell(“Hello Actor”,Actor...
Ask
Future<Object> rt = Patterns.ask(Target, message, timeout);
A B
message
reply
getSender().tell(reply_message, getSelf(...
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
public class Hel...
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.japi.Procedure;
p...
Forward
ActorContext
A B
forward
Target.forward(message, getContext());
C
Target.tell(message, getSender());
ActorRef
Targ...
3.BECOME
getContext().become(Procedure<Object>);
1.Dynamically redefines actor behavior
2.Reactively triggered by message
...
BECOME
public class HotSwapActor extends UntypedActor {
Procedure<Object> angry = new Procedure<Object>() {
@Override
publ...
public class HotSwapActor extends UntypedActor {
Procedure<Object> angry = new Procedure<Object>() {
@Override
public void...
Hierarchy
• Actors can form hierarchies
Akka System
Default Actor
Supervisor
Actor
Child
Actor
ActorRef supervisor = Akka....
3.WebSocket with Actor
PlayFramework 2
WebSocket with Actor
Actor
WebSocket Actor
messages
Client
websocketHTTP
WebSocket with Actor
• Controller
• Routes
• URL
import play.mvc.WebSocket;
public class Application extends Controller {
...
public class ChatWebSocketActor extends UntypedActor {
LoggingAdapter log = Logging.getLogger(getContext().system(), this)...
WebSocket with Actor
out
handler
akka://application/system/websockets/123
akka://application/system/websockets/123/handler...
WebSocket with Actor
• Send websocket message to client
• Closing a websocket
out.tell(message, null);
out
handler
out.tel...
WebSocket Actors
Chat Actor
out
handler
out
handler
out
handler
out
handler
Chat
Client-1 Client-2 Client-3 Client-4
webso...
WebSocket Actors
Chat flow – (1)
out
handler
out
handler
out
handler
out
handler
Chat
Client-1 Client-2 Client-3 Client-4
...
WebSocket Actors
Chat flow – (2)
out
handler
out
handler
out
handler
out
handler
Chat
Client-1 Client-2 Client-3 Client-4
...
WebSocket Actors
Chat flow – (3)
out
handler
out
handler
out
handler
out
handler
Chat
Client-1 Client-2 Client-3 Client-4
...
4.Remote Actor
Remote Actors
Actor
B
messageActor
A
192.168.0.2192.168.0.1
 akka.tcp://application@192.168.0.1/user/ActorA  akka.tcp://...
Preparing your ActorSystem for Remoting
Akka System
Default Actor
Supervisor
Actor
Child
Actor
akka://application/user
akk...
Preparing your ActorSystem for Remoting
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
enabled-...
Remote Actors
Play Framework 2 Play Framework 2
Client
Actor
B
Controller
View
HTTP GET
What’s your name?
Gene
The name is...
Send Messages to Remote Actors
ActorSelection selection =
Akka.system().actorSelection("akka.tcp://application@127.0.0.1:7...
Creating Actor Remotely
remote
Actor
CreateActor
A
192.168.0.2192.168.0.1
 akka.tcp://application@192.168.0.1/user/ActorA...
Creating Actor Remotely – How to create?
remote
Actor
CreateActor
A
 Both server must have same Actor class to be remote
...
Remote
Actor
Create
Actor
A
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider“
deployment {
/remoteActor {
rem...
Creating Actor Remotely – How to create?
remote
Actor
CreateActor
A
 Use actorOf () to create a remote actor on Server A....
Another way to Create Actor Remotely
import akka.actor.ActorSelection;
import akka.actor.Address;
import akka.actor.Addres...
Remote Actors
name := """hello2"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayJava...
THANKS
Akka Actor presentation
Próxima SlideShare
Cargando en…5
×

Akka Actor presentation

3.146 visualizaciones

Publicado el

Intro to Akka Actor (PlayFramework in Java)

Publicado en: Software
  • Sé el primero en comentar

Akka Actor presentation

  1. 1. Akka Actor Introduction Gene
  2. 2. Akka The name comes from the goddess in the Sami (Native swedes) mythology that represents all the wisdom and beauty in the world. It’s also the name of a beautiful mountain in Laponia in the north part of Sweden. Akka
  3. 3. Agenda 1. Akka Introduction 2. Core Operations 3. WebSocket with Actor 4. Remote Actor
  4. 4. Typesafe Toolkit/Library(*.jar) Programming Language Web Application Framework
  5. 5. Scala • Functional as well as Object-Oriented • Scala is compatible with Java • Scala is compiled to Java byte-codes and run on Java Virtual Machine
  6. 6. Java Scala
  7. 7. 1.Akka Introduction
  8. 8. Akka •一個 JVM 上 Actor Model 的實作 1. Concurrency 2. Distribution 3. Fault-tolerance Actors Remoting Supervision
  9. 9. Object Actor Java AKKA
  10. 10. Object Java AKKA Object MailboxMailbox Message Message Message
  11. 11. Concurrent programming
  12. 12. Concurrent Parallel
  13. 13. Java Thread Thread Shared Memory R/W R/W
  14. 14. Java Thread Thread Shared Memory Locks Waits
  15. 15. Java Thread Thread Shared Memory Shared Memory Locks Locks Waits Waits Dead Lock ! How to avoid?
  16. 16. Shared state Java Threads Lockers
  17. 17. Global variables, Static variables Java implements Runnable{} synchronized(lock);….
  18. 18. Global variables, Static variables Java implements Runnable{} synchronized(lock);…. Immutable object Wait()/Notify()/NofifyALL() Thread Safe
  19. 19. AKKA Actor Actor Actor Message Message Message Java Global variables, Static variables implements Runnable{} synchronized(lock);…. Immutable object Wait()/Notify()/NofifyALL() Thread Safe
  20. 20. AKKA Message flow Java Global variables, Static variables implements Runnable{} synchronized(lock);…. Immutable object Wait()/Notify()/NofifyALL() Thread Safe
  21. 21. Actor MailboxMailbox State Message Message Message Actor having 1. Behavior (Processing) 2. State (Storage) 3. Mailbox (Message Queue) *State is not shared, only accessible through…messages passing. Behavior
  22. 22. Messages are in mailbox. Actor MailboxMailbox Behavior State Message Message Message
  23. 23. Actor MailboxMailbox Thread is allocated to the Actor.  It has read message and is applying behavior (in OnReceive() function). Behavior State Message Message Message
  24. 24. AKKA  Actor has handled message*. Thread is deallocated. *One message is executed at a time & messages are processed sequentially . MailboxMailbox Message Message Behavior State
  25. 25. Dispatcher
  26. 26. Dispatcher
  27. 27. 4 types of dispatchers 1. Dispatcher (default) 2. Pinned dispatcher 3. Balancing dispatcher (Deprecated*) 4. Calling thread dispatcher *Instead by BalancingPool of Router.
  28. 28. Router
  29. 29. Router Types • RoundRobinPool & RoundRobinGroup • RandomPool & RandomGroup • BalancingPool- shared mailbox • SmallestMailboxPool • BroadcastPool & BroadcastGroup • ScatterGatherFirstCompletedPool & ScatterGatherFirstCompletedGroup • TailChoppingPool & TailChoppingGroup • ConsistentHashingPool & ConsistentHashingGroup
  30. 30. 2.Core Operations
  31. 31. 5 Core Actor Operations 0. Define → Define Actors 1. Create → Create new Actors 2. Send → Send messages to other Actors 3. Become → Change the behavior for handling the next message 4. Supervise → Manage another Actors failure
  32. 32. 0.DEFINE import akka.actor.UntypedActor; import akka.event.Logging; import akka.event.LoggingAdapter; import akka.japi.Procedure; public class AnActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message){ if (message instanceof String) { log.info((String) message); }else{ unhandled(message); log.info("Unhandled message"); } } }
  33. 33. package controllers; import akka.actor.ActorRef; import akka.actor.Props; import play.libs.Akka; import play.mvc.*; public class HelloActor extends Controller { public static Result index() { ActorRef actor = Akka.system().actorOf(Props.create(AnActor.class)); // insert stuff actor.tell(message) return ok("ok"); } } 1.CREATE
  34. 34. package controllers; import akka.actor.ActorRef; import akka.actor.Props; import play.libs.Akka; import play.mvc.*; public class HelloActor extends Controller { public static Result index() { ActorRef actor = Akka.system().actorOf(Props.create(AnActor.class)); actor.tell("Hello Actor!!", null); return ok("ok"); } } 2.SEND [INFO] [03/13/2015 22:14:01.442] [application-akka.actor.default-dispatcher-2] [akka://application/user/$a] Hello Actor!!
  35. 35. 3 ways to sending messages 1. Fire-Forget 2. Ask and Reply 3. Forward  Tell  Ask  Forward
  36. 36. Tell Target.tell(message, sender); ActorRef ActorRefObject 1. null 2. ActorRef.noSender() 3. getSelf() 4. … A Bmessage 1. To send a message to an actor, you need a Actor reference 2. Asynchronous and Non-blocking (Fire-and-forget) Target
  37. 37. Target.tell(message, sender); Tell public void onReceive(Object message){ if (message instanceof String) { log.info((String) message); log.info("getSender()="+getSender()); } } A Bmessage Target
  38. 38. Tell ActorRef ActorRefObject A Bmessage B.tell(new Person(“David”,”Chang”),getSelf()); EXAMPLE: B.tell(“Hello Actor”,ActorRef.noSender()); Target.tell(message, sender); Target
  39. 39. Ask Future<Object> rt = Patterns.ask(Target, message, timeout); A B message reply getSender().tell(reply_message, getSelf()); String result = Await.result(rt , timeout.duration); 1 2 3 Target
  40. 40. import scala.concurrent.Await; import scala.concurrent.Future; import scala.concurrent.duration.Duration; public class HelloActor extends Controller { public static Result index() { ActorRef actor = Akka.system().actorOf(Props.create(AnActor.class)); final Timeout timeout = new Timeout(Duration.create(1, SECONDS)); Future<Object> rt = Patterns.ask(actor,"What's your name?", timeout); try { String result = (String) Await.result(rt, timeout.duration()); System.out.println("The name is "+result); return ok("The name is "+result); } catch (Exception e) { System.out.println(e); } return ok(""); } } Ask The Name is David
  41. 41. import akka.actor.UntypedActor; import akka.event.Logging; import akka.event.LoggingAdapter; import akka.japi.Procedure; public class AnActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message){ if(message.equals("What's your name?")){ getSender().tell("David",getSelf()); }else if (message instanceof String) { log.info((String ) message); }else{ unhandled(message); log.info("Unhandled message"); } } } Ask The Name is David
  42. 42. Forward ActorContext A B forward Target.forward(message, getContext()); C Target.tell(message, getSender()); ActorRef Target
  43. 43. 3.BECOME getContext().become(Procedure<Object>); 1.Dynamically redefines actor behavior 2.Reactively triggered by message 3.Behaviors are stacked & can be pushed and popped  getContext().unbecome();
  44. 44. BECOME public class HotSwapActor extends UntypedActor { Procedure<Object> angry = new Procedure<Object>() { @Override public void apply(Object message) { if (message.equals(“work")) { getSender().tell(“I am angry ",getSelf()); } else if (message.equals(“play")) { getContext().become(happy); } } }; Procedure<Object> happy = new Procedure<Object>() { @Override public void apply(Object message) { if (message.equals(“play")) { getSender().tell("I am happy ", getSelf()); } else if (message.equals(“work")) { getContext().become(angry); } } }; public void onReceive(Object message) { if (message.equals(“work")) { getContext().become(angry); } else if (message.equals(“play")) { getContext().become(happy); } else { unhandled(message); } } } public void onReceive(Object message) { if (message.equals(“work")) { getContext().become(angry); } else if (message.equals(“play")){ getContext().become(happy); } else { unhandled(message); } } }
  45. 45. public class HotSwapActor extends UntypedActor { Procedure<Object> angry = new Procedure<Object>() { @Override public void apply(Object message) { if (message.equals(“work")) { getSender().tell(“I am angry ",getSelf()); } else if (message.equals(“play")) { getContext().become(happy); } } }; Procedure<Object> happy = new Procedure<Object>() { @Override public void apply(Object message) { if (message.equals(“play")) { getSender().tell("I am happy ", getSelf()); } else if (message.equals(“work")) { getContext().become(angry); } } }; public void onReceive(Object message) { if (message.equals(“work")) { getContext().become(angry); } else if (message.equals(“play")) { getContext().become(happy); } else { unhandled(message); } } } BECOME Procedure<Object> angry = new Procedure<Object>() { @Override public void apply(Object message) { if (message.equals(“work")) { getSender().tell("I am angry ", getSelf()); } else if (message.equals(“play")) { getContext().become(happy); } } };
  46. 46. Hierarchy • Actors can form hierarchies Akka System Default Actor Supervisor Actor Child Actor ActorRef supervisor = Akka.system().actorOf(Props.create(SupervisorActor.class), “Supervisor”); ActorRef child = getContext().actorOf(Props.create(ChildActor.class), “Child”); akka://application/user akka://application/user/Supervisor akka://application/user/Supervisor/Child parent child
  47. 47. 3.WebSocket with Actor
  48. 48. PlayFramework 2 WebSocket with Actor Actor WebSocket Actor messages Client websocketHTTP
  49. 49. WebSocket with Actor • Controller • Routes • URL import play.mvc.WebSocket; public class Application extends Controller { public static WebSocket<JsonNode> chat(final String username) { return WebSocket.withActor(new Function<ActorRef, Props>() { public Props apply(ActorRef out) throws Throwable { return ChatWebSocketActor.props(out, username); } }); } } GET /room/chat controllers.Application.chat(username) ws://127.0.0.1:9000/room/chat?username=XXX
  50. 50. public class ChatWebSocketActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public static Props props(ActorRef out, String username) { return Props.create(ChatWebSocketActor.class, out, username); } private final ActorRef out; private final String username; public ChatWebSocketActor(ActorRef out,String username) { this.out = out; this.username = username; } public void preStart(){ //do something } public void onReceive(Object message) throws Exception { //do something } public void postStop() throws Exception { //do something } } WebSocket with Actor Receive websocket message
  51. 51. WebSocket with Actor out handler akka://application/system/websockets/123 akka://application/system/websockets/123/handler parent child Send websocket message Receive websocket message In child: getContext().parnet()
  52. 52. WebSocket with Actor • Send websocket message to client • Closing a websocket out.tell(message, null); out handler out.tell(PoisonPill.getInstance(), self());
  53. 53. WebSocket Actors Chat Actor out handler out handler out handler out handler Chat Client-1 Client-2 Client-3 Client-4 websocket messages
  54. 54. WebSocket Actors Chat flow – (1) out handler out handler out handler out handler Chat Client-1 Client-2 Client-3 Client-4 websocket
  55. 55. WebSocket Actors Chat flow – (2) out handler out handler out handler out handler Chat Client-1 Client-2 Client-3 Client-4 message
  56. 56. WebSocket Actors Chat flow – (3) out handler out handler out handler out handler Chat Client-1 Client-2 Client-3 Client-4 websocket
  57. 57. 4.Remote Actor
  58. 58. Remote Actors Actor B messageActor A 192.168.0.2192.168.0.1  akka.tcp://application@192.168.0.1/user/ActorA  akka.tcp://application@192.168.0.2/user/ActorB
  59. 59. Preparing your ActorSystem for Remoting Akka System Default Actor Supervisor Actor Child Actor akka://application/user akka://application/user/Supervisor akka://application/user/Supervisor/Child akka://application@127.0.0.1/user akka://application@127.0.0.1/user/Supervisor akka://application@127.0.0.1/user/Supervisor/Child Local Path Remote Path •Each Actor has a Path, but an ActorSystem can be publish in an Address.
  60. 60. Preparing your ActorSystem for Remoting akka { actor { provider = "akka.remote.RemoteActorRefProvider" } remote { enabled-transports = ["akka.remote.netty.tcp"] netty.tcp { hostname = "127.0.0.1" port = 7100 } } } Play Framework AKKA Port 7100 Port 7000Web
  61. 61. Remote Actors Play Framework 2 Play Framework 2 Client Actor B Controller View HTTP GET What’s your name? Gene The name is Gene Reponse  akka.tcp://application@127.0.0.1:7100/user/ActorB
  62. 62. Send Messages to Remote Actors ActorSelection selection = Akka.system().actorSelection("akka.tcp://application@127.0.0.1:7100/user/ActorB"); selection.tell("Hello Remote",null); Future<Object> rt = Patterns.ask(selection,"What's your name?", timeout); Retrieve remote actor Tell message Ask message
  63. 63. Creating Actor Remotely remote Actor CreateActor A 192.168.0.2192.168.0.1  akka.tcp://application@192.168.0.1/user/ActorA  akka.tcp://application@192.168.0.2/user/ActorB Server A Server B
  64. 64. Creating Actor Remotely – How to create? remote Actor CreateActor A  Both server must have same Actor class to be remote Server A Server B 1
  65. 65. Remote Actor Create Actor A akka { actor { provider = "akka.remote.RemoteActorRefProvider“ deployment { /remoteActor { remote = "akka.tcp://application@127.0.0.1:9100" } } } remote { enabled-transports = ["akka.remote.netty.tcp"] netty.tcp { hostname = "127.0.0.1" port = 7100 } } } akka { actor { provider = "akka.remote.RemoteActorRefProvider } remote { enabled-transports = ["akka.remote.netty.tcp"] netty.tcp { hostname = "127.0.0.1" port = 9100 } } } for server A for server B 2  Amend the application.conf file for Server A Server A Server B
  66. 66. Creating Actor Remotely – How to create? remote Actor CreateActor A  Use actorOf () to create a remote actor on Server A. Server A Server B 3 ActorRef actor = Akka.system().actorOf(Props.create(remoteActor.class), "remoteActor"); actor.tell("Hello Remote",null);
  67. 67. Another way to Create Actor Remotely import akka.actor.ActorSelection; import akka.actor.Address; import akka.actor.AddressFromURIString; import akka.actor.Deploy; import akka.remote.RemoteScope; public class HelloActor extends Controller { public static Result index() { Address addr = AddressFromURIString.parse("akka.tcp://application@127.0.0.1:9100"); ActorRef actor = Akka.system().actorOf(Props.create(remoteActor.class).withDeploy( new Deploy(new RemoteScope(addr)))); actor.tell("Hello Remote",null); } } for server A
  68. 68. Remote Actors name := """hello2""" version := "1.0-SNAPSHOT" lazy val root = (project in file(".")).enablePlugins(PlayJava) scalaVersion := "2.11.1" libraryDependencies ++= Seq( javaJdbc, javaEbean, cache, javaWs ) libraryDependencies += "com.typesafe.akka" %% "akka-remote" % "2.3.9"
  69. 69. THANKS

×