ActiveMQ get all messages from the queue
Asked Answered
M

2

8

I want to create some tool which will be able to manage messages inside queue. So I would like to be able to get all the messages from the queue(something like export) and don't remove it from there.

I tried to use JMX API:

  ObjectName mbeanNameQueue = new ObjectName("org.apache.activemq:type=Broker,brokerName=static-broker1,destinationType=Queue,destinationName=tmp_queue2");
  org.apache.activemq.broker.jmx.QueueViewMBean queueView = JMX.newMBeanProxy(mbsc, mbeanNameQueue, org.apache.activemq.broker.jmx.QueueViewMBean.class);
  System.out.println(queueView.browseAsTable());

But I can not get more than 400 messages.

Also I used such approach:

  ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:55901");
  ActiveMQConnection connection = (ActiveMQConnection)connectionFactory.createConnection();
  DestinationSource ds = connection.getDestinationSource();

  QueueSession queueSession = connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
  Queue queue = queueSession.createQueue("tmp_queue2");
  QueueBrowser browser = queueSession.createBrowser(queue);
  Enumeration<?> messagesInQueue = browser.getEnumeration();

  while (messagesInQueue.hasMoreElements()) {
      Message queueMessage = (Message) messagesInQueue.nextElement();
      System.out.println(queueMessage);
  }

but messagesInQueue.hasMoreElements() always returns false despite the queue contains many messages.

Also if I try to use consumer it retrieves all the messages but it removes it from the queue.

I tried to export messages from the queue using command line tool:

activemq browse --amqurl tcp://localhost:55901 tmp_queue2  >> messages22222.txt

But if queue contains about 1000000 messages it throws

Failed to execute main task. Reason: java.lang.OutOfMemoryError: GC overhead limit exceeded

So, how can I get all the messages form the queue and don't remove them from there?

Mieshamiett answered 27/11, 2013 at 14:41 Comment(2)
When no messages are delivered, maybe you should start the connection first. Call connection.start() before iterating the messages from the browser or even creating the browser object. The JMS specification says that connection start is required before consuming the messages, no word about how the browsing should work, but as the browsing is not so common activity, ActiveMQ has implemented it probably by itself.Smedley
@Matej, thanks, I forgot to start connection. When I started it I could read about 5000 messages from the queue but then It stopped for a long time while retrieving next element....Mieshamiett
E
8

The JMS Queue browser makes no guaruntee that it will return each and every message in the Queue. It provides a snapshot of the Messages but may not give them all. In the case of ActiveMQ there are limits on how many messages it will browse in order to reduce overhead. You can increase the limits, see maxBrowsePageSize, however there is still no way to ensure that for a very deep Queue you will get them all.

A better option would be to use a Camel route that sends messages targeted at some Queue into another process Queue and a mirror Queue that you can drain using standard JMS consumers or another Camel route. This way you can consume the mirrored messages at your own pace.

Elegant answered 27/11, 2013 at 15:54 Comment(0)
K
2

messagesInQueue.hasMoreElements() always returns false becouse you must call

connection.start();

before iterate the queue.

Kapoor answered 31/1, 2018 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.