gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:44:35 24 4
gpt4 key购买 nike

我的目标+进度

我有兴趣使用 ActiveMQ 将消息发布到主题并将其桥接到多个队列。我通过提供包含复合主题的 xml 配置,成功地使用命令行代理实现了此目的:

<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 是我的 Activity 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"));

最佳答案

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

无论哪种方式,我将消息生成风格与我的 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"));

输出:

Posting message to topic:

Q1: Hello World!

Q2: Hello World!

Q3: Hello World!

Q4: Hello World!

Ending application.

希望它对其他人有帮助!

关于java - 具有 XML 配置的 ActiveMQ 嵌入式代理主题到队列桥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26063603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com