user2853437
user2853437

Reputation: 780

How to retrieve created Topics (and Queues) of session with ActiveMQ 5.x

I created different topics using session.createTopic(topicname). How can I retrieve a list of all available topics in the session? I tried to use session.getStats(), but I can't iterate over it, to get the information I need.

Upvotes: 0

Views: 1085

Answers (2)

Justin Bertram
Justin Bertram

Reputation: 35038

The JMS API doesn't provide any method which provides a list of all destinations created with a javax.jms.Session.

I recommend you store the javax.jms.Destination instances you create in a local data structure (e.g. a java.util.ArrayList) in order to keep track of them.

Keep in mind that both javax.jms.Session.createTopic(String) and javax.jms.Session.createQueue(String) just create a client-side instance of a javax.jms.Topic or javax.jms.Queue respectively. They do not actually create a topic or queue on the broker. This is noted in the JavaDoc, e.g.:

Note that this method simply creates an object that encapsulates the name of a topic. It does not create the physical topic in the JMS provider. JMS does not provide a method to create the physical topic, since this would be specific to a given JMS provider. Creating a physical topic is provider-specific and is typically an administrative task performed by an administrator, though some providers may create them automatically when needed. The one exception to this is the creation of a temporary topic, which is done using the createTemporaryTopic method.

The getStats() method you cited is not part of the JMS API. It is unique to the ActiveMQ 5.x JMS client implementation. Furthermore, it does not track the names of destinations created with the corresponding session.

Upvotes: 1

Sebastian C.
Sebastian C.

Reputation: 131

Using the following command you will get the all the topics from the broker:

 Set<ActiveMQTopic> topics = activeMqConnection.getDestinationSource().getTopics();

But I don't think if it's want you want. Another option would be:

session.getSessionStats()
.getProducers()
.stream()
.map(JMSProducerStatsImpl::getDestination)
.collect(Collectors.toList());

Upvotes: 1

Related Questions