查找队列是否有更多元素

发布于 2024-12-12 06:00:20 字数 1003 浏览 0 评论 0原文

我有以下代码。我正在尝试访问队列并消耗资源,但是我不知道队列中有多少元素以及剩余多少元素,所以我正在执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜味拾荒者 2024-12-19 06:00:20

听起来你需要另一个线程。你怎么知道什么时候该听完呢?如果消费者队列不再有值,请尝试以下操作:

while(true){
        Message message = consumer.receive(//some int timeout);
        if (message == null)
        {
            break;
        }

        if (message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) message;
            System.out.println("message from queue : '" + textMessage.getText() + "'");
        }
}

否则你是对的,循环将永远不会返回,并且当 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:

while(true){
        Message message = consumer.receive(//some int timeout);
        if (message == null)
        {
            break;
        }

        if (message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) message;
            System.out.println("message from queue : '" + textMessage.getText() + "'");
        }
}

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.

伤痕我心 2024-12-19 06:00:20

那么您要等待多长时间才能收到更多消息?除非有任何保证不再有消息传入,否则您无法真正说它何时“完成”。

您可以为 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 call receiveNoWait() and break if the return value is null, meaning there was no message available in the given time period.

淡淡の花香 2024-12-19 06:00:20

实际上,您有几个选项可以确定 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文