Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveMQ get all messages from the queue

Tags:

java

activemq

jmx

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?

like image 202
evgeniy44 Avatar asked Nov 27 '13 14:11

evgeniy44


2 Answers

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.

like image 105
Tim Bish Avatar answered Sep 21 '22 19:09

Tim Bish


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

connection.start();

before iterate the queue.

like image 20
Andrea Bozzetto Avatar answered Sep 19 '22 19:09

Andrea Bozzetto