如何使用 ActiveMQ 5.x 检索创建的会话主题(和队列)

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

我使用 session.createTopic(topicname) 创建了不同的主题。如何检索会话中所有可用主题的列表?我尝试使用 session.getStats(),但我无法遍历它来获取我需要的信息。

JMS API 不提供任何方法来提供使用 javax.jms.Session 创建的所有目的地的列表。

我建议您将创建的 javax.jms.Destination 个实例存储在本地数据结构中(例如 java.util.ArrayList)以便跟踪它们。

请记住,javax.jms.Session.createTopic(String)javax.jms.Session.createQueue(String) 只是分别创建 javax.jms.Topicjavax.jms.Queue 的客户端实例。他们 不会 实际上在代理上创建主题或队列。这在 JavaDoc 中有说明,例如:

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.

您引用的 getStats() 方法不是 JMS API 的一部分。它是 ActiveMQ 5.x JMS 客户端实现所独有的。此外,它不会跟踪使用相应会话创建的目的地的名称。

使用以下命令,您将从代理获取所有主题:

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

但我不认为这是你想要的。另一种选择是:

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