并发编程模型AKKA
2019-06-03 本文已影响0人
iarchitect
Actor模型快速入门
应用实例需求
1)编写一个Actor,可以给自己发消息
SayHelloActor.java
package com.smart
import akka.actor.{Actor, ActorSystem, Props}
class SayHelloActor extends Actor{
override def receive: Receive = {
case "hello" => println("收到hello,回应hello too")
case "ok" => println("收到ok,回应ok too")
case "exit" => {
println("收到退出指令")
context.stop(self)
context.system.terminate()
}
case _ => println("匹配不到")
}
}
object SayHelloActorDemo{
private val actorFactory = ActorSystem("actoryFactory")
private val sayHelloActorRef = actorFactory.actorOf(Props[SayHelloActor],"sayHelloActor")
def main(args: Array[String]): Unit = {
sayHelloActorRef ! "hello"
sayHelloActorRef ! "exit"
}
}
运行结果
result.png