[英]could not find implicit value for parameter timeout: akka.util.Timeout
I have this code, where I am making an ask request in Scala: 我有以下代码,在Scala中提出询问请求:
someActorRef ? SomeMessage()
However, I'm getting this message: 但是,我收到此消息:
could not find implicit value for parameter timeout: akka.util.Timeout
I also tried this: 我也试过这个:
Await.ready(someActorRef ? SomeMessage(), Duration("3 seconds")).asInstanceOf[String]
But I get the same message. 但是我得到了同样的信息。
Anyway, I don't want to block. 无论如何,我不想阻止。 I want to get a
Future
and then later on give it an onComplete
callback. 我想要一个
Future
,然后再给它一个onComplete
回调。
Can I ask for a message from another actor without blocking? 我可以要求其他演员发信息而不加阻挡吗?
ask
needs an implicit Timeout
, after which it will just fail the Future with a TimeoutException
. ask
需要一个隐式的Timeout
,之后它只会通过TimeoutException
使Future失败。
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.duration._
implicit val timeout = Timeout(5 seconds)
val f = someActorRef ? SomeMessage()
Note that nothing will block for those 5 seconds, the ask
pattern is fully async/non-blocking. 请注意,在这5秒钟内没有任何内容会阻塞,
ask
模式是完全异步/非阻塞。 It will give you back a Future
, on which you can block (NOT RECOMMENDED) or attach a callback (as you wish) 它将带给您
Future
,您可以在其上阻止(不推荐)或附加回调(如您所愿)
f.onComplete(doSomething(_))
You can. 您可以。 But don't use
ask
. 但是不要用
ask
。 Just pipe
result to the sender. 只是
pipe
导致给发件人。 Eg let's say this is an actor which performs long running task and returns a future: 例如,假设这是一个执行长期任务并返回未来的演员:
class LongRunningTaskPerformer extends Actor {
import context.dispatcher
override def receive: Receive = {
case PerformTask => someLongRunningTask() pipeTo sender()
}
}
class QueryPerformer extends Actor {
val longRunningTaskPerformer = context.actorOf(LongRunningTaskPerformer.props())
override def receive: Receive = {
case TriggerLongRunningTask => longRunningTaskPerformer ! PerformTask
case result @ Result => // do something with your result
}
}
In this case, I'm sending a message PerformTask
to LongRunningTaskPerformer
actor. 在这种情况下,我正在向
LongRunningTaskPerformer
actor发送一条消息PerformTask
。 When the future is being completed, result will be sent to my QueryPerformer
. 当将来完成时,结果将发送到我的
QueryPerformer
。 No blocking. 无阻塞。 Easy.
简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.