简体   繁体   English

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

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

I created different topics using session.createTopic(topicname) .我使用session.createTopic(topicname)创建了不同的主题。 How can I retrieve a list of all available topics in the session?如何检索 session 中所有可用主题的列表? I tried to use session.getStats() , but I can't iterate over it, to get the information I need.我尝试使用session.getStats() ,但我无法迭代它来获取我需要的信息。

The JMS API doesn't provide any method which provides a list of all destinations created with a javax.jms.Session . JMS API 不提供任何方法来提供使用javax.jms.Session创建的所有目的地的列表。

I recommend you store the javax.jms.Destination instances you create in a local data structure (eg a java.util.ArrayList ) in order to keep track of them.我建议您将创建的javax.jms.Destination实例存储在本地数据结构中(例如java.util.ArrayList )以便跟踪它们。

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.请记住, javax.jms.Session.createTopic(String)javax.jms.Session.createQueue(String)只是分别创建javax.jms.Topicjavax.jms.Queue的客户端实例。 They do not actually create a topic or queue on the broker.他们实际上并没有在代理上创建主题或队列。 This is noted in the JavaDoc, eg:这在 JavaDoc 中有说明,例如:

Note that this method simply creates an object that encapsulates the name of a topic.请注意,此方法只是创建一个封装主题名称的 object。 It does not create the physical topic in the JMS provider.它不会在 JMS 提供者中创建物理主题。 JMS does not provide a method to create the physical topic, since this would be specific to a given JMS provider. JMS 不提供创建物理主题的方法,因为这将特定于给定的 JMS 提供者。 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.一个例外是创建一个临时主题,这是使用 createTemporaryTopic 方法完成的。

The getStats() method you cited is not part of the JMS API.您引用的getStats()方法不是 JMS API 的一部分。 It is unique to the ActiveMQ 5.x JMS client implementation.它是 ActiveMQ 5.x JMS 客户端实现所独有的。 Furthermore, it does not track the names of destinations created with the corresponding session.此外,它不会跟踪使用相应 session 创建的目的地的名称。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM