You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

配置ActiveMQ Artemis启用AMQP后Node.js Pub/Sub收发异常排查

问题排查:Node.js通过AMQP连接ActiveMQ Artemis无法收发消息

我有一台运行正常的ActiveMQ Artemis服务器,现需为Node.js应用启用AMQP协议以实现发布/订阅(Pub/Sub)功能。目前Node.js的发布端与订阅端可成功连接到localhost:5672,但无法发送或接收消息,请求排查该问题的原因。

我参考了AMQP Rhea项目的持久化订阅示例(publisher.js与subscriber.js)。

以下是我的ActiveMQ Artemis服务器Java实现代码:

//this is my jmsserver
/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package testSupport.artemis.server;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.config.Configuration;
import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.ActiveMQServers;
import org.apache.activemq.artemis.core.server.QueueConfig;
import org.apache.activemq.artemis.core.server.impl.AddressInfo;

public class JMSServer {
    private static int qId = 0;

    private ActiveMQServer server;
    private String errMsg = "";

    /**
     * Factory method to create an instance of a JMS Server
     * @param topics list of topics to add to the server
     * @return
     */
    public static JMSServer createJMSServer(List<String> topics) {
        JMSServer s = new JMSServer();
        s.start();
        if (topics != null) {
            s.setTopics(topics);
        }
        return s;
    }

    /**
     * Factory method to create an instance of a JMS Server
     * @return
     */
    public static JMSServer createJMSServer() {
        return createJMSServer(null);
    }

    /**
     * Updates the server config with settings required to connect invm or from
     * another process on localhost
     * 
     * @param config
     */
    public static void updateConfig(Configuration config) {
        try {
            config.setPersistenceEnabled(false)
                  .setSecurityEnabled(false)
                  .addAcceptorConfiguration("tcp", "tcp://localhost:61616")
                  .addAcceptorConfiguration("amqp", "tcp://localhost:5672");

            // SpringBoot may have already created an invm connector at 0, so only add one if it doesnt exist
            if (config.getAcceptorConfigurations().stream().noneMatch(poo -> poo.getParams().containsKey("serverId") &&
                    Integer.valueOf(poo.getParams().get("serverId").toString()) == 0)) {
                config.addAcceptorConfiguration("invm", "vm://0");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Default Constructor
     */
    public JMSServer() {
        try {
            Configuration config = new ConfigurationImpl();
            updateConfig(config);
            server = ActiveMQServers.newActiveMQServer(config);
        } catch (Exception ex) {
            errMsg = ex + ": " + ex.getMessage();
            ex.printStackTrace();
        }
    }

    /**
     * Start the JMS Server
     * @return
     */
    public boolean start() {
        boolean success = false;
        try {
            server.start();

            for (int i = 0; i < 50; i++) {
                Thread.sleep(100);
                if (server.isActive()) {
                    success = true;
                    break;
                }
            }
        } catch (Exception ex) {
            errMsg = ex + ": " + ex.getMessage();
            ex.printStackTrace();
        }

        return success;
    }

    /**
     * Stop the JMS Server
     */
    public void stop() {
        try {
            server.stop();
        } catch (Exception ex) {
            errMsg = ex + ": " + ex.getMessage();
            ex.printStackTrace();
        }
    }

    /**
     * Get the Error Message
     * @return
     */
    public String getErrMsg() {
        return errMsg;
    }

    /**
     * Set a list of topics to add to this server
     * @param topics
     * @return
     */
    public boolean setTopics(List<String> topics) {
        boolean success = true;

        if (!server.isActive()) {
            errMsg = "Topics cannot be set until the server has been started.";
            return false;
        }

        // add the topics
        for (String t : topics) {
            try {
                SimpleString addr = SimpleString.toSimpleString(t);
                QueueConfig qcfg = QueueConfig.builderWith(qId, addr, addr).autoCreated(false)
                        .autoDelete(false)
                        .durable(true)
                        .build();

                server.getQueueFactory().createQueueWith(qcfg);
                server.addAddressInfo(new AddressInfo(addr, RoutingType.MULTICAST));
                qId++;
            } catch (Exception ex) {
                errMsg = ex + ": " + ex.getMessage();
                success = false;
            }
        }
        return success;
    }

}

更新:
根据建议,我已将artemis-amqp-protocol添加到类路径中,在Gradle中引入了匹配版本的依赖:

dependencies {
    implementation 'org.apache.activemq:artemis-jms-server:2.22.0'
    implementation 'org.apache.activemq:artemis-amqp-protocol:2.22.0'
    // 其他依赖...
}

内容的提问来源于stack exchange,提问作者Potion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 18:20:56