使用Akka播放2.5-找不到参数超时的隐式值:akka.util.Timeout [英] Play 2.5 with Akka - could not find implicit value for parameter timeout: akka.util.Timeout

查看:76
本文介绍了使用Akka播放2.5-找不到参数超时的隐式值:akka.util.Timeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Pla​​y 2.5进行Akka测试,但遇到了似乎无法解决的编译错误.

I'm attempting to test out Akka with Play 2.5 and I'm running into a compile error I can't seem to get around.

我正在关注Play文档中的以下页面: https://playframework.com/documentation/2.5.x/ScalaAkka

I'm following this page from the Play docs: 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:

And, finally, my 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

当我尝试运行此代码时,出现以下编译错误:

When I attempt to run this, I get the following compilation error:

could not find implicit value for parameter timeout: akka.util.Timeout

我尝试绕过超时无济于事.有谁知道什么可能导致此编译错误?

I've tried moving around the timeout to no avail. Does anyone have an idea what might cause this compilation error?

推荐答案

您会收到此错误,因为询问模式需要一个隐式的超时时间(如果没有答案,它将在将来以TimeoutException完成目前已收到).因此,您所需要做的就是在sayHello方法中创建一个隐式本地值,如下所示:

You getting this error because the ask pattern requires an implicit timeout for asking (it will complete future with TimeoutException if no answer was received for this time). So all you need is to create an implicit local value in sayHello method like this:

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)
    }
  }

您可以使用以下语法指定一个有限超时,而不是指定无限超时:

Instead of specifying infinite timeout you can specifiy a finite one with following syntax:

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

implicit val duration: Timeout = 20 seconds

这篇关于使用Akka播放2.5-找不到参数超时的隐式值:akka.util.Timeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆