ActiveMQ JMS to send message to subset of consumers

Viewed 108

My problem is, let's say we have 10 consumers subscribed to the topic. From the producer side, I have to send a message to only 5 consumers.

Let's say 5 consumers are having unique id [1,2,3,4,5] I have included this in the producer side with string concatenation as "1,2,3,4,5", I specified this in

devices = "1,2,3,4,5" messagePostProcessor.setStringProperty("deviceIds", devices);

How to handle it on the consumer side as a selector. Because I may send to 5 consumers, 10 consumers, or 50 consumers out of 100 consumers based on the demand of the situation.

From the producer's side we get consumers id's to send. But how we can identify or handle it on consumers.

1 Answers

As mentioned in jms-selectors, jms-message and activemq-message, you cannot use array object as a selector property for jms message. Regardless, what you can try is something like this.

I am thinking your device ids are going to be like this. For ex: 'P8O4O18143JA3068', 'M0A0H8081436A22N', 'A0N0G8081436A2DI' etc.

So, while sending the message from the producer do like this.

String messageBody = "Message body that you want to send."
String messageSelector = "P8O4O18143JA3068, M0A0H8081436A22N, A0N0G8081436A2DI";
TextMessage message = session.createTextMessage(messageBody);
message.setStringProperty("deviceIds", messageSelector);
producer.send(message);

And, while receiving the message in the consumer do like this.

String myDeviceId = "P8O4O18143JA3068";
String messageSelector = "deviceIds LIKE '%" + myDeviceId + "%'";
consumer = session.createConsumer(destination, messageSelector);
Message message = consumer.receive()

So, this way you can allow your consumers to select/receive the messages only if it's associated deviceId exists in the message property.

Related