Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 从Akka演员那里得到消息_Scala_Akka_Actor - Fatal编程技术网

Scala 从Akka演员那里得到消息

Scala 从Akka演员那里得到消息,scala,akka,actor,Scala,Akka,Actor,我构建了一个Akka actor,它定期查询API,如下所示: val cancellable = system.scheduler.schedule(0 milliseconds, 5 seconds, actor, QueryController(1)) 参与者本质上是: object UpdateStatistics { /** * Query the controller for the given switch Id *

我构建了一个Akka actor,它定期查询API,如下所示:

  val cancellable =
    system.scheduler.schedule(0 milliseconds,
      5 seconds,
      actor,
      QueryController(1))
参与者本质上是:

object UpdateStatistics {
  /**
   * Query the controller for the given switch Id
   *
   * @param dpId Switch's Id
   */
  case class QueryController(dpId: Int)
  case object Stop

  def props: Props = Props[UpdateStatistics]
}

class UpdateStatistics extends Actor with akka.actor.ActorLogging {
  import UpdateStatistics._

  def receive = {

    case QueryController(id) =>
      import context.dispatcher
      log.info(s"Receiving request to query controller")
      Future { FlowCollector.getSwitchFlows(1) } onComplete {
        f => self ! f.get
      }
    case Stop =>
      log.info(s"Shuting down")
      context stop self
    case json: JValue =>
      log.info("Getting json response, computing features...")
      val features = FeatureExtractor.getFeatures(json)
      log.debug(s"Features: $features")
      sender ! features
    case x =>
      log.warning("Received unknown message: {}", x)
  }
}
我试图做的是从
UpdateStatistics
actor中获取
json:Jvalue
消息。读了这本书,我认为这可能会有用:

  implicit val i = inbox()
  i.select() {
     case x => println(s"Valor Devuelto $x")
  }
  println(i receive(2.second))
但是我不知道如何修改
UpdateStatistics
actor,以便将结果发送到上面的收件箱

我在文档中读到的另一个选项是

但我认为这不是正确的方法


有没有办法实现我想做的事情?或者我是否需要使用第二个
Actor
,向其发送
JSON
响应?

您可能正在AKKA中寻找
ask
模式。这将允许您向发件人返回一个值

import akka.pattern.ask
import akka.util.duration._

implicit val timeout = Timeout(5 seconds)

val future = actor ? QueryController(1)    
val result = Await.result(future, timeout.duration).asInstanceOf[JValue]

println(result)

要使这项工作正常,您需要将响应发送给原始的
发送者
,而不是
自己
。此外,在处理邮件时,您还应注意将来关闭发件人的危险

UpdateStatistic
actor中获取信息到底是什么意思?谁是收件人?你想对这些数据做什么?@JorgenGValley我的意思是把信息发送到Akka之外的主线程。例如,在主应用程序中,我启动了一个actor系统,并安排actor查询api。我希望参与者的响应出现在主应用程序中,而不是Akka中。我不知道我的解释是否正确。但这会阻碍线程,对吗
UpdateStatistics
将每5秒发布一次消息,使用您的解决方案,我只能从
UpdateStatistics
获得第一个响应,我说得对吗?。谢谢你的回答。如前所述,它会阻塞。相反,只需使用
future.onSuccess
来处理结果我认为你问的演员也必须是回答的人,但很高兴地发现事实并非如此。谢谢你的回复,让我回去尝试,而另一位演员的回复确实解决了我的问题