0

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

MoveFast
  • 3,011
  • 2
  • 27
  • 53

4 Answers4

2

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.

1

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

ncdc
  • 341
  • 3
  • 7
0

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

0

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"