哪些设计决策有利于 Scala 的 Actors 而不是 JMS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4648280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What design decisions would favour Scala's Actors instead of JMS?
提问by Kristian
What are the differences using Scala Actors instead of JMS?
使用 Scala Actors 而不是 JMS 有什么区别?
For example from a performance and scalability perspective, what does the Scala Actor model add compared to JMS? In which cases does it make more sense to use Actors rather than JMS, i.e. what problems does Actors address that JMS cannot cover?
例如,从性能和可扩展性的角度来看,与 JMS 相比,Scala Actor 模型增加了什么?在哪些情况下使用 Actors 而不是 JMS 更有意义,即 Actors 解决了哪些 JMS 无法解决的问题?
回答by James Iry
JMS and Scala actors share a theoretical similarity but don't think of them as necessarily solving the same problems architecturally. Actors are meant to be a lightweight alternative to shared-memory concurrency where races and deadlocks are generally harder to accidentally create. JMS is a sophisticated API that's meant to span direct messaging, publish/subscribe, transactions, EJB integration, etc.
JMS 和 Scala 参与者在理论上有相似之处,但不要认为它们在架构上解决了相同的问题。Actor 旨在成为共享内存并发的轻量级替代方案,在这种情况下,竞争和死锁通常更难意外创建。JMS 是一个复杂的 API,旨在跨越直接消息传递、发布/订阅、事务、EJB 集成等。
The closest JMS equivalent to an actor would be a message driven bean that is backed by a non-persistent, non-transactional, non-pub/sub queue. I'll call that a "simple JMS bean".
与参与者最接近的 JMS 是由非持久性、非事务性、非发布/订阅队列支持的消息驱动 bean。我将其称为“简单的 JMS bean”。
Now, to your questions.
现在,回答你的问题。
Performance is a hard thing to talk about since JMS is a specification rather than an implementation. None-the-less when using simple JMS bean I'd expect performance to be roughly similar with perhaps a bit of an edge to the actor in time and memory. As you add capabilities to JMS such as pub/sub, transactions, etc performance will naturally degrade even further but then you're trying to compare apples to oranges.
由于 JMS 是一种规范而不是一种实现,因此性能很难谈论。尽管如此,当使用简单的 JMS bean 时,我希望性能大致相似,并且在时间和内存方面可能比参与者有一点优势。当您向 JMS 添加功能(例如发布/订阅、事务等)时,性能自然会进一步降低,但随后您正试图将苹果与橙子进行比较。
As for scalability, simple JMS beans should scale pretty much exactly the same way as actors. Adding transactions into the JMS mix will naturally hurt scalability by an amount depending on the scope of the transactions.
至于可伸缩性,简单的 JMS bean 应该以与 actor 几乎完全相同的方式伸缩。将事务添加到 JMS 组合中自然会损害可伸缩性,这取决于事务的范围。
The broader question of what do actors do that JMS can't. Well, without built-in pub sub or transactions it would seem that actors subtract from JMS - and broadly that's true. But here's the thing: actors require so little code that I can happily use them for very fine grained concurrency. In ordinary Java code I might say "I don't feel like screwing with JMS and its dependencies or the code it requires etc so I'll just spawn a thread, use a lock, and share a data structure." With Scala actors I'm much more likely to say "I'll just whip up an actor and move on."
演员做什么而 JMS 不能做的更广泛的问题。好吧,如果没有内置的 pub sub 或事务,参与者似乎会从 JMS 中减去——从广义上讲,这是真的。但问题是:actor 只需要很少的代码,我可以很高兴地将它们用于非常细粒度的并发。在普通的 Java 代码中,我可能会说“我不想搞砸 JMS 及其依赖项或它需要的代码等,所以我只会生成一个线程、使用锁并共享数据结构。” 对于 Scala 演员,我更可能会说“我会挑起一个演员然后继续前进”。
There's also a philosophical difference in design. Actors have a simple, built in concept of supervisor hierarchies. Actors are usually used in a "let it crash" design. If an actor dies for some reason then another actor is responsible for deciding what to do about it such as restarting that actor, killing a bunch of actors and restarting all of them, or killing a bunch of actors and itself so that some otther actor can deal with the problem. That kind of thing can appended onto JMS, but it's not core to the API and must be managed externally somehow.
设计中也存在哲学上的差异。Actors 有一个简单的、内置的主管层级概念。Actor 通常用于“让它崩溃”的设计。如果一个演员因某种原因死亡,那么另一个演员负责决定如何处理它,例如重新启动该演员,杀死一群演员并重新启动所有演员,或者杀死一群演员和自己,以便其他演员可以处理问题。这种东西可以附加到 JMS 上,但它不是 API 的核心,必须以某种方式在外部进行管理。
By the way, for a Scala actor library that moves more into the realms that JMS covers see Akka. Akka also brings a declarative approach to many common actor hierarchy strategies.
顺便说一句,对于更多进入 JMS 涵盖的领域的 Scala 演员库,请参阅Akka。Akka 还为许多常见的 actor 层次结构策略带来了一种声明式方法。

