Search code examples
bindingrabbitmqamqpmessagebrokerrabbitmq-exchange

RabbitMQ exchange bind replicating messages published to one exchange across two other exchanges


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");

Solution

  • 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"));````