如何使用Java在RabbitMQ中实现Headers Exchange? [英] How do i implement Headers Exchange in RabbitMQ using Java?

查看:504
本文介绍了如何使用Java在RabbitMQ中实现Headers Exchange?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

要使用标题交换,您只需要将您的交易所声明为标题类型:

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);
...

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

This will create the binding using headerName#1 and headerName#2. I hope this helps!

这篇关于如何使用Java在RabbitMQ中实现Headers Exchange?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆