How to know if a listener gets the message in JMS?
Asked Answered
M

2

5

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?

Morisco answered 18/11, 2013 at 22:13 Comment(2)
You mention 'async' a few times, but you are suggesting you want 'sync' style behaviour, i.e. the client sends the message but then needs the reply before it can continue. If you don't want to use JMS synchronously, then you could have the client send the message then poll a db for a reply record. Your listener consumes the message, then when finished writes to the reply table (with perhaps listener number, custName, date, etc) with the client will pick up on the next poll and continue its processing.Coop
Thanks for your suggestion. Just updated my post to show the message sender and listener to make it clear what I'm trying to do. Basically I just want to add message receipt confirmation when one of the listeners gets the message so that the sender knows the listener CustomerStatusListener already gets the message. Is there a simple way to do that?Morisco
F
6

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.

  1. Sender posts a 'Request' message to 1st topic.
  2. Listener receives the message as it listens to Message<Request> object.
  3. After processing the message Listener sends Message<Ack> object to 2nd Topic.
  4. Sender in turn listens to Message<Ack> object recieves it. I've added identifiable information about the listener to find out which listeners ultimately got my request Message<Request>.

I hope it solves your problem..

Frazzle answered 25/11, 2013 at 19:56 Comment(1)
Thanks. I've solved the problem by a similar approach: Use a topic to send message to all listeners, and use a queue to send the ack back to the sender.Morisco
G
4

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.

Greggrega answered 18/11, 2013 at 23:24 Comment(3)
Where should I set the replyTo, in the sender or the listener? Why do I need to create a TemporaryQueue for reply?Morisco
The replyTo is a header in the message. Search for JMS ReplyTo. You said you wanted to know which listener got the message. You can use a fixed reply queue if you want, but you'll need a message selector on your consumer so you only get the reply you are interested in. I suggest you look for some JMS tutorials.Greggrega
I tried using setReplyTo in the sender but I got some error due to temporaryQueue unavailable for the listener. Will start a new post on that. Thanks.Morisco

© 2022 - 2024 — McMap. All rights reserved.