Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ActiveMQ从队列中获取所有消息_Java_Activemq_Jmx - Fatal编程技术网

Java ActiveMQ从队列中获取所有消息

Java ActiveMQ从队列中获取所有消息,java,activemq,jmx,Java,Activemq,Jmx,我想创建一些能够管理队列中消息的工具。因此,我希望能够从队列中获取所有消息(类似于导出),而不从队列中删除它 我尝试使用JMX API: ObjectName mbeanNameQueue = new ObjectName("org.apache.activemq:type=Broker,brokerName=static-broker1,destinationType=Queue,destinationName=tmp_queue2"); org.apache.activemq.bro

我想创建一些能够管理队列中消息的工具。因此,我希望能够从队列中获取所有消息(类似于导出),而不从队列中删除它

我尝试使用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);
  }
但若队列包含大约1000000条消息,它就会抛出

Failed to execute main task. Reason: java.lang.OutOfMemoryError: GC overhead limit exceeded

那么,如何才能从队列中获取所有消息,而不将它们从队列中删除?

JMS队列浏览器无法保证它将返回队列中的每一条消息。它提供消息的快照,但可能不会提供所有消息。对于ActiveMQ,为了减少开销,它将浏览多少消息是有限制的。您可以增加限制,请参见maxBrowsePageSize,但是仍然无法确保对于非常深的队列,您将获得所有限制


更好的选择是使用驼峰路由,该路由将针对某个队列的消息发送到另一个进程队列和镜像队列中,您可以使用标准JMS使用者或另一个驼峰路由来耗尽这些队列。这样,您可以按照自己的速度使用镜像消息

messagesInQueue.hasMoreElements()总是返回false,因为您必须调用

connection.start();

在迭代队列之前。

当没有消息传递时,您可能应该首先启动连接。在迭代来自浏览器的消息或甚至创建浏览器对象之前,调用connection.start()。JMS规范说,在使用消息之前需要启动连接,没有说明浏览应该如何工作,但由于浏览不是很常见的活动,ActiveMQ可能是自己实现的。@Matej,谢谢,我忘记启动连接了。当我启动它时,我可以从队列中读取大约5000条消息,但在检索下一个元素时,它停止了很长一段时间。。。。
connection.start();