ActiveMQ Artemis - 使用 JMS 获取可用地址列表

ActiveMQ Artemis - Obtain list of available Addresses using JMS

我最近不得不为我正在处理的项目从 ActiveMQ 5.X 切换到 ActiveMQ Artemis。我有一个函数可以读取可用主题并在 MySQL 数据库中填充 table,但该函数不再适用于 Artemis 方法的变化。

基本上,我需要获取可用地址列表,而不是获取可用主题列表(又名多播队列),以便我的消费者可以连接并为该地址创建同名多播队列。

这就是我需要此功能的原因:

这是我以前的代码,它与以前的 ActiveMQ 5.X:

public class ActiveMQ {

    public ActiveMQ(String amq_url) throws JMSException, NamingException{
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(amq_url);      
        this.connection = (ActiveMQConnection) connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        connection.start();
    }

    public void useTopic(String topicName) throws JMSException{
        this.topic = session.createTopic(topicName);
        this.producer = session.createConsumer(topic);
    }

    
    public void readAvailableTopics() throws JMSException{ 
         
        Set<ActiveMQTopic> topics = connection.getDestinationSource().getTopics();
        System.out.println(topics.toString());
        return topics;
    }
 

readAvailableTopics() 方法是有问题的方法。

我已经在消费者和生产者端尝试了 .getTopics() 和 .getQueues(),它们总是 return 一个空集。我找不到 JMS 的任何文档来获取可用地址。

如有任何帮助,我们将不胜感激。

ActiveMQ 5 客户端通过 advisory messages. Since ActiveMQ Artemis 2.18 the advisory messages support is disabled by default, see ARTEMIS-3414 更新主题和队列列表。要启用建议消息支持,请将 supportAdvisory 参数添加到接受器,即

<acceptor name="artemis">tcp://0.0.0.0:61616?protocols=OPENWIRE;supportAdvisory=true</acceptor>

JMS 规范未涵盖此类管理操作,因此每个代理实现都将采用不同的方式提供访问权限。 ActiveMQ Artemis 在许多方面与 ActiveMQ“Classic”相似,但访问管理操作不同。也就是说,你 可以 use JMS to invoke management operations。您可以使用如下代码列出所有地址:

import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueRequestor;
import javax.jms.QueueSession;
import javax.jms.Session;
...
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.api.jms.management.JMSManagementHelper;
import org.apache.activemq.artemis.api.core.management.ResourceNames;
...

QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");
QueueRequestor requestor = new QueueRequestor(session, managementQueue);
Message m = session.createMessage();
JMSManagementHelper.putOperationInvocation(m, ResourceNames.BROKER, "getAddressNames");
Message reply = requestor.request(m);
boolean success = JMSManagementHelper.hasOperationSucceeded(reply);
if (success) {
   Object[] addresses = (Object[]) JMSManagementHelper.getResult(reply);
   for (Object address : addresses) {
      System.out.println(address);
   }
}