简体   繁体   English

Akka的事件总线是否保证消息顺序?

[英]Does Akka's event bus guarantee message order?

I would like to keep the order the events entered the bus. 我想保留事件进入公交车的顺序。 For example if event1 then event2 where entered to the bus then a subscribed actor would get them in that order. 例如,如果event1然后event2输入到总线,那么订阅的actor将按顺序获取它们。

The question is if such order is guarantee, both on clustered and on single node actor system. 问题是如果这种顺序是保证的,无论是在集群节点还是单节点演员系统上。

If you are using the event stream on the actor system ( system.eventStream ) and if you can guarantee a single thread is publishing, then yes, the order will be preserved. 如果您在actor系统( system.eventStream )上使用事件流,并且如果您可以保证单个线程正在发布,那么是,将保留该顺序。 The subchannel classification flavor of the event bus (the kind tied to system.eventStream ) is really simple. 事件总线的子通道分类风格(与system.eventStream绑定的类型)非常简单。 There is a Map of basically class type to a list of subscribing actors. 有一个基本类类型的Map到订阅actor的列表。 When an event is published, it gets the matching list of subscribers from the Map (if any) and then sends the message to each of them. 发布事件时,它会从Map (如果有)获取匹配的订户列表,然后将消息发送给每个订阅者。 So if only one thread was calling publish , then it would need to finish publishing event1 (and thus delivering it to the mailboxes of all subscribers) before moving on to event2. 因此,如果只有一个线程正在调用publish ,那么它需要完成发布event1(从而将其传递给所有订阅者的邮箱),然后再转到event2。 Actors process their mailboxes in the order that the messages were received (unless you are using a custom PriorityMailbox impl), so first in first out. Actors按照收到邮件的顺序处理邮箱(除非您使用的是自定义PriorityMailbox impl),所以先进先出。

Now of course if you had multiple threads, I can't guarantee this in order processing as thread1 may start the publish first, but thread 2 might finish it first leading to what you would consider out of order. 当然,如果你有多个线程,我不能保证这个顺序处理,因为thread1可能首先启动发布,但是线程2可能首先完成它,导致你不按顺序考虑。

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

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