Advertisement
Guest User

Become Unbecome example

a guest
Oct 7th, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.01 KB | None | 0 0
  1. import akka.actor.{ ActorRef, ActorSystem, Props, Actor, Inbox }
  2.  
  3. class HotSwapActor extends Actor {
  4.   import context._
  5.  
  6.   def one: PartialFunction[Any, Unit] = {
  7.     case "foo" => sender ! "I am One!"; become(two)
  8.   }
  9.  
  10.   def two: PartialFunction[Any, Unit] = {
  11.     case "foo" => sender ! "I am Two"; become(three)
  12.   }
  13.  
  14.   def three: PartialFunction[Any, Unit] = {
  15.     case "foo" => sender ! "I am Three"; unbecome
  16.   }
  17.  
  18.   def receive = {
  19.     case "foo" => sender ! "I am recieve and becomes one"; become(one)
  20.     case "bar" => sender ! "I am recieve";
  21.   }
  22. }
  23.  
  24. class OtherActor extends Actor {
  25.   val system = ActorSystem()
  26.   val actor = system.actorOf(Props[HotSwapActor])
  27.   def receive = {
  28.     case "start" =>
  29.       actor ! "foo" //nop
  30.       actor ! "foo" //one
  31.       actor ! "foo" //two
  32.       actor ! "foo" //three
  33.       actor ! "foo"
  34.     case a @ _ => println(a)
  35.   }
  36. }
  37.  
  38. object HotSwapMain extends App {
  39.   val system = ActorSystem()
  40.   val actor = system.actorOf(Props[OtherActor])
  41.   actor ! "start"
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement