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?
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.
messagesInQueue.hasMoreElements() always returns false becouse you must call
connection.start();
before iterate the queue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With