查找队列是否有更多元素
我有以下代码。我正在尝试访问队列并消耗资源,但是我不知道队列中有多少元素以及剩余多少元素,所以我正在执行 while(true)
但在这种情况下,我无法关闭稍后会导致错误的连接,就像进程不会终止一样。即使我阻止了它。
我如何找到队列中是否有更多项目然后执行以下操作?我查看了 ActiveMqQueueBrowser 但它是内部的,所以我不能真正轻松地使用它。
你会怎么办?你有什么推荐?
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(subject);
MessageConsumer consumer = session.createConsumer(destination);
while(true){
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("message from queue : '" + textMessage.getText() + "'");
}
}
// unreachable code. compiler complains.
// connection.close();
I have the following code. I m trying to access a queue and consume resources, well i dont know how many elements are in the queue and how many left, so i am doing while(true)
but in this case, i cant close the connection which causes errors, later on, like the process doesnt die. even though i stopped it.
How can i find if there are more items in the queue then do the following ? i looked at ActiveMqQueueBrowser but it s internal so i cant really use it easily.
What would you do? What do u recommend?
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(subject);
MessageConsumer consumer = session.createConsumer(destination);
while(true){
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("message from queue : '" + textMessage.getText() + "'");
}
}
// unreachable code. compiler complains.
// connection.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你需要另一个线程。你怎么知道什么时候该听完呢?如果消费者队列不再有值,请尝试以下操作:
否则你是对的,循环将永远不会返回,并且当 JVM 关闭时你将无法正确关闭连接。
Sounds like you need to have another thread. How do you know when to be done listening? If its where the consumerQueue no longer has a value, try this:
Otherwise you are right, the loop will never return and you won;t be able to close the connections correctly when the JVM comes down.
那么您要等待多长时间才能收到更多消息?除非有任何保证不再有消息传入,否则您无法真正说它何时“完成”。
您可以为
receive()
设置超时,或调用receiveNoWait()
并在返回值为null
时中断,这意味着没有可用的消息在给定的时间段内。Well how long do you want to wait for more messages to arrive? Unless there's any guarantee that no more messages can come in, you can't really say when it's "done".
You can give a timeout to
receive()
or callreceiveNoWait()
and break if the return value isnull
, meaning there was no message available in the given time period.实际上,您有几个选项可以确定 ActiveMQ 中给定目标上可用的内容。代理公开了大量 JMX 信息,因此如果您对此感到满意,则可以连接并获取排队的消息数量。不涉及使用 JMX 的另一个选项是使用 Broker Statistics 插件。该插件允许您向代理发送带有 ReplyTo 目标集的消息,它将使用包含目标当前统计信息的消息进行响应。请参阅此处统计插件的文档。
You actually have a couple of options for determining what is available on a given destination in ActiveMQ. The broker exposes a great deal of information of JMX so if you are comfortable with that you can connect and get the number of Messages that are enqueued. Another option that doesn't involve using JMX is to use the Broker Statistics plugin. This plugin allows you to send a message to the broker with a ReplyTo destination set and it will respond with a Message containing the current statistics of the Destination. See the documentation for the statistics plugin here.