5

这是我的代码:

class testActor extends Actor   {
    var test = "test2"
    def receive = {
            case "test" ⇒ 
                    test="works"
                    "works"

    }
}


 def test = Action {
    var test = "test"
    val system = ActorSystem("MySystem")
    val myActor = system.actorOf(Props[testActor.testActor], name = "testActor")

    myActor ! "test"

    test = myActor.test

Ok(views.html.test(test))
}

该行: test = myActor.test 不起作用。

我要么需要一种方法来访问 Actor 函数返回的内容,在这种情况下是“有效的”,要么需要一种访问 Actor 内部变量的方法。

4

1 回答 1

9

要将结果返回给发件人,请向其发送一条消息:

def receive = {
  case "test" => sender ! "works"
}

等待响应使用 Await.result() 调用:

  implicit val timeout = Timeout(Duration(1, TimeUnit.SECONDS))
  test = Await.result(myActor ? "test", Duration(1, TimeUnit.SECONDS))
于 2012-07-13T20:53:01.283 回答