错误:找不到参数超时的隐式值:akka.util.Timeout

时间:2017-07-05 05:17:22

标签: scala akka-http

我有这个代码,我在Scala中提出请求请求:

 implicit val result =system.actorOf(Props(new TestActor("TestingName")),name = "Scala")

   val future3:Future[String]= ask(result ,Message).mapTo[String]
  val results = Await.result(future3, 5 second)

这里是我的导入语句

import scala.concurrent.{Await, Future}
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import akka.util.Timeout
import akka.pattern.ask
import scala.concurrent.Await
import scala.concurrent.duration._

我收到错误消息,如

错误:(42,35)找不到参数超时的隐含值:akka.util.Timeout    val future3:Future [String] = ask(result,Message).mapTo [String]

错误:(42,35)没有足够的方法请求:(隐式超时:akka.util.Timeout)scala.concurrent.Future [Any]。 未指定的值参数超时。    val future3:Future [String] = ask(result,Message).mapTo [String]

1 个答案:

答案 0 :(得分:2)

如果您查看ask?的签名,您会看到:

def ask(message: Any)(implicit timeout: Timeout, 
                      sender: ActorRef = Actor.noSender): Future[Any]

它采用隐式timeout: Timeout参数,告诉将来何时超时。致电ask时,您必须将其纳入范围:

import scala.concurrent.duration._
import akka.util.Timeout

implicit val timeout: Timeout = Timeout(5 seconds)
val future3: Future[String]= ask(result, Message).mapTo[String]