Bhawani
Bhawani

Reputation: 93

How do i implement Headers Exchange in RabbitMQ using Java?

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.

Upvotes: 9

Views: 10432

Answers (1)

hveiga
hveiga

Reputation: 6915

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!

Upvotes: 26

Related Questions