I'm using Spring JMS and ActiveMQ to send message from one sender to multiple listeners asynchronously. All listeners subscribe to an ActiveMQ Topic so that the message can be delivered to them. I want to know if a particular listener gets the message from the sender. Is there a way to achieve this?
Edit: Added the JMS sender and listener classes
This is my message sender class:
public class CustomerStatusSender {
private JmsTemplate jmsTemplate;
private Topic topic;
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void setTopic(Topic topic) {
this.topic = topic;
}
public void simpleSend(final String customerStatusMessage) {
jmsTemplate.send(topic, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage message = session.createTextMessage("hello from sender.");
message.setStringProperty("content", customerStatusMessage);
return message;
}
});
}
}
And this is one of the message listeners:
public class CustomerStatusListener implements MessageListener {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
System.out.println("Subscriber 1 got you! The message is: "
+ message.getStringProperty("content"));
} catch (JMSException ex) {
throw new RuntimeException(ex);
}
}
}
}
Upon calling simpleSend()
method in the sender class, all listeners subscribed to this topic will get this message asynchronously. But I want to know if this particular CustomerStatusListener
receives the message from the message sender. How to do it asynchronously? I assume that I can't use ReplyTo
in the sender and listener as suggested in one of the answers if I want to do it asynchronously. What do I need to add in the sender and listener classes to get message receipt confirmation from the listener?
The status of the listeners is always available by running the list listeners command from the user interface or the Transaction Server console. If a listener becomes disconnected, the Transaction Server can attempt to re-connect to the queue multiple times before it fails with a connection error.
JMS provides three acknowledgment options and the recovery is handled manually. AUTO_ACKNOWLEDGE – JMS Session automatically acknowledges the client's receipt of the messages. No duplicate messages delivered. CLIENT_ACKNOWLEDGE – JMS clients have to acknowledge by calling the message's acknowledge() method.
The JMS Listener adapter operates in an asynchronous mode. It establishes an asynchronous listener on the queue or topic destination specified by the JNDI name of Destination field. When a qualified message arrives at the destination, the adapter immediately processes the message.
The parameter destination is a Queue or Topic object that the application has created previously. The application then uses the receive() method of the MessageConsumer object to receive a message from the destination, as shown in the following example: Message inMessage = consumer. receive(1000);
The providers push the JMS message to queues and topics. The consumers pull the message from the broker. Load balancing can be designed by implementing some clustering mechanism. Thus, once the producer sends the messages, the load will be distributed across the clusters.
I ran into a situation like this before. I've used two topics to handle the scenario. 1st topic is used for publish and the second topic is used for receipts. Here's the flow I had in my application.
I hope it solves your problem..
You would have to set a replyTo and have the listener reply when it gets a message. You can create a TemporaryQueue for the reply.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With