Apache Qpid - 在消息级别设置路由密钥

发布于 2025-01-03 07:52:12 字数 288 浏览 2 评论 0 原文

是否有任何选项可以在 Apache Qpid 中的消息级别设置路由密钥。 我目前的做法是

  1. 在地址字符串中指定路由键。使用此目标地址创建生产者。

    topic = (主题) context.lookup("目的地"); sender = session.createProducer(topic);

  2. 通过生产者发送消息。

这样所有的消息都具有相同的路由密钥。我想要实现的是为每条消息单独设置一个路由键。

让我知道这是否可以做到

Is there any option to set a routing key at message level in Apache Qpid.
The way I currently do is

  1. Specify routing key in address string. Create a producer with this destination address.

    topic = (Topic) context.lookup("destination");
    sender = session.createProducer(topic);

  2. Send messages through the producer.

This way all the messages have the same routing key. What I want to achieve is set a routing key for each message individually.

Let me know if this can be done

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

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

发布评论

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

评论(4

葵雨 2025-01-10 07:52:12

通过指定每条消息的主题可以轻松完成此操作。
当使用 0-10 协议时,Qpid 地址方案定义的“主题”将映射到主题的路由键。

Message m = ssn.createMessage();
m.setStringProperty("qpid.subject", "my-subject");
prod.send(m);

这允许您使用标准 JMS 接口,同时仍然使用 Qpid 附加组件。

This can be easily done by specifying a per message subject.
The "subject" as defined by the Qpid address scheme, would map to the routing key for Topics when using the 0-10 protocol.

Message m = ssn.createMessage();
m.setStringProperty("qpid.subject", "my-subject");
prod.send(m);

This allows you to use standard JMS interfacing while still using the Qpid add-ons.

寂寞陪衬 2025-01-10 07:52:12

我首先尝试这样做:

Message message = session.createTextMessage("test");
AMQMessageDelegate_0_10 delegate = (AMQMessageDelegate_0_10) ((AbstractJMSMessage)message).getDelegate();
delegate.getDeliveryProperties().setRoutingKey("rk1");

但是在发送消息时,它仍然具有在我的目的地中设置的路由密钥。

查看 Qpid 的 Java 源代码,我不确定这目前是否可行。如果您查看 https://svn.apache.org/repos/asf/qpid/trunk/qpid/java/client/src/main/java/org/apache/qpid/client/BasicMessageProducer_0_10.java,您将看到如下代码:

String routingKey = destination.getRoutingKey().toString();
if (deliveryProp.getRoutingKey() == null || ! deliveryProp.getRoutingKey().equals(routingKey))
{
        deliveryProp.setRoutingKey(routingKey);
}

不幸的是,这似乎意味着即使您在消息上设置了路由键,如果消息的路由键与目标的路由键不同,它也会被目标的路由键替换目的地的路由键。

也许有办法做到这一点,但不幸的是,我对 Qpid 的 Java 方面不太熟悉。您最好的选择可能是在 Qpid 用户邮件列表上询问(请参阅 http://qpid.apache.org/ mailing_lists.html 获取信息)。

I first tried doing this:

Message message = session.createTextMessage("test");
AMQMessageDelegate_0_10 delegate = (AMQMessageDelegate_0_10) ((AbstractJMSMessage)message).getDelegate();
delegate.getDeliveryProperties().setRoutingKey("rk1");

But upon sending the message, it still had the routing key that was set in my Destination.

Looking at Qpid's Java source code, I'm not sure this is currently possible. If you look at https://svn.apache.org/repos/asf/qpid/trunk/qpid/java/client/src/main/java/org/apache/qpid/client/BasicMessageProducer_0_10.java, you'll see code like this:

String routingKey = destination.getRoutingKey().toString();
if (deliveryProp.getRoutingKey() == null || ! deliveryProp.getRoutingKey().equals(routingKey))
{
        deliveryProp.setRoutingKey(routingKey);
}

This unfortunately appears to mean that even if you set a routing key on your message, it will be replaced by the destination's routing key if the message's routing key differs from the destination's routing key.

There may be a way to do this, but I'm not super familiar with the Java side of Qpid, unfortunately. Your best bet is probably to ask on the Qpid User Mailing List (see http://qpid.apache.org/mailing_lists.html for info).

挽梦忆笙歌 2025-01-10 07:52:12

您可以将 setJMSReplyTo("address") 设置为路由键。
我已使用它来获取所需响应渠道的回复。

You can set the setJMSReplyTo("address") to the routing key.
I have used it to get replies on required response channel.

初心 2025-01-10 07:52:12

您应该能够通过使用 AMQP Topic 来实现您想要的目标。将routingKey 设置为“my-topic”之类的内容。根据设计将您的消费者设置为不同的主题,例如“subject-1”,“subject-2”,...

对于生产者,每个人都可以发送具有不同主题的消息,例如“my-topic.subject-1 ", "my-topic.subject-2", ...使用它们作为生产者的routingKey。

示例代码如下所示:

//set up message consumer for "subject-1"
AMQTopic topic-1 = new AMQTopic(new AMQShortString("amq.topic"), new AMQShortString("my-topic.subject-1), false, null, true);
MessageConsumer consumer = session.createConsumer(topic-1);
Message message = consumer.receive();
...

//set up message producer for "subject-1"
MessageProducer producer = session.createProducer(topic-1);
producer.send(session.createTextMessage("my message"));

通过这种方式,您还可以设置消费者来接收发送到“my-topic”的所有消息,并使用“my-topic.*”作为其路由键。
请参阅 Qpid 文档“Programming-In-Apache-Qpid”中的更多详细信息

You should be able to achieve what you want by using AMQP Topic. Set the routingKey to something such as "my-topic". Set up your Consumers to different subjects as designed, such as "subject-1", "subject-2", ...

For the producers each of them can send messages with different subjects, such as "my-topic.subject-1", "my-topic.subject-2", ... use those as the routingKey for the producers.

Sample code look like this:

//set up message consumer for "subject-1"
AMQTopic topic-1 = new AMQTopic(new AMQShortString("amq.topic"), new AMQShortString("my-topic.subject-1), false, null, true);
MessageConsumer consumer = session.createConsumer(topic-1);
Message message = consumer.receive();
...

//set up message producer for "subject-1"
MessageProducer producer = session.createProducer(topic-1);
producer.send(session.createTextMessage("my message"));

in this way you can also set up a consumer to receive all the message that are sent to "my-topic" as well using "my-topic.*" as its routing key.
See more details in Qpid documentation, "Programming-In-Apache-Qpid"

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