具有XML配置的ActiveMQ嵌入式代理主题到队列桥接

时间:2014-09-26 15:57:01

标签: java jms activemq messagebroker

我的目标+进度

我对使用ActiveMQ向主题发布消息并将其桥接到多个队列感兴趣。我已经设法通过提供包含复合主题的xml-config来实现命令行代理:

<destinationInterceptors>
    <virtualDestinationInterceptor>
        <virtualDestinations>
            <compositeTopic name="LOCAL.EC.T">
                <forwardTo>
                    <queue physicalName="LOCAL.EC.Q.1" />
                    <queue physicalName="LOCAL.EC.Q.2" />                       
                </forwardTo>
            </compositeTopic>
        </virtualDestinations>
    </virtualDestinationInterceptor>
</destinationInterceptors>

使用此启动命令:activemq start xbean:amq.xml。在这种情况下,amq.xml是我的活动MQ XML配置文件。 xml配置还有更多内容:http://activemq.apache.org/xml-configuration.html

我的问题

如果我使用Web控制台向上述主题发布消息,它会按预期显示在两个队列中。但现在我想切换到使用嵌入式代理。


在下面的代码中,我可以告诉它正在使用我的amq.xml文件(因为当我更改它时会出现相关错误),但是当我发布到主题时,队列上的接收永远阻止。如果我发布并接收相同的主题或队列,一切正常。 为什么我的复合主题在这里不起作用


//Create the broker using the xbean configuration and start it.
brokerService = BrokerFactory.createBroker(new URI("xbean:amq.xml"));
brokerService.setBrokerName("localhost");
brokerService.setPersistent(false);
brokerService.setDeleteAllMessagesOnStartup(true);
brokerService.setUseJmx(false);
brokerService.start();

//Create the connection factory and JMS template.
connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL("vm://localhost?create=false&jms.redeliveryPolicy.maximumRedeliveries=-1");

//Send the message to the topic.
template = new JmsTemplate(connectionFactory);
ActiveMQMapMessage message = new ActiveMQMapMessage();
message.setString("batch", "Hello World!");
template.convertAndSend("LOCAL.EC.T",message);

//Read the message from the queues.
final Message receive = template.receive("LOCAL.EC.Q.1");
MapMessage received = (MapMessage)receive;
System.out.println(received.getString("batch"));  

1 个答案:

答案 0 :(得分:2)

我在网站上找到了一个示例,演示了如何以稍微不同的方式生成消息。不幸的是,我关闭了标签,但找不到引用它的链接。

无论哪种方式,我将该消息生成样式与我的xbean配置相结合,现在一切正常。这是工作代码:

brokerService = BrokerFactory.createBroker(new URI("xbean:amq.xml"));
brokerService.setPersistent(false);
brokerService.setDeleteAllMessagesOnStartup(true);
brokerService.setUseJmx(false);
brokerService.start();

connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
template = new JmsTemplate(connectionFactory);

//Create a connection.
Connection connection = connectionFactory.createConnection();
connection.start();

//Create a session.
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

//Create a topic and publish to it - it should be linked to 4 queues by the configuration.
System.out.println("Posting message to topic:");
Destination destination = session.createTopic("LOCAL.EC.T");
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
ActiveMQMapMessage message = new ActiveMQMapMessage();
message.setString("batch", "Hello World!");
producer.send(message);

//Read the message from the queues.
System.out.println("Q1: " + ((MapMessage)receive("LOCAL.EC.Q.1")).getString("batch"));
System.out.println("Q2: " + ((MapMessage)receive("LOCAL.EC.Q.2")).getString("batch"));
System.out.println("Q3: " + ((MapMessage)receive("LOCAL.EC.Q.3")).getString("batch"));
System.out.println("Q4: " + ((MapMessage)receive("LOCAL.EC.Q.4")).getString("batch"));

<强>输出:

  

向主题发布消息:

     

Q1:Hello World!

     

Q2:Hello World!

     

Q3:Hello World!

     

问题4:Hello World!

     

结束申请。

希望它可以帮助其他人!