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

2019-06-22 12:34发布

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?

2条回答
叛逆
2楼-- · 2019-06-22 12:52

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..

查看更多
SAY GOODBYE
3楼-- · 2019-06-22 13:06

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.

查看更多
登录 后发表回答