Properly iterating over queues from ActiveMQ DestinationSource.getQueues response

前端 未结 2 711
一整个雨季
一整个雨季 2021-01-22 20:39

For some reason in the following code, destinationSource.getQueues() is returning a CopyOnWriteArraySet instead of a simple Set. This is a

相关标签:
2条回答
  • 2021-01-22 21:00

    As you say, it is the nature of CopyOnWriteArraySet to behave this way. The list of queues can change concurrently with your thread. By returning you a CopyOnWriteArraySet ActiveMQ is giving you a data structure that is safe to use in your thread (no change of ConcurrentModificationException) and one that will remain up-to-date.

    Since new queues could be added at any time there is no way to "wait" until they are all done.

    If you want to know when new queues are added and then do something in response, the best way is to listen for the appropriate ActiveMQ advisory message. This facility will let you respond to message queue additions, consumer and producer additions, as well as removals of the same. Think link has a code example.

    0 讨论(0)
  • 2021-01-22 21:08

    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 ;)

    0 讨论(0)
提交回复
热议问题