我是一个新手,试图在Java客户端中实现 header 交换。我知道这就是“x-match”绑定(bind)参数的作用。当“x-match”参数设置为“any”时,仅一个匹配的 header 值就足够了。或者,将“x-match”设置为“all”要求所有值必须匹配。
但任何人都可以提供基本代码以更好地理解我。

最佳答案

对于使用 header 交换,您只需要声明交换为 header 类型:

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

然后,您需要声明一个队列,该队列将成为使用者使用消息之前的消息的最终目标:
channel.queueDeclare("myQueue", true, false, false, null);

现在,我们需要将交换绑定(bind)到声明绑定(bind)的队列中。在此声明中,您可以在其中设置要将邮件从交换机路由到队列的头。例如:
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);
...

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

07-24 20:17