Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 326
tonga Avatar asked Nov 18 '13 22:11

tonga


People also ask

How do you check if JMS listener is running?

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.

How do I acknowledge a JMS message?

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.

How does JMS listener work?

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.

How do I get messages from JMS queue?

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);

Is JMS push or pull?

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.


2 Answers

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

like image 157
saratbeesa Avatar answered Oct 29 '22 04:10

saratbeesa


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.

like image 20
Gary Russell Avatar answered Oct 29 '22 03:10

Gary Russell