0

When using JmsTemplate to get the list of activemq queues, the number of queues reported changes

 private Set<String> queues = new HashSet<>();

     try(ActiveMQConnection connection = (ActiveMQConnection) 
      jmsTemplate.getConnectionFactory().createConnection()){
            connection.start();

            for(ActiveMQQueue queue : connection.getDestinationSource().getQueues()){
                queues.add(queue.getQueueName());
            }

            queues.remove(defaultReplyToQueue);

            log.info("findAllQueues found {}", queues.size());
            return queues;
        }
D R
  • 41
  • 1
  • 6
  • when the above code is invoked multiple times, the reported number of queues varies, sometimes it is the same, sometimes it is less , sometimes it is more – D R Jun 20 '19 at 19:34
  • Is there any other activity when you invoke the code – Simon Martinelli Jun 20 '19 at 19:37
  • the above code is invoked in response to a rest api call, and the result varies from time to time, the above code is invoked each time the rest api is called – D R Jun 21 '19 at 07:03

1 Answers1

0

This is hard to full answer with the limited detail given but I'd guess the issue comes down to the way the queues are populated in the destination source. They arrive in an asynchronous manner as the broker enumerates the existing queues. This implies that just opening a connection and immediately asking for all the queues will likely report random results as not all have arrived from the broker.

Tim Bish
  • 17,475
  • 4
  • 32
  • 42
  • Hi Tim, I have a service that needs to report the queues via a rest api. What I am observing is the rest api response returns a different list of queues sometimes, the above code is invoked for each rest call – D R Jun 21 '19 at 07:03
  • Can you please tell us what the differences are? – Simon Martinelli Jun 21 '19 at 07:48
  • the list of queue names is sometimes all the queue names as expected, but on later calls, even though the same queues exist, the queues reported is less, sometimes, 5 instead of 35, then sometimes, the correct number again – D R Jun 21 '19 at 08:03
  • Your approach is doomed to failure as the API you are using makes no guarantees that it will have all the data at any given time. You need to use a stable API like JMX to access the broker MBeans to get valid data. – Tim Bish Jun 21 '19 at 13:55