FSM 与 Akka 中的成为/不成为 [英] FSM vs become/unbecome in Akka
问题描述
Akka 提供了两种有些重叠的方式来管理 actor 状态,有限状态机和unbecome/become.它们各自的优点/缺点是什么?什么时候应该选择其中一个?
Akka provides two somewhat overlapping ways to manage actor states, Finite State Machines and unbecome/become. What are their respective benefits/drawbacks? When should one of them be chosen over the other?
推荐答案
FSM
是一种 DSL,允许您构建比可能的更复杂、可读 的状态机使用核心actor API.您可以向业务人员展示 FSM 代码,他们可以验证业务规则.
FSM
is a DSL that allows you to build more sophisticated, readable state machines than would be possible using the core actor API. You could potentially show the FSM code to a business person and they could validate the business rules.
FSM
DSL 允许您更干净地将事物组合在一起.例如 transitions 允许您分解逻辑必须在参与者 become
行为之间复制.此外,您还可以订阅其他参与者以接收有关转换的通知,这有助于解耦和测试.
The FSM
DSL allows you to compose things together more cleanly. For example transitions allow you to factor out logic that would have to be duplicated across actor become
behaviors. Also you can subscribe other actors to be notified of transitions which helps with decoupling and testing.
计时器也很好地集成到 DSL 中,并且可以干净地处理取消之类的事情.使用调度程序编码超时消息有很多问题.
Also timers are integrated nicely into the DSL and things like cancellation are handled cleanly. Coding timeout messages using the scheduler has a number of gotchas.
FSM
的缺点是它是一种 DSL 和一种供其他团队成员消化的新语法.好处是它是 DSL 和更高级别的抽象.我认为agilesteel 的2 个状态的阈值是一个很好的阈值.但是一旦你通过了 2 个状态,FSM
的好处就非常引人注目.
The down side to FSM
is that it's a DSL and a new syntax for other team members to digest. The up side is that it's a DSL and a much higher level abstraction. I think agilesteel's threshold of 2 states is a good one. But once you get past 2 states the benefits of FSM
are really compelling.
一定要阅读FSM 文档和随附示例对比become
和 FSM
.
Definitely read the FSM docs and the accompanying examples contrasting become
and FSM
.
一个注意事项:使用 unbecome
弹出"一个行为 - 默认行为是不使用行为堆叠.它仅与少数用例相关(即,通常不是状态机).
One note re: "popping" a behavior using unbecome
- the default behavior is to not use the behavior stacking. It is only relevant in a small number of use cases (ie, usually not state machines).
这篇关于FSM 与 Akka 中的成为/不成为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!