9

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.

1 Answer 1

26

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Good explanation of exchange/queue relationship. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.