Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i implement Headers Exchange in RabbitMQ using Java?

Tags:

java

rabbitmq

i am a newbie trying to implement Headers exchange in java client . im aware that This is what the "x-match" binding argument is for. When the "x-match" argument is set to "any", just one matching header value is sufficient. Alternatively, setting "x-match" to "all" mandates that all the values must match. but can anyone provide me a skeleton code for better understanding.

like image 690
Bhawani Avatar asked Oct 08 '13 06:10

Bhawani


1 Answers

For using a headers exchange you just need to declare your exchange as headers type:

channel.exchangeDeclare("myExchange", "headers", true);

Then you need to declare a queue that will be the final destination of the messages before a consumer consumes them:

channel.queueDeclare("myQueue", true, false, false, null);

Now we need to bind the exchange to queue declaring a binding. In this declaration is where you set which headers you want for routing messages from your exchange to your queue. An example could be:

Map<String, Object> bindingArgs = new HashMap<String, Object>();
bindingArgs.put("x-match", "any"); //any or all
bindingArgs.put("headerName#1", "headerValue#1");
bindingArgs.put("headerName#2", "headerValue#2");

...
channel.queueBind("myQueue", "myExchange", "", bindingArgs);
...

This will create the binding using headerName#1 and headerName#2. I hope this helps!

like image 185
hveiga Avatar answered Oct 30 '22 07:10

hveiga