我正试图用Play 2.5测试Akka,我遇到了编译错误,我似乎无法绕过。
我在Play文档中关注此页面: https://playframework.com/documentation/2.5.x/ScalaAkka
以下是完整代码:
package controllers
import javax.inject.{Inject, Singleton}
import akka.actor.ActorSystem
import controllers.HelloActor.SayHello
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.duration._
import akka.pattern.ask
@Singleton
class Application @Inject()(system: ActorSystem) extends Controller {
implicit val timeout = 5.seconds
val helloActor = system.actorOf(HelloActor.props, "hello-actor")
def sayHello(name: String) = Action.async {
(helloActor ? SayHello(name)).mapTo[String].map { message =>
Ok(message)
}
}
}
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
}
}
我的路线如下:
GET /:name controllers.Application.sayHello(name: String)
最后,我的build.sbt:
name := "AkkaTest"
version := "1.0"
lazy val `akkatest` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq( jdbc , cache , ws , specs2 % Test )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
routesGenerator := InjectedRoutesGenerator
当我尝试运行它时,我收到以下编译错误:
could not find implicit value for parameter timeout: akka.util.Timeout
我试过移动超时无济于事。有没有人知道什么可能导致这个编译错误?
答案 0 :(得分:11)
您收到此错误是因为 ask模式需要隐式超时询问(如果此时未收到任何答复,它将在TimeoutException
完成未来)。所以你需要的是在sayHello
方法中创建一个隐含的本地值,如下所示:
import akka.util.Timeout
import scala.concurrent.duration.Duration
// ...
def sayHello(name: String) = Action.async {
implicit val timeout: Timeout = Duration.Infinite
(helloActor ? SayHello(name)).mapTo[String].map { message =>
Ok(message)
}
}
您可以使用以下语法指定有限的超时,而不是指定无限超时:
import scala.concurrent.duration._
import akka.util.Timeout
implicit val duration: Timeout = 20 seconds