2

使用Akka 2.1.0我正在从一个 Actor ( ActorA) 向另一个Actor ( ) 发送消息,ActorB并期望返回的消息是Option[(String, String)]. ActorB有一个 val 定义为 anEnumeration并且这将作为Tuple结果的第二个元素返回。 ActorA使用Await.result().asInstanceOf[Option[(String, String)]模式(我知道这是不好的阻塞行为,但不认为这解释了我看到的行为),并将结果分配给 val. 后来,当我尝试提取结果的第二个元素时,Tuple我得到了一个强制转换scala.Enumeration#Val cannot be cast to java.lang.String异常。在以前的版本中Akka没有出现这种情况,并且Akka 2.1.0我预计该Await.result操作会爆炸。谁能解释这里发生了什么?

object MyEnumeration extends Enumeration {
  type Enum = Value
  val Foo = Value("foo")
  val Bar = Value("bar")
}

case class ActorA extends Actor {
  implicit val timeout = Timeout(10000)
  val result = Await.result((ActorB ? MyMessage), timeout.duration).asInstanceOf[Option[(String, String)]]

  val validResult = result.get
  val validType = validResult._2 // this is of type Enumeration#Val not String

}

case class ActorB extends Actor {
  def myType = MyEnumeration.Foo
  def receive = {
  case MyMessage =>
      sender ! Option((validString, myType))
  }
}
4

1 回答 1

0

它实际上在 Akka 2.0.3 中不起作用。据我了解,您以不同方式解析收到的消息。

// enum
object E extends Enumeration { val a,b,c = Value }

// send 
case "Hello" => sender ! Some(("Hai", E.a))

// works
Await.result(rootActor ? "Hello", timeout.duration)
.asInstanceOf[Option[(String, String)]] match {
  case a: Option[Tuple2[_, _]] => // diff is here 
    println("" + a.get._2)
    a.get._2
}  


// does not work
Await.result(rootActor ? "Hello", timeout.duration)
.asInstanceOf[Option[(String, String)]] match {
  case a: Option[Tuple2[String, String]] => // diff is here
    println("" + a.get._2)
    a.get._2
}  
于 2013-01-30T09:18:00.627 回答