gpt4 book ai didi

java - ActiveMQ 浏览器需要很长时间才能执行最后一个 .hasMoreElements()

转载 作者:行者123 更新时间:2023-11-30 08:09:32 25 4
gpt4 key购买 nike

我尝试为 ActiveMQ 实现一个队列浏览器。
下面显示的代码应显示名为“Q1”的队列中的文本消息。里面有两条消息。一般来说,它可以工作,但最后一次 e.hasMoreElements() 调用最多需要 20 秒。我想每 500 毫秒更新一次列表。
为什么这么慢?

当我在浏览器 View 中按 http://localhost:8161/admin/browse.jsp?JMSDestination=Q1< 的“更新”时 e.hasMoreElements() 立即返回。这里发生了什么?如何实现“实时” View ?

        //init:
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
Queue queue = session.createQueue("Q1");

boolean run = true;
while (run) {
LOG.info("--------------------------------------------------------------------------------");
QueueBrowser browser = session.createBrowser(queue);
Enumeration e = browser.getEnumeration();
while (e.hasMoreElements()) { //<- very slow before returning false after last message. why?
Object o = e.nextElement();
if (o instanceof ActiveMQTextMessage) {
LOG.info(((ActiveMQTextMessage) o).getText());
} else {
LOG.info(o.toString());
}
}
Thread.sleep(500);
browser.close();
}

session.close();
connection.close();

最佳答案

在我之前的评论之后,我发现在连接工厂上调用 setTransactedIndividualAck(true) 可以解决问题

ActiveMQConnectionFactory cf2 = new ActiveMQConnectionFactory(...);
cf2.setTransactedIndividualAck(true);

我不确定这是否是解决问题的正确方法,但至少现在有效。请参阅此处 ActiveMQ 用户论坛上的消息: http://activemq.2283324.n4.nabble.com/JMS-browser-getEnumeration-hasMoreElements-takes-15s-on-last-element-td4709969.html

关于java - ActiveMQ 浏览器需要很长时间才能执行最后一个 .hasMoreElements(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30591192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com