如何使用 amq.topic 在 Apache Qpid 中发布/订阅消息
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的消费者指定的绑定密钥与生产者使用的路由密钥不同。
您的生产者代码:
您的消费者代码:
请注意每个键的第一个字符的大小写差异。您的生产者使用
test.A
,而您的消费者使用Test.A
,并且由于密钥区分大小写,因此它们被认为是完全不同的。这就是为什么你的制作人不会收到任何消息。Your consumer specifies a binding key that is different than the routing key used by the producer.
Your producer code:
Your consumer code:
Notice the difference in case for the first character of each key. Your producer uses
test.A
and your consumer usesTest.A
, and since the keys are case-sensitive they are considered completely different. That's why your producer won't get any messages.您的绑定密钥应该是 test.# 或 test.*
# 和 * 之间的区别,请点击此链接 http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_MRG/2/html/Messaging_User_Guide/chap-Messaging_User_Guide-Exchanges.html#sect-Messaging_User_Guide-Exchange_Types-Topic_Exchange
your binding key should be test.# or test.*
the differencees between # and *, follow this link http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_MRG/2/html/Messaging_User_Guide/chap-Messaging_User_Guide-Exchanges.html#sect-Messaging_User_Guide-Exchange_Types-Topic_Exchange