在akka actor示例中的Terminated方法中找不到的值

时间:2014-08-09 07:42:06

标签: scala akka actor

嗨,我是scala和akka的新手,我正在创建一个在处理消息后终止的actor,但我在PoisonPill和Terminated案例中出错,它给出了未找到的值错误 我很困惑,如果我去掉它,那么参数类型应该是什么

import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props

case class Greet(name: String)
case class Praise(name: String)
case class Celebrate(name: String, age: Int)

class Talker extends Actor {
  def receive = {
    case Greet(name)=>println(s"Hello $name")
    case Praise(name) => println(s"$name, you're amazing")
    case Celebrate(name, age) => println(s"Here's to another $age years, $name")
  }
}

object HelloActors extends App {
  val system = ActorSystem("HelloActors")
  system.actorOf(Props[Master], "master")
}

class Master extends Actor {
  val talker = context.actorOf(Props[Talker], "talker")
  override def preStart {
    context.watch(talker)
    talker ! Greet("Huey")
    talker ! Praise("Dewey")
    talker ! Celebrate("Louie", 16)
    talker ! PoisonPill
  }

  def receive = {
    case Terminated(`talker`) => context.system.shutdown
  }
}

这是错误

  

[错误] /home/ahsen/SbtPrctc/PoisionPill/src/main/scala/HelloActors.scala:27​​:未找到:值PoisonPill   [错误]说话者! PoisonPill   [错误] ^   [错误] /home/ahsen/SbtPrctc/PoisionPill/src/main/scala/HelloActors.scala:30:找不到:值已终止   [error] case Terminated(talker)=> context.system.shutdown   [错误] ^   [错误]发现两个错误

1 个答案:

答案 0 :(得分:1)

您需要导入PoisonPill对象:

import akka.actor.{Props, Actor, PoisonPill, ActorSystem, Terminated}

或者:

import akka.actor._