如何使用 amq.topic 在 Apache Qpid 中发布/订阅消息

发布于 2024-12-05 21:33:22 字数 1401 浏览 0 评论 0原文

我有一个 C++ 发布者发送这样的消息:

Connection connection;
connection.open("127.0.0.1", 5672);
Session session = connection.createSession();
Message msg;
msg.setData("TestAMsg");
msg.getDeliveryProperties().setRoutingKey("test.A");
session.messageTransfer(arg::content = message, 
                         arg::destination = "amq.topic");
msg.setData("TestBMsg");
msg.getDeliveryProperties().setRoutingKey("test.B");
session.messageTransfer(arg::content = message, 
                         arg::destination = "amq.topic");

我有一个像这样的 Java 订阅者:

AMQConnectionFactory connectionFactory = new 
                AMQConnectionFactory("amqp://guest:guest@myhost/test?
                                     brokerlist='tcp://127.0.0.1:5672'");
AMQConnection connection = (AMQConnection) 
                             connectionFactory.createConnection();
org.apache.qpid.jms.Session session = connection.createSession(false, 
                                             Session.AUTO_ACKNOWLEDGE);
AMQTopic destination = (AMQTopic) 
        AMQDestination.createDestination("topic://amq.topic//exclusive='false'?
                                          bindingkey='Test.A'");
MessageConsumer messageAConsumer = session.createConsumer(destination);
Message message_ = messageConsumer_.receive();

在上面的代码中没有收到消息。我很困惑这将如何运作?对于消费者来说,绑定 URL 的正确形式是什么?我缺少什么?

I have a C++ publisher to send messages like this:

Connection connection;
connection.open("127.0.0.1", 5672);
Session session = connection.createSession();
Message msg;
msg.setData("TestAMsg");
msg.getDeliveryProperties().setRoutingKey("test.A");
session.messageTransfer(arg::content = message, 
                         arg::destination = "amq.topic");
msg.setData("TestBMsg");
msg.getDeliveryProperties().setRoutingKey("test.B");
session.messageTransfer(arg::content = message, 
                         arg::destination = "amq.topic");

And I have a Java subscriber like this:

AMQConnectionFactory connectionFactory = new 
                AMQConnectionFactory("amqp://guest:guest@myhost/test?
                                     brokerlist='tcp://127.0.0.1:5672'");
AMQConnection connection = (AMQConnection) 
                             connectionFactory.createConnection();
org.apache.qpid.jms.Session session = connection.createSession(false, 
                                             Session.AUTO_ACKNOWLEDGE);
AMQTopic destination = (AMQTopic) 
        AMQDestination.createDestination("topic://amq.topic//exclusive='false'?
                                          bindingkey='Test.A'");
MessageConsumer messageAConsumer = session.createConsumer(destination);
Message message_ = messageConsumer_.receive();

No messages received in above code. I am very confused how this will work? What is the right form of bingding URL for consumers? What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法唧唧 2024-12-12 21:33:22

您的消费者指定的绑定密钥与生产者使用的路由密钥不同。

您的生产者代码:

msg.getDeliveryProperties().setRoutingKey("test.A");

您的消费者代码:

AMQTopic destination = (AMQTopic) 
        AMQDestination.createDestination("topic://amq.topic//exclusive='false'?
                                          bindingkey='Test.A'");

请注意每个键的第一个字符的大小写差异。您的生产者使用 test.A ,而您的消费者使用 Test.A ,并且由于密钥区分大小写,因此它们被认为是完全不同的。这就是为什么你的制作人不会收到任何消息。

Your consumer specifies a binding key that is different than the routing key used by the producer.

Your producer code:

msg.getDeliveryProperties().setRoutingKey("test.A");

Your consumer code:

AMQTopic destination = (AMQTopic) 
        AMQDestination.createDestination("topic://amq.topic//exclusive='false'?
                                          bindingkey='Test.A'");

Notice the difference in case for the first character of each key. Your producer uses test.A and your consumer uses Test.A, and since the keys are case-sensitive they are considered completely different. That's why your producer won't get any messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文