在单线程上下文中使用JMS Session对象的原因 [英] Reason for a JMS Session object to be used in a single threaded context

查看:196
本文介绍了在单线程上下文中使用JMS Session对象的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JMS的新手。所以这可能是一个真正的noob问题的专家在这里SO。
然而,我在尝试汇编一个非常重要的JMS如何工作的概念时遇到困难。

I am a newbie to JMS. So this may be a really noob question for the experts out here in SO. However I am having difficulty trying to grok one very important concept of how JMS has been laid out to work.

Javadoc的 JMS Session (粗体强调我)

From the Javadoc of the JMS Session(bold emphasis mine)


Session对象是一个<上下文和消费消息
。虽然它可以在Java虚拟机(JVM)之外分配提供程序资源
,但它被认为是一个轻量级的
JMS对象。

A Session object is a single-threaded context for producing and consuming messages. Although it may allocate provider resources outside the Java virtual machine (JVM), it is considered a lightweight JMS object.

我认为JMS的主要优点是异步传递消息。术语 asynchronous 自动适用于同时产生和消费消息的多个线程的概念。所以我对所有重要的 Session 对象的单线程上下文的概念感到困惑。我在这里缺少什么?

I thought that the main advantage of JMS is asynchronous delivery of messages. The term asynchronous automatically lends itself to the notion of multiple threads concurrently producing and consuming messages. So I am confused with the concept of a single threaded context for the all important Session object. What am I missing here?

推荐答案

它的原因与JTA是单线程的相同。对于如何/何时调度运行线程没有任何硬的保证。此外,没有同步,没有保证什么时候线程将完成他们的工作。给定这些约束,难以在保持并行处理的好处的同时创建事务保证。

Its for the same reason that JTA is single threaded. There aren't any hard guarantees about how/when threads are scheduled to run. In addition, without synchronization there are no guarantees about when threads will complete their work. Given these constraints it is difficult to create transactional guarantees while maintaining the benefits of parallel processing.

例如,如果我产生一个事务,然后产生一个线程,发送消息在该事务,但任意提交事务在主线程,事务管理器do(忽略 commit()?失败 send()?如果我有其他事务性工作(数据库提交)尚未完成,但我的消息发送线程已完成,并调用 commit()如果怎么办?在TransactionManager尝试确定所有事务参与者的 2-Phase-Commit 状态时,如果线程旋转并开始更多的工作,该怎么办?

For instance, if I spawn a transaction, then spawn a thread that sends messages on that transaction, but arbitrarily commit the transaction in the main thread, what should the transaction manager do(ignore the commit()? fail the send()?). What if If I have other transactional work(database commits) that haven't completed, but my message sending thread is done and has called commit()? What if a thread spins up and starts more work while the TransactionManager is trying to determine 2-Phase-Commit status of all the transaction participants?

事务API都基于定义良好的工作单元。当工作单元在单个线程中运行时,这些API非常清晰。事情变得复杂,否则会被Java EE规范未解决的实现所覆盖。这些事务管理器允许跨越多个线程的嵌套事务和/或事务。这些实现使用非常仔细设计的同步,有限的使用情况,可能只是最终序列化的工作。

Transaction API's are all based on well defined units of work. These API's are pretty clear cut when the unit of work runs in a single thread. Things become complicated otherwise and are covered by implementations not addressed by the Java EE spec. These transaction managers allow nested transactions and/or transactions that span multiple threads. These implementations use very carefully designed synchronization, have limited use cases, and likely just ends up serializing the work anyway.

对于大多数用户,JMS的主要好处是类似于任何基于消息的演员系统的。许多离散步骤可以拉取队列的消息,处理它们,通过将结果放置在另一队列上来推进到下一步骤。如果任何步骤成为瓶颈,您只需添加更多的处理代理(MDB,spring bean等)。每个代理在单个线程上执行其处理,您可以通过创建更多代理来扩展到多个内核。

For most users, the primary benefit of JMS is similar to that of any message based actors system. Many discrete steps can pull messages of a queue, process them, advance to the next step by placing the result on another queue. If any step becomes a bottleneck you can just add more processing agents(MDBs, spring beans, etc.). Each agent does its processing on a single thread and you scale to multiple cores by creating more agents.

这篇关于在单线程上下文中使用JMS Session对象的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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