0

I have 3 different exchanges in my rabbitMQ i am trying to route all the messages sent to mainex across the other two exchanges dmyex and monex i tried to use the channel.exchngeBind method to bind the exchanges.

I am still not able to see the messages published to mainex going to dmyex and monex.

Is this possible in RabbitMq?

Is there any mistake in what i am doing here?

ch.exchangeDeclare("mainex", DIRECT_EXCHANGE_TYPE, true, false, null);
ch.exchangeDeclare("dmyex", CONSISTENT_HASH_EXCHANGE_TYPE, true, false, null);
ch.exchangeDeclare("monex", CONSISTENT_HASH_EXCHANGE_TYPE, true, false, null);
ch.exchangeBind("dmyex","mainex","abcd_KEY");
ch.exchangeBind("monex","mainex","abcd_KEY");

2 Answers2

0

I think that the exchange mainex will need to be a Topic exchange in order for wildcard routing to work, as Direct exchanges do their routing based on exact matching of the routing key. Per this CloudAMQP blog post on exchange types and routing:

A direct exchange delivers messages to queues based on a message routing key. The routing key is a message attribute added to the message header by the producer. Think of the routing key as an "address" that the exchange is using to decide how to route the message. A message goes to the queue(s) with the binding key that exactly matches the routing key of the message.

(This is also touched on in this answer to a question about wildcard routing keys on SO)

That should be enough to get your routing working. Perhaps even more simply, if you are going to match any routing key for all of your exchanges that are bound to mainex, you could just make mainex a Fanout exchange.

0

Just figured out the below model is working.

   ch.exchangeDeclare("mainex", DIRECT_EXCHANGE_TYPE, true, false, null);
   ch.exchangeDeclare("dmyex", CONSISTENT_HASH_EXCHANGE_TYPE, true, false, null);
   ch.exchangeDeclare("mongoex", CONSISTENT_HASH_EXCHANGE_TYPE, true, false, null);
   
   
   ch.exchangeBind("dmyex","mainex", "abcd_KEY");
   ch.exchangeBind("monex","mainex", "abcd_KEY");
   
   
   for (String q : Arrays.asList("sharding: dmyex - rabbit@node1 - 0", "sharding: dmyex - rabbit@node2 - 0","sharding: dmyex - rabbit@node3 - 0","sharding: dmyex - rabbit@node4 - 0","sharding: dmyex - rabbit@node5 - 0","sharding: dmyex - rabbit@node6 - 0")) {
       ch.queueBind(q, "dmyex", "1");
   }
   
   
   for (String q : Arrays.asList("sharding: mongoex - rabbit@node1 - 0", "sharding: mongoex - rabbit@node2 - 0","sharding: mongoex - rabbit@node3 - 0","sharding: mongoex - rabbit@node4- 0","sharding: mongoex - rabbit@node5 - 0","sharding: mongoex - rabbit@node6 - 0")) {
       ch.queueBind(q, "mongoex", "1");
   }
   
   
     AMQP.BasicProperties.Builder bldr = new AMQP.BasicProperties.Builder();
     for (int i = 0; i < 10; i++) {
     ch.basicPublish("mainex", "abcd_KEY", bldr.build(), "TestMessage".getBytes("UTF-8"));````