如何让我的 MDB 忽略来自自身的消息?

发布于 2024-08-22 04:16:39 字数 576 浏览 4 评论 0原文

我有一些客户端,它们既是 ActiveMQ 消息代理上单个主题的消费者又是订阅者。所有客户端共享相同的代码,它们实际上使用完全相同相同的企业应用程序,其中包括:(1) 生成消息的 EJB,以及 (2) 使用消息的 MDB。

问题基本上是,如果我有客户端 A、B 和 C,并且如果 A 发出一条消息,那么 A、B、C 都会收到该消息。我不希望A收到自己的消息。

因此,我尝试了各种解决方案,我想到的最好的解决方案是在传出消息上设置一个字符串属性,例如 source=myVeryOwnID。然后在 MDB 中,我设置了一个消息选择器,例如 source <> “myVeryOwnID”。

不幸的是,这是一个糟糕的解决方案,因为我必须在源代码中设置此 ID(在我的例子中,在注释中)。这意味着在部署新客户端时,我不能简单地将 .EAR 文件提供给某人,而是必须使用唯一的“源”属性专门重新编译。

理想情况下,我想使用 MAC 地址作为 ID,或者可能是 Glassfish 中设置的 ID(我使用的是 GFv3)。

任何解决方案或想法将不胜感激!

I have a few clients that are both consumers and subscribers to a single topic on an ActiveMQ message broker. All the clients share the same code, they are in fact using exactly the same Enterprise Application consisting of: (1) an EJB producing a message, and (2) an MDB consuming the message.

The problem is basically that if I have clients A, B and C, and if A sends out a message, then A, B, C will all receive the message. I don't want A to receive its own message.

So I played around with various solutions, the best one I came up with was to set a string property on the outgoing message, e.g. source=myVeryOwnID. Then in the MDB, I set up a message selector like source <> 'myVeryOwnID'.

Unfortunately, that is a poor solution because I would have to set this ID in the source code (in my case, within annotations). This means that when deploying a new client, I cannot simply give the .EAR file to someone, instead I have to specifically re-compile with a unique "source" property.

Ideally, I would like to use the MAC address as the ID, or perhaps an ID set within Glassfish (I am using GFv3).

Any solutions or ideas would be highly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开半夏魅人心 2024-08-29 04:16:39

恕我直言,在消息中使用“源”消息属性和消息选择器是正确的方法。现在,如果您不想在 MDB 中对其进行硬编码(在注释中),请使用 部署描述符并在打包时设置消息选择器。

Using a "source" message attribute in the message and a message selector is IMHO the way to go. Now, if you don't want to hard code this in the MDB (in annotations), then use a deployment descriptor and set the message selector at packaging time.

爱给你人给你 2024-08-29 04:16:39

ActiveMQ 包含一个方法参数来准确解决这种情况。 ActiveMQSession.createConsumer() 方法和 ActiveMQSession。 createDurableSubscriber() 方法提供了一个接受名为 noLocal 的参数的变体。将 noLocal 参数设置为 true 以避免接收在同一连接上本地发布的消息。

布鲁斯

ActiveMQ contains a method argument to address exactly this situation. Both the ActiveMQSession.createConsumer() method and the ActiveMQSession. createDurableSubscriber() method provide a variant that accepts an argument named noLocal. Set the noLocal argument to true to avoid receiving messages that were published locally on the same connection.

Bruce

明月松间行 2024-08-29 04:16:39

普通的旧 System.GetProperty() 和 -D 选项怎么样?您可以将唯一的应用程序 ID 设置为系统属性:

-Dmyapp.id=A

例如,在 Tomcat 中,您可以通过 JAVA_OPTS 变量传递系统属性:

export JAVA_OPTS='-Dmyapp.id=A'

然后您可以在应用程序中读取它:

String appId = System.getProperty("myapp.id")

所有您必须为每个应用程序服务器设置系统变量。

What about the plain old System.GetProperty() and -D option? You can set the unique application ID as the system property:

-Dmyapp.id=A

For example in Tomcat you can pass system property via the JAVA_OPTS variable:

export JAVA_OPTS='-Dmyapp.id=A'

Then you can read it in the application:

String appId = System.getProperty("myapp.id")

All you have to do it to set system variable for each of your application server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文