【发布时间】:2015-10-20 10:06:13
【问题描述】:
我正在尝试复制Integrating with Akka, Play 2.4 for Scala doc 中提出的基本示例。但是我很难将最后的部分放在一起......
我已经使用以下代码在app/actors/HelloActor.scala 定义了演员(参见Writing actors 段):
package actors
import akka.actor._
object HelloActor {
def props = Props[HelloActor]
case class SayHello(name: String)
}
class HelloActor extends Actor {
import HelloActor._
def receive = {
case SayHello(name: String) =>
sender() ! "Hello, " + name
}
}
那么(请参阅Creating and using actors)我想我应该在app/controllers/Hello.scala 创建一个控制器,例如:
package controllers
import play.api.mvc._
import akka.actor._
import javax.inject._
import actors.HelloActor
@Singleton
class Hello @Inject() (system: ActorSystem) extends Controller {
val helloActor = system.actorOf(HelloActor.props, "hello-actor")
...
}
问题:我在哪里以及如何使用以下段落Asking things of actors 中的代码来获得有效的解决方案?我曾尝试将其添加到上述Hello.scala 控制器但没有成功。
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.duration._
import akka.pattern.ask
implicit val timeout = 5.seconds
def sayHello(name: String) = Action.async {
(helloActor ? SayHello(name)).mapTo[String].map { message =>
Ok(message)
}
}
【问题讨论】:
-
它应该按预期工作,你有什么错误?
-
with
implicit val timeout = 5.seconds我摇摇晃晃地收到以下错误value seconds is not a member of Int Note: implicit value timeout is not applicable here because it comes after the application point and it lacks an explicit result type加上我不是 100% 确定如何将所有东西放在控制器中
标签: scala akka playframework-2.4