简体   繁体   English

JMS onMessage()和并发

[英]JMS onMessage() and concurrency

I have a stand-alone JMS app that subscribes to several different JMS topics. 我有一个独立的JMS应用程序,可以订阅几个不同的JMS主题。 Each topic has its own session and onMessage() listener. 每个主题都有自己的会话和onMessage()侦听器。 Each onMessage() method updates a common current value table - all the onMessage() methods update the same current value table. 每个onMessage()方法都更新一个公共当前值表 - 所有onMessage()方法都更新相同的当前值表。

I've read that the onMessage method is actually called on the JMS provider's thread. 我已经读过onMessage方法实际上是在JMS提供者的线程上调用的。 So, my question is: if all these onMessage() methods are called on a separate thread than my app, doesn't this present a concurrency problem since all these threads update a common CVT? 所以,我的问题是:如果所有这些onMessage()方法都在一个单独的线程上调用而不是我的应用程序,那么这是否会出现并发问题,因为所有这些线程都会更新一个常见的CVT? Seems like I need to synchronize access to the CVT somehow? 好像我需要以某种方式同步访问CVT?

Short answer to your question : YES, you need to take care of concurrency concerns when your JMS code is updating some common in-memory object. 简短回答您的问题 :是的,当您的JMS代码更新一些常见的内存中对象时,您需要注意并发问题。

However, I'm not sure what you mean by "common current value table"? 但是,我不确定“常用电流值表”是什么意思? If this is some database table, then database should take care of concurrency issues for you. 如果这是一些数据库表,那么数据库应该为您处理并发问题。

EDIT: it turned out that "common current value table" is a common in-memory object. 编辑:事实证明,“常用当前值表”是一个常见的内存中对象。 As I mentioned earlier, in this case you need to handle the concurrency concerns yourself ( Java concurrency tutorial ). 正如我之前提到的,在这种情况下,您需要自己处理并发问题( Java并发教程 )。

There are mainly two approaches to this problem: 这个问题主要有两种方法:

  • synchronization - suitable if you have low-contention or you are stuck with some non-threadsafe object , then your best choice is synchronization. 同步 - 如果你有低争用或者你遇到一些非线程安全的对象 ,那么你最好选择同步。
  • high-level concurrency objects that come with the JDK - best fit if you have high-contention and you are using some class from regular collections ; JDK附带的高级并发对象 - 如果您有高争用并且正在使用常规集合中的某些类,则最适合该对象 ; just swap in an instance of concurrent collections . 只需交换并发集合的实例。

In any case, it is highly recommended to do your own testing to choose the best approach for you. 无论如何,强烈建议您进行自己的测试 ,为您选择最佳方法。

If you would be dealing with expensive to create non-threadsafe stateless procedural code (no storage of data involved) then you could also use object pooling (eg Commons Pool ), but this is not relevant in your current issue. 如果您要处理昂贵的创建非线程安全的无状态过程代码 (不涉及存储数据),那么您也可以使用对象池(例如Commons Pool ),但这与您当前的问题无关。

JMS onMessage() method is always called by the JMS provider's thread (also known as asynchronous calling). JMS onMessage()方法始终由JMS提供程序的线程(也称为异步调用)调用。

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

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