Java ActiveMQ 从队列中获取所有消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20245349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ActiveMQ get all messages from the queue
提问by evgeniy44
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:
我尝试使用 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.
但我无法收到超过 400 条消息。
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.
但尽管队列包含许多消息,但 messagesInQueue.hasMoreElements() 始终返回 false。
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
但是如果队列包含大约 1000000 条消息,它会抛出
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?
那么,如何从队列中获取所有消息而不将它们从那里删除?
采纳答案by Tim Bish
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.
JMS 队列浏览器不保证它会返回队列中的每条消息。它提供了消息的快照,但可能不会全部提供。在 ActiveMQ 的情况下,为了减少开销,它会浏览多少消息是有限制的。您可以增加限制,请参阅 maxBrowsePageSize,但是仍然无法确保对于非常深的队列,您将获得所有限制。
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.
更好的选择是使用 Camel 路由,将针对某个 Queue 的消息发送到另一个进程 Queue 和一个镜像队列,您可以使用标准 JMS 消费者或另一个 Camel 路由将其排出。通过这种方式,您可以按照自己的节奏使用镜像消息。
回答by Andrea Bozzetto
messagesInQueue.hasMoreElements() always returns false becouse you must call
messagesInQueue.hasMoreElements() 总是返回 false 因为你必须调用
connection.start();
before iterate the queue.
在迭代队列之前。