gpt4 book ai didi

scala - 使用Akka Play 2.5-找不到参数超时的隐式值: akka. util.Timeout

转载 作者:行者123 更新时间:2023-12-04 13:31:21 25 4
gpt4 key购买 nike

我正在尝试用Pla​​y 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

我尝试绕过超时无济于事。有谁知道可能导致此编译错误的主意吗?

最佳答案

之所以会收到此错误,是因为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

关于scala - 使用Akka Play 2.5-找不到参数超时的隐式值: akka. util.Timeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35854812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com