Search code examples
javajmsactivemq-artemis

Is it possible to get all messages from a queue via JMS without deleting them from the queue?


I would like to know if it is possible to get the messages from a queue through JMS without removing them. There is a browse() method in JMX, but is there a similar method in JMS?


Solution

  • You can use a QueueBrowser. You can create the browser using Session.createBrowser(Queue). As the API doc states:

    A client uses a QueueBrowser object to look at messages on a queue without removing them.

    The getEnumeration method returns a java.util.Enumeration that is used to scan the queue's messages. It may be an enumeration of the entire content of a queue, or it may contain only the messages matching a message selector.

    The Enumeration contains instances of the javax.jms.Message objects from the queue. Therefore you can inspect all the message's headers, properties, and body.