【发布时间】:2013-12-13 06:21:28
【问题描述】:
我想创建一些工具来管理队列中的消息。所以我希望能够从队列中获取所有消息(比如导出)并且不要从那里删除它。
我尝试使用 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());
但我不能收到超过 400 条消息。
我也使用了这样的方法:
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);
}
但messageInQueue.hasMoreElements() 总是返回false,尽管队列包含许多消息。
此外,如果我尝试使用消费者,它会检索所有消息,但会将其从队列中删除。
我尝试使用命令行工具从队列中导出消息:
activemq browse --amqurl tcp://localhost:55901 tmp_queue2 >> messages22222.txt
但是如果队列包含大约 1000000 条消息,它会抛出
Failed to execute main task. Reason: java.lang.OutOfMemoryError: GC overhead limit exceeded
那么,我怎样才能从队列中获取所有消息并且不从那里删除它们?
【问题讨论】:
-
当没有消息传递时,也许你应该先启动连接。在从浏览器迭代消息甚至创建浏览器对象之前调用 connection.start()。 JMS 规范说在消费消息之前需要启动连接,没有关于浏览应该如何工作的消息,但是由于浏览不是那么常见的活动,ActiveMQ 可能已经自己实现了它。
-
@Matej,谢谢,我忘了开始连接。当我启动它时,我可以从队列中读取大约 5000 条消息,但随后它在检索下一个元素时停止了很长时间......