Actor - Become/Unbecome

class HotSwapActor extends Actor {

import context._

def angry: Receive = {
case "foo" => sender() ! "I am already angry?"
case "bar" => become(happy)
case "ex" => throw new Exception("exception here")
case _ => println("angry")
}

def happy: Receive = {
case "bar" => sender() ! "I am already happy :-)"
case "foo" => become(angry)
case _ => println("happy")
}

def receive = {
case "foo" => become(angry)
case "bar" => become(happy)
case _ => println("default")
}
}

become是对消息处理集合的动态切换.
当actor restart的时候 状态恢复初始状态

val swap = system.actorOf(Props[HotSwapActor], "swap")

swap ! "xxx"
swap ! "foo"
swap ! "xxx"
swap ! "ex"
swap ! "xxx"
swap ! "bar"
swap ! "xxx"
default
angry
default
happy

unbecome就不介绍了,感觉一般用不上.还容易搞出内存泄漏