How to read JMS messages without consuming them -

2019-06-19 18:49发布

问题:

I would like to know if there is any way to read a jms and actibemq messages without consuming it ?? I know messages can be consumed from the queue , yet still I want ask this question .!!

回答1:

You can browse Messages on a Queue via the JMS QueueBrowser, or in ActiveMQ you can browse the contents over JMX or with the commands line tools:

ActiveMQ console tools

JMS QueueBrowser API

ActiveMQ JMX



回答2:

Rather than using Message-consumers you need to use the QueueBrowser class for doing this:

ConnectionFactory connectionFactory = 
    new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); 
Connection connection = 
    connectionFactory.createConnection("admin","admin");
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("Test");
QueueBrowser queueBrowser = session.createBrowser(queue);
Enumeration msgs = queueBrowser.getEnumeration();
while (msgs.hasMoreElements()) {
    //do your things here
}


回答3:

Another option is to "consume" the messages but just in a transaction, then when you're done, roll it back, but a browser might be better since that's "what it's for" as it were.

If you're just looking for a particular message, and manual will do, you can see (I think all, at least some of) the messages and their contents for an activemq by clicking on the "RSS feed" button in the UI. which basically dumps them all to the screen. The "atom feed" option seems to load faster than the "RSS" one FWIW.



标签: jms activemq