简体   繁体   English

在Akka演员之间传送未来作为讯息

[英]Sending a Future as a message between Akka actors

I'm invoking a web service that returns an Option[Future[String]] inside an Akka actor. 我正在调用一个Web服务,该Web服务在Akka actor中返回Option[Future[String]] Now I need to spin a new child actor and pass this Option[Future[String]] and stop the actor.In the child actor I want to wait for the future to complete so that I can do something with the result. 现在,我需要旋转一个新的子actor,并传递此Option[Future[String]]并停止actor。在子actor中,我要等待将来的完成,以便我可以对结果进行处理。 Is it okay to pass Futures around Akka actors? 在Akka演员周围传递Futures可以吗?

Warning: Not compiled, see doc.akka.io if in doubt. 警告:尚未编译,如有疑问,请参阅doc.akka.io。

import akka.pattern.pipeTo
def receive = {
  case something =>
    webService.call() foreach { _ pipeTo context.actorOf(Props[Child]) }
}

The idea is that you do not need the child actor if the web service doesn't return anything (None), and if it returns Some(future) you send the result of that Future to the newly created child actor. 这个想法是,如果Web服务不返回任何内容(无),则不需要子角色;如果它返回Some(将来),则可以将Future的结果发送给新创建的子角色。 The newly created child actor can then self-terminate once it receives the result of the webservice call. 新创建的子actor一旦收到webservice调用的结果,便可以自行终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM