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

嵌入式ActiveMQ Artemis报错:队列不存在问题排查求助

嵌入式ActiveMQ Artemis多播队列连接报错“队列不存在”解决方法

问题根源

你的代码存在两个核心问题:

  • 配置时机错误:启动Broker后再添加地址、队列配置,修改不会生效,因为Broker启动时已加载初始配置。
  • 队列与地址绑定不匹配:补充代码中,队列的setAddress("q1")与多播地址名address1不一致,导致队列未关联到目标多播地址。

修正后的完整代码

public class Example {

    public Example() throws Exception {
        EmbeddedActiveMQ broker = new EmbeddedActiveMQ();
        broker.setConfigResourcePath("file:cfg/broker.xml");

        // 先配置地址和队列,再启动Broker
        CoreAddressConfiguration cac = new CoreAddressConfiguration();
        cac.setName("address1");
        cac.addRoutingType(RoutingType.MULTICAST);
        
        // 队列绑定到地址address1,名称设为q1
        QueueConfiguration qc = QueueConfiguration.of("q1")
                .setAddress("address1")
                .setRoutingType(RoutingType.MULTICAST);
        cac.addQueueConfiguration(qc);
        
        broker.getConfiguration().addAddressConfiguration(cac);
        
        // 启动Broker
        broker.start();
    }

    public static void main(String[] args) throws Exception {
        Example example = new Example();

        ServerLocator locator = ActiveMQClient.createServerLocator("vm://0");
        ClientSessionFactory factory = locator.createSessionFactory();
        ClientSession session = factory.createSession();
        session.start();

        // 连接队列q1,而非地址
        ClientConsumer consumer = session.createConsumer("q1");
        consumer.setMessageHandler(new MessageHandler() {
            @Override
            public void onMessage(ClientMessage message) {
                System.out.println("got message: " + message);
            }
        });
    }
}

关键修改说明

  1. 调整配置顺序:将地址、队列的配置代码移到broker.start()之前,确保Broker启动时加载这些配置。
  2. 修正队列地址绑定:队列的setAddress参数改为多播地址名address1,让队列关联到正确的多播地址上。
  3. 消费者连接队列:消费者需要连接队列名q1,而非地址名,因为Artemis中消费者是绑定到队列而非地址的。

配置文件保留原内容即可

你的broker.xml配置无需修改,保持原有设置即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 18:15:15