简体   繁体   English

期货如何在 Akka 演员中执行

[英]How are Futures executed in an Akka actor

I'm trying to make a background task that performs a network call and stores the response in a database.我正在尝试执行一个后台任务,该任务执行网络调用并将响应存储在数据库中。 According to the documentation , background tasks are supposed to use the scheduler within the Akka actor system.根据文档,后台任务应该使用 Akka 演员系统中的调度程序。 I need to run a Future inside of this actor:我需要在这个演员里面运行一个 Future :

    actorSystem.scheduler.scheduleOnce(delay = new FiniteDuration(0, TimeUnit.SECONDS)) {
        val future = network.request()
        future.flatMap(saveToDatabase(_))
    }

Therefore, I have two questions:因此,我有两个问题:

  1. Is this future guaranteed to get executed (to completion)?这个未来是否保证得到执行(完成)?
  2. Is it possible for other requests to follow up on the status of this task (whether it has finished or not)?其他请求是否可以跟进此任务的状态(是否已完成)?

The Future in the future value is returned by the network object, so this object is responsible for executing the code that triggers the Future , not Akka. future值中的Futurenetwork对象返回,因此该对象负责执行触发Future的代码,而不是 Akka。 So you need to look at the documentation for the request call to see what completion guarantees there are for this Future .因此,您需要查看request调用的文档,以了解此Future有哪些完成保证。

The Future returned by the flatMap call uses the default execution context that is in scope when this task is created. flatMap调用返回的Future使用创建此任务时范围内的默认执行上下文。 But the saveToDatabase call is not guaranteed to be called because the Future can fail and flatMap is only called on success.但是不保证会调用saveToDatabase调用,因为Future可能会失败,而flatMap只会在成功时调用。

If you want to track the status of this task, send messages to a monitoring actor at various points in the execution.如果要跟踪此任务的状态,请在执行的各个点向监视参与者发送消息。 Other actors can then ask this monitoring actor about the progress of the task.然后其他参与者可以向这个监控参与者询问任务的进度。

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

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