如何使用Java在RabbitMQ中实现Headers Exchange?

我是一个尝试在java客户端实现Headers交换的新手。 我知道这就是“x-match”绑定参数的用途。 当“x-match”参数设置为“any”时,只需一个匹配的标头值就足够了。 或者,将“x-match”设置为“all”,强制所有值必须匹配。 但任何人都可以为我提供一个骨架代码,以便更好地理

要使用标头交换,您只需将交换声明为标头类型:

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

然后,您需要在消费者使用它们之前声明一个队列,该队列将成为消息的最终目标:

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

现在我们需要将交换绑定到队列来声明绑定。 在此声明中,您可以设置将邮件从交换机路由到队列所需的标头。 一个例子可能是:

 Map bindingArgs = new HashMap(); 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); ... 

这将使用headerName#1和headerName#2创建绑定。 我希望这有帮助!

首先使用标题类型声明交换: –

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

然后声明队列: –

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

现在定义标头并用队列绑定它: –

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

检查一下: – http://codedestine.com/rabbitmq-headers-exchange/