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?