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

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

本文介绍了具有XML配置的ActiveMQ嵌入式代理主题到队列的桥梁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标+进度

我对使用ActiveMQ将消息发布到某个主题并将其桥接到多个队列感兴趣.通过提供包含复合主题的xml-config,我设法通过命令行代理实现了这一点:

I'm interested in using ActiveMQ to publish a message to a topic and have it bridge to multiple queues. I have managed to achieve this with the command-line broker by providing an xml-config containing a composite topic:

<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 .

Using this start-up command: activemq start xbean:amq.xml. In this case, amq.xml is my active MQ XML configuration file. There is more on xml configuration here: http://activemq.apache.org/xml-configuration.html.

我的问题

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

If I publish a message to the topic above using the web console, it shows up in both queues as expected. But now I want to switch to using an embedded broker.

在下面的代码中,我可以说它正在使用我的amq.xml文件(因为当我对其进行更改时,我得到了相关的错误),但是当我发布到该主题时,队列上的接收将永远阻塞.如果我发布并接收到相同的主题或队列,则一切正常. 为什么我的综合主题在这里不起作用?

In the following code, I can tell that it is using my amq.xml file (as when I change it I get relevant errors), but when I publish to the topic, the receive on the queue blocks forever. If I publish and receive to the same topic or queue though, everything works fine. Why is my composite topic not working here?

//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"));

推荐答案

我在一个网站上发现了一个示例,该示例演示了如何以略有不同的方式生成消息.不幸的是,我关闭了选项卡,但找不到引用它的链接.

I found an example on a website which demonstrated how to produce a message in a slightly different way. Unfortunately, I closed the tab and can't find the link to reference it.

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

Either way, I combined that message-production style with my xbean configuration and now everything is working. Here's the working code:

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:世界您好!

第二季度:世界您好!

Q3:世界您好!

Q4:世界您好!

结束应用程序.

希望它可以帮助其他人!

Hopefully it helps someone else out!

这篇关于具有XML配置的ActiveMQ嵌入式代理主题到队列的桥梁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:04