How to implement Headers Exchange in RabbitMQ using Java?

I am a newbie trying to implement header exchange in java client. im knows this is what for the x-match binding argument. When the argument "x-match" is set to "any", one matching header value is sufficient. Alternatively, setting "x-match" to "all" means that all values ​​must match. but can anyone provide me with skeletal code for a better understanding.

+7
java rabbitmq
source share
2 answers

To use header exchanges, you just need to declare your exchange as a header type:

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

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

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

Now we need to bind the exchange to the queue, declaring the binding. This ad indicates which headers you want to route messages from your exchange to the queue. An example would 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 a binding using headerName # 1 and headerName # 2. Hope this helps!

+20
source share

First declare an exchange with the type of headers: -

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

Then declare the queue: -

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

Now define the header and bind it to the queue: -

 Map<String,Object> map = new HashMap<String,Object>(); map.put("x-match","any"); map.put("First","A"); map.put("Fourth","D"); channel.queueBind("Queue", "Exchange_Header", ROUTING_KEY ,map); 

Check this out: http://codedestine.com/rabbitmq-headers-exchange/

0
source share

All Articles