简体   繁体   English

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

[英]How to send Message to particular Receiver using JMS Queue

Is it possible to send message to particular receiver using JMS Queue(HornetQ)?是否可以使用 JMS Queue(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.谢谢。

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)设置消息属性,然后让您的消费者 select 使用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.示例中的接收器将忽略(它们会将 go 发送到其他接收器)所有与选择器不匹配的消息。 In this case all message where the 'OS' property is not 'LINUX' will be ignored by this consumer.在这种情况下,该消费者将忽略“OS”属性不是“LINUX”的所有消息。

You can set properties of JMS message: http://download.oracle.com/javaee/1.4/api/javax/jms/TextMessage.html and filter messages at client side.您可以设置 JMS 消息的属性: http://download.oracle.com/javaee/1.4/api/javax/jms/TextMessage.html.com/javaee/1.4/api/javax/jms/TextMessage.html在客户端过滤消息。 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例如,message.setStringProperty("TARGET_OS", "LINUX") - 在发送方http://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/ - 在接收方检测操作系统和使用正确的 TARGET_OS 属性过滤消息

You can use JMS selectors on the consumer side to look for messages that fit specific criteria.您可以在消费者端使用 JMS 选择器来查找符合特定条件的消息。

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

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

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