package actor
import akka.actor.{Actor, ActorSystem, Props}
class HelloActor extends Actor{// 接受消息
override def receive: Receive = {
// 接受消息并处理
case "1" => println("11")
case "2" => println("22")
case "stop" => {
context.stop(self) // 停止自己的actorRef
context.system.terminate() // 关闭ActorSystem
}
}
}
object HelloActor {
private val nBFactory = ActorSystem("NBFactory")// 工厂
private val helloActorRef = nBFactory.actorOf(Props[HelloActor], "helloActor")
def main(args: Array[String]): Unit = {
// 给自己发送消息
helloActorRef ! "1"
helloActorRef ! "2"
helloActorRef ! "stop"
}
}
Scala Aka
最新推荐文章于 2024-04-16 19:26:41 发布