How to send Message to particular Receiver using JMS Queue

SmartSolution picture SmartSolution · Jun 3, 2011 · Viewed 19.6k times · Source

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

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

Every suggestion is appriciated.

Thanks.

Answer

Dev picture Dev · Jun 3, 2011

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)

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.