等待多阿卡FSM消息 [英] Waiting on multiple Akka FSM messages

查看:186
本文介绍了等待多阿卡FSM消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有收到消息后运行下面的伪code的阿卡FSM演员,而在的readyState

I have an Akka FSM actor that runs the following pseudocode after receiving a message while in ReadyState

lookupA ! Wrapper(Lookup("A"))
lookupB ! Wrapper(Lookup("B"))
lookupC ! Wrapper(Lookup("C"))
goto(LookingUpDataState) using DataFound(a = None, b = None, c = None)

演员,然后等待回应它可以是 FullResult [T] (延长 ServiceResult [T] )或空缺(延长 ServiceResult [没什么] )。成功的查询结果用于填充DataFound实例的领域和空查找结果导致记录的错误消息和演员的终止。

The actor then waits for responses which can be either FullResult[T] (extending ServiceResult[T]) or Empty (extending ServiceResult[Nothing]). Successful lookup results are used to populate the DataFound instance's fields and Empty lookup results result in a logged error message and the termination of the actor.

我的问题是:我怎么能确定哪个查找失败,这样我就可以登录故障或退回到一个默认值?所有我能想到的是检查发件人的ActorRef(哈克)或添加一个唯一的ID字段的所有消息(高开销)。

My question is this: how can I determine which lookup failed, so that I can log the failure or fallback to a default value? All I can think of is examining the sender's ActorRef (hacky) or adding a unique ID field to all messages (high overhead).

这是一个简单的问题,使用ASK的期货来解决。难道一个习惯阿卡解决存在?

This is a simple problem to solve using Ask's and Futures. Does an idiomatic Akka solution exist?

推荐答案

有就是你可以在这里使用了一些图案。你将不得不诉诸下面列出的其中一个选项。它们都有一些折衷(和基准为王无论如何​​),但我已经在坏到好的顺序列出它们,​​

There's a few patterns you could use here. You will have to resort to one of these options listed below. They all have some trade off (and benchmarking is king anyway), but I've listed them in order of "bad to good",


  • 在顺序执行的查询,所以发A,等待响应,派出乙......但是,这是的可怕的 - 在连续不必要

  • 使用要求模式,所以你会旋转起来(即内部是如何工作要求)3的演员,他们将完成自己的未来。因此,这也对寄件人一定的成本,因为它启动这些特殊用途的行为者(比普通演员更小,但仍然)。

  • 是,你需要以某种方式标记这些消息。所以,你会送一些ID和响应必须包含相同的ID,那么你知道它的哦,这是一个为响应等等。我认为,这是去这里的推荐方式。

  • 有一个的的contrib 的在阿卡模式可称为聚合,这是专为这种使用情况,所以你可能要检查出来:的 http://doc.akka.io/docs/akka/2.3.4/contrib/aggregator.html 然而,如果你喜欢还是不喜欢非常多的个人喜好我猜的问题。

  • performing the queries in-order, so send A, wait for A response, send B ... But that's horrible - as in needlessly sequential.
  • use the ask pattern, so you'll spin up (internally that's how ask works) 3 actors, and they'll complete "their own" future. So this also has some cost on the sender because it has to start these special purpose actors (smaller than a normal actor, but still).
  • Yes, you'll need to tag these messages somehow. So you'd send some ID and the response must contain the same ID, then you know it's "oh it's the response for A" etc. I would argue that this is the recommended way to go here.
  • there's a contrib pattern available in Akka called the Aggregator, which is designed for this use case, so you might want to check it out: http://doc.akka.io/docs/akka/2.3.4/contrib/aggregator.html However if you like it or not is very much a matter of personal taste I guess.

我个人最喜欢的(我们倾向于避免要求一般)将被标记在信封(ID,有效载荷)这样的反应也可以在这样的信封(ID,响应)包裹。如果您决定把这些简单的信封或更多的领域的术语是由你。

My personal favourite (we tend to avoid ask in general) would be tagging the requests in an Envelope(id, payload) so the responses can also be wrapped in such Envelope(id, response). If you decide to call these simply envelope or more with domain terminology is up to you.

希望这有助于。

这篇关于等待多阿卡FSM消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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