从ActiveMQ DestinationSource.getQueues响应正确地遍历队列 [英] Properly iterating over queues from ActiveMQ DestinationSource.getQueues response

查看:173
本文介绍了从ActiveMQ DestinationSource.getQueues响应正确地遍历队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下代码中的某些原因, destinationSource.getQueues()返回的是 CopyOnWriteArraySet 而不是简单的设置。这是一个问题,因为for循环在 Set 已满之前开始处理,并且由于 CopyOnWriteArraySet 的性质,只会在循环之前处理 Set 中的项目。我知道我可以在其中扔一个 Thread.sleep(),但这不能解决潜在的问题。是否有任何理由将其作为 CopyOnWriteArraySet 而不是 Set 返回?还有什么方法可以迭代 CopyOnWriteArraySet 以确保所有项目都被覆盖,甚至包括在迭代过程中添加的项目?

For some reason in the following code, destinationSource.getQueues() is returning a CopyOnWriteArraySet instead of a simple Set. This is a problem because the for loop begins to process before the Set is full and due to the nature of CopyOnWriteArraySet it will only process the items in the Set before the loop. I know I can throw a Thread.sleep() in there but that doesn't fix the underlying problem. Is there any reason it would be returned as a CopyOnWriteArraySet instead of a Set? Also is there any way to iterate over a CopyOnWriteArraySet to ensure all items would be covered, even ones added during the iteration?

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
ActiveMQConnection activeMQConnection = (ActiveMQConnection) connectionFactory.createConnection();
activeMQConnection.start();
DestinationSource destinationSource = activeMQConnection.getDestinationSource();

Set<ActiveMQQueue> queues = destinationSource.getQueues();

for(ActiveMQQueue queue : queues) {
  queueNames.add(queue.getPhysicalName());
}

activeMQConnection.close()

编辑:此处是我想出的解决方案,虽然它不是完美的,但它可以确保您将所有队列都排满,直到队列之间添加的时间超过1秒为止。

Here is the solution I came up with, while its not perfect it ensures that you will get all the queues up until there is more than 1 second between queues being added.

    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

    ActiveMQConnection activeMQConnection = (ActiveMQConnection) connectionFactory.createConnection();

    activeMQConnection.start();

    DestinationSource destinationSource = activeMQConnection.getDestinationSource();

    Set<ActiveMQQueue> queues = destinationSource.getQueues();

    do {
        for(ActiveMQQueue queue : queues) {
            String physcialName = queue.getPhysicalName();
            if(!queueNames.contains(physcialName)) {
                queueNames.add(physcialName);
            }
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log(e.toString());
        }
    }while(queueNames.size() < queues.size());

    activeMQConnection.close();


推荐答案

我遇到相同的问题,即从连接。
每当我从DestinationSource获取队列,然后在该集合上进行迭代(foreach)时,
i就会获得不同数量的队列(在迭代循环中,我总是比初始集合中的队列更多)。 / p>

I had the same issue with getting all queues from a connection. Whenever i got the queues from the DestinationSource and then iterated afterwards (foreach) over this set, i got different number of queues (In the iteration loop i always get more queues than in the initial set).

DestinationSource ds = connection.getDestinationSource();
Set<ActiveMQQueue> queues = ds.getQueues();
log.debug("Found '" + queues.size() + "' queues");
for (ActiveMQQueue queue : queues) {...}

然后,我添加了一个像这样的目标源的侦听器

Then, i added a listener to the destination source like this

DestinationSource ds = connection.getDestinationSource();
Set<ActiveMQQueue> queues = ds.getQueues();
// Add listener:
ds.setDestinationListener(event -> event.hashCode());
log.debug("Found '" + queues.size() + "' queues");
for (ActiveMQQueue queue : queues) {...}

从现在开始,我总是获得正确数量的队列,并且可以遍历整个队列。

From now on, i always get the right number of queues and can iterate over the complete set.

尽管如此,我真的不知道为什么;)

Allthough, i don't really know why ;)

这篇关于从ActiveMQ DestinationSource.getQueues响应正确地遍历队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆