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

How do i implement Headers Exchange in RabbitMQ using Java?(如何使用 Java 在 RabbitMQ 中实现 Headers Exchange?)
本文介绍了如何使用 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.

推荐答案

对于使用 headers 交换,您只需将您的交换声明为 headers 类型:

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)