java 如何使用 JMS 队列将消息发送到特定的接收器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6224007/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:56:07  来源:igfitidea点击:

How to send Message to particular Receiver using JMS Queue

javajakarta-eejmshornetq

提问by SmartSolution

Is it possible to send message to particular receiver using JMS Queue(HornetQ)?

是否可以使用 JMS 队列(HornetQ)向特定接收者发送消息?

Among so many receivers, I want certain message to be received by receiver which are running on Linux OS.

在这么多接收器中,我希望在 Linux 操作系统上运行的接收器接收某些消息。

Every suggestion is appriciated.

每一个建议都是appriciated。

Thanks.

谢谢。

回答by Dev

You can set a message property using Message.setObjectProperty(String, Object)and then have your consumers select the messages they are interested in using Session.createConsumer(Destination, String)

您可以使用Message.setObjectProperty(String, Object)设置消息属性,然后让您的消费者使用Session.createConsumer(Destination, String)选择他们感兴趣的消息

Sender example:

发件人示例:

Message message = session.createMessage();
message.setObjectProperty("OS", "LINUX");
producer.send(message);

Receiver example:

接收器示例:

MessageConsumer consumer = session.createConsumer(destination, "OS = 'LINUX'");
//Use consumer to receive messages.

The receiver in the example will ignore (they will go to some other receiver) all messages that do not match the selector. In this case all message where the 'OS' property is not 'LINUX' will be ignored by this consumer.

示例中的接收器将忽略(它们将转到其他接收器)所有与选择器不匹配的消息。在这种情况下,此使用者将忽略“OS”属性不是“LINUX”的所有消息。

回答by dbf

You can set properties of JMS message: http://download.oracle.com/javaee/1.4/api/javax/jms/TextMessage.htmland filter messages at client side. For example, message.setStringProperty("TARGET_OS", "LINUX") - at sender http://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/- detect OS at receivers and filter messages with correct TARGET_OS property

您可以设置 JMS 消息的属性:http: //download.oracle.com/javaee/1.4/api/javax/jms/TextMessage.html并在客户端过滤消息。例如, message.setStringProperty("TARGET_OS", "LINUX") - 在发送方http://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/- 在接收方 检测操作系统和使用正确的 TARGET_OS 属性过滤消息

回答by Joseph Ottinger

You can use JMS selectors on the consumer side to look for messages that fit specific criteria.

您可以在使用者端使用 JMS 选择器来查找符合特定条件的消息。

回答by Biju Kunjummen

Not sure if I am missing something, you could keep things simple by having multiple queues - specific to each platform, then the linux based consumers can listen to the linux specific queue alone. Now your challenge probably will be to route the messages to the appropriate queue from the producer side, that should be fairly easy if the routing is based on some attribute of the message?

不确定我是否遗漏了什么,您可以通过拥有多个队列来使事情变得简单 - 特定于每个平台,然后基于 linux 的消费者可以单独收听 linux 特定队列。现在您的挑战可能是将消息从生产者端路由到适当的队列,如果路由基于消息的某些属性,那应该相当容易?