Akka跟踪演员何时完成 [英] Akka tracking when the actors finished

查看:79
本文介绍了Akka跟踪演员何时完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码遍历人员列表,并在class1中为每个人员调用一个回调。

I have following code that traverses through a list of people and calls a callback for each of them in class1.

def syncPeople(callback: Person => _) = Future {
   person.findAll(criteria).foldLeft(0L) { (count, obj) =>
      callback(obj)
      count + 1
   } 
}

回调和对syncPeople的调用在class2中,并且看起来与此类似

Callback and the call to syncPeople is in class2 and looks similar to this

def getActor(person: Person):ActorRef = {
  if(person.isMale) maleActor
  else femaleActor
}

def process(person: Person): Unit = {
   val workActor = getActor(person)
   workActor ! person
} //The actor does the actual work and may be quite intense

def syncPeople(process)

现在,我想跟踪同步所有人所需的总时间。即最后一个workActor完成工作时。我正在使用第三个Actor:MonitorActor来跟踪开始和结束时间。当男演员,女演员处理一个人时,可以向其发送消息

Now, I want to track the total time taken to sync all people. ie when the last workActor completes the work. I am using a third Actor: MonitorActor to keep track of start and end times. The MaleActor, FemaleActor can send messages to this when they process an individual

跟踪此衍生程序的最佳方法是什么?

Whats the best way to keep track of this spawned processes?

我探索了


  1. Future.sequence // //但是发送workActor消息的类不是actor 。因此将来不会收到消息

  1. Future.sequence // but the class sending the workActor the message is not an actor. so the future does not receive the message

在它们完成时跟踪personId,但不使用var来在MonitorActor中累积接收到的消息是不可能的实施此方法。使用var不是首选的处理方式

keeping track of personIds when they finish, but without using a var, to accumulate the received messages in MonitorActor its not possible implement this.. and using var is not preferred way of doing things

实现此目标的其他方法还有什么

What could be other ways of implementing this

推荐答案

有趣,目前我正在解决与此类似的问题。我建议的解决方案是使用 akka-fsm 进行跟踪

Funny, I'm working on a very similar problem to this at the moment. The solution I would suggest is using akka-fsm which keeps track of state.

本质上是在您的状态对象之外的某个地方,执行类似生成代表id的Long的操作:

Essentially in something outside of your state object, do something like generate a Long that represents an id:

def getId(): Long = System.currentTimeMillis() / 1000L

状态对象在正确实现后是不可变的,因此您只需在整个事务中重复使用此ID。

The state object when implemented correctly is immutable, so you just keep reusing this id throughout the transaction.

我知道此答案缺少很多实现细节,但我仍在自己的代码中进行实现。希望在阅读了一些有关akka-fsm的知识并进行尝试之后,这个答案有意义吗?

I know this answer is missing a lot of the implementation details but I'm still working on the implementation myself in my own code. Hopefully after reading about akka-fsm a bit and playing with it, this answer will make sense?

这篇关于Akka跟踪演员何时完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆