Akka actor 无法发回消息
Akka actor cannot send back the message
比如,我有一个 Actor,其接收函数喜欢
def receive = {
case Message =>
val doFuture: Future[String] = doSomething()
doFuture onSuccess {
case doResult =>
//////////// Here is the problem !! /////////////
// --> here fail. Seems sender cannot send back the result to the caller
sender ! doResult
}
doFuture onFailure {
// handle exception
}
}
为什么发件人不能再回信了?
def receive = {
case Message =>
val doFuture: Future[String] = doSomething()
val requester = sender
doFuture onSuccess {
case doResult =>
requester ! doResult
}
doFuture onFailure {
// handle exception
}
}
您可以保存原始发件人然后使用它(以防您出于某种原因不想使用管道)。不管怎样,管道看起来好多了,专门为这种情况设计的:
import akka.pattern.pipe
def receive = {
case Message =>
val doFuture: Future[String] = doSomething()
doFuture pipeTo sender
}
这就是 pipeTo
的用途:
import akka.pattern.pipe
...
doFuture.pipeTo(sender())
比如,我有一个 Actor,其接收函数喜欢
def receive = {
case Message =>
val doFuture: Future[String] = doSomething()
doFuture onSuccess {
case doResult =>
//////////// Here is the problem !! /////////////
// --> here fail. Seems sender cannot send back the result to the caller
sender ! doResult
}
doFuture onFailure {
// handle exception
}
}
为什么发件人不能再回信了?
def receive = {
case Message =>
val doFuture: Future[String] = doSomething()
val requester = sender
doFuture onSuccess {
case doResult =>
requester ! doResult
}
doFuture onFailure {
// handle exception
}
}
您可以保存原始发件人然后使用它(以防您出于某种原因不想使用管道)。不管怎样,管道看起来好多了,专门为这种情况设计的:
import akka.pattern.pipe
def receive = {
case Message =>
val doFuture: Future[String] = doSomething()
doFuture pipeTo sender
}
这就是 pipeTo
的用途:
import akka.pattern.pipe
...
doFuture.pipeTo(sender())