版权声明:本文为博主原创文章,如果转载请给出原文链接:http://doofuu.com/article/4156157.html

Headers 类型的Exchanges是不处理路由键的,而是根据发送的消息内容中的headers属性进行匹配。在绑定Queue与Exchange时指定一组键值对;当消息发送到RabbitMQ时会取到该消息的headers与Exchange绑定时指定的键值对进行匹配;如果完全匹配则消息会路由到该队列,否则不会路由到该队列。headers属性是一个键值对,可以是Hashtable,键值对的值可以是任何类型。而fanout,direct,topic 的路由键都需要要字符串形式的。

匹配规则x-match有下列两种类型:x-match = all   :表示所有的键值对都匹配才能接受到消息

x-match = any :表示只要有键值对匹配就能接受到消息

不过headers比较少用到,下面是headers的官方说明文档:

A headers exchange is designed to for routing on multiple attributes that are more easily expressed as message headers than a routing key. Headers exchanges ignore the routing key attribute. Instead, the attributes used for routing are taken from the headers attribute. A message is considered matching if the value of the header equals the value specified upon binding.

It is possible to bind a queue to a headers exchange using more than one header for matching. In this case, the broker needs one more piece of information from the application developer, namely, should it consider messages with any of the headers matching, or all of them? 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.

Headers exchanges can be looked upon as "direct exchanges on steroids". Because they route based on header values, they can be used as direct exchanges where the routing key does not have to be a string; it could be an integer or a hash (dictionary) for example.

下面贴出headers的事例代码!!

Consumer:package headers;

import java.io.IOException;

import java.util.Hashtable;

import java.util.Map;

import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP;

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.ConnectionFactory;

import com.rabbitmq.client.Consumer;

import com.rabbitmq.client.DefaultConsumer;

import com.rabbitmq.client.Envelope;

public class HeadersConsumer {

private static final String EXCHANGE_NAME = "exchange_headers";

private static final String QUEUE_NAME = "headers_test_queue";

public static void main(String[] argv) throws IOException, TimeoutException  {

new ExchangeHeaders();

}

static class ExchangeHeaders{

public  ExchangeHeaders() throws IOException, TimeoutException {

ConnectionFactory factory = new ConnectionFactory();

//rabbitmq监听IP

factory.setHost("192.168.249.128");

//rabbitmq监听默认端口

factory.setPort(5672);

//设置访问的用户

factory.setUsername("test");

factory.setPassword("test");

Connection connection = factory.newConnection();

Channel channel = connection.createChannel();

//声明路由名字和类型

channel.exchangeDeclare(EXCHANGE_NAME, "headers", false, true, null);

//创建队列

channel.queueDeclare(QUEUE_NAME, false, false, true, null);

//设置消息头键值对信息

Map headers = new Hashtable();

//这里x-match有两种类型

//all:表示所有的键值对都匹配才能接受到消息

//any:表示只要有键值对匹配就能接受到消息

headers.put("x-match", "any");

headers.put("name", "jack");

headers.put("age" , 31);

//把队列绑定到路由上并指定headers

channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "", headers);

System.out.println(" Waiting for msg....");

Consumer consumer = new DefaultConsumer(channel) {

@Override

public void handleDelivery(String consumerTag, Envelope envelope,

AMQP.BasicProperties properties, byte[] body) throws IOException {

String message = new String(body, "UTF-8");

System.out.println("Received msg is '" + message + "'");

}

};

channel.basicConsume(QUEUE_NAME, true, consumer);

}

}

}

Producer:package headers;

import java.io.IOException;

import java.util.Hashtable;

import java.util.Map;

import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP.BasicProperties.Builder;

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.ConnectionFactory;

public class HeadersProducer {

private static final String EXCHANGE_NAME = "exchange_headers";

public static void main(String[] argv) throws Exception{

new ExchangeHeaders("exchanges type headers test msg~");

}

static class ExchangeHeaders{

public ExchangeHeaders(String message) throws IOException, TimeoutException{

ConnectionFactory factory = new ConnectionFactory();

//rabbitmq监听IP

factory.setHost("192.168.249.128");

//rabbitmq监听默认端口

factory.setPort(5672);

//设置访问的用户

factory.setUsername("test");

factory.setPassword("test");

Connection connection = factory.newConnection();

Channel channel = connection.createChannel();

//声明路由名字和类型

channel.exchangeDeclare(EXCHANGE_NAME, "headers", false, true, null);

//设置消息头键值对信息

Map headers = new Hashtable();

headers.put("name", "jack");

headers.put("age", 30);

Builder builder = new Builder();

builder.headers(headers);

channel.basicPublish(EXCHANGE_NAME, "", builder.build(), message.getBytes());

System.out.println("Sent msg is '" + message + "'");

channel.close();

connection.close();

}

}

}

运行结果:

注意如果将Consumer中的headers.put("x-match", "any");改成headers.put("x-match", "all"); 则不会接受到消息!!

祝生活愉快!

「创作不易,你的支持是本站持续更新最大的动力!」

1

谢谢你请我喝奶茶*^_^*

支付宝

微信

headers java_RabbitMQ四种Exchange类型之Headers(Java)相关推荐

  1. RabbitMQ 四种Exchange

    AMQP协议中的核心思想就是生产者和消费者隔离,生产者从不直接将消息发送给队列.生产者通常不知道是否一个消息会被发送到队列中,只是将消息发送到一个交换机.先由Exchange来接收,然后Exchang ...

  2. python中内置的四种数值类型为_浅谈python语言四种数值类型

    Python语言支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数),本文章向码农介绍python 四种数值类型,需要的朋友可以参考一下.希望对 ...

  3. ospf的四种网络类型

    原文链接:https://blog.csdn.net/weixin_37945631/article/details/79498334 https://blog.csdn.net/qq_3921960 ...

  4. 卷积神经网络四种卷积类型

    卷积神经网络四种卷积类型 https://www.toutiao.com/a6631333810287936013/ 一般卷积 首先,我们需要就定义卷积层的一些参数达成一致. 卷积核大小(Kernel ...

  5. VMware vSphere四种迁移类型的区别与适应场景

    最近一直刚开始接触VMware vSphere这款虚拟化软件,每天的过程都是上午学新知识不理解,痛苦:下午实验各种出错,折磨:晚上回顾一天所学,五味陈杂,不过相比上午,下午已经好很多了.然后第二天依旧 ...

  6. python中long类型_浅谈python 四种数值类型(int,long,float,complex)

    Python支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数),本文章向码农介绍python 四种数值类型,需要的朋友可以参考一下. 数字数据 ...

  7. python数字类型floatcomplexint_浅谈python 四种数值类型(int,long,float,complex)

    Python支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数),本文章向码农介绍python 四种数值类型,需要的朋友可以参考一下. 数字数据 ...

  8. 四种依恋类型_依恋关系的研究

    说到依恋关系,大家都会想到安全型.焦虑型.回避型,以及这些类型会影响到成年后的人际关系.本文具体罗列一下心理学家研究依恋关系的都做出了怎样的努力. 第一:哈罗做的恒河猴验. 实验情境:把小猴放在一个笼 ...

  9. P2P穿透四种NAT类型

    P2P穿透&四种NAT类型 以下对四种NAT类型分别予以说明: 1.全锥形NAT IP.端口都不受限.只要客户端由内到外打通一个洞之后(NatIP:NatPort -> A:P1),其他 ...

最新文章

  1. python的assert作用
  2. php单文件短链接,php实现的短网址算法分享
  3. 仅输入单张图片,就能“看”出物体材质!这篇图形学论文已被SIGGRAPH 2021收录...
  4. vue 实现瀑布流布局的 组件/插件总汇:vue-waterfall、vue-waterfall-easy、vue-virtual-collection、vue-grid-layout
  5. 韩流来袭哈狗帮_韩流可以教给我们什么设计知识
  6. DWA论文解析(CurvatureVelovityMethod)(3)
  7. 读取kaf卡数据_墨菊居然能秒开机秒读数据!Lexar雷克沙1667x UHS-II存储卡体验
  8. C# FileStream
  9. java servlet init方法_Servlet详解之两个init方法的作用
  10. mysql版本号字段比较大小_MySQL查询 – 比较版本号
  11. nexus3的目录介绍
  12. css内联样式---动态绑定背景图片
  13. 根据身份证判断年龄排序
  14. 比较常见的几种代理ip类型
  15. mongodb添加仲裁节点无响应
  16. Kali开机后的初始化配置
  17. 关于computer vision的会议及vision guys-机器学习与视觉大牛族谱深度挖掘
  18. NOIP 为什么要学 数学
  19. win10杀毒防护已经关闭了还是删除文件解决方法
  20. 数据库学习:数据模型

热门文章

  1. AMD EPYC架构
  2. NX二次开发-UFUN创建圆柱UF_MODL_create_cyl1
  3. Transform.LookAt解析
  4. html方框打勾字段,word文档怎么输入带方框的对勾
  5. Gameplay常用编写方法(持续更新)
  6. 大数据BI可视化应用介绍
  7. 西门子SMART 200 modbus rtu通讯宇电温控器例程
  8. 一键加速去不掉加锁的_vivo手机一键加速的时候,怎样可以不把当前的应用清除?...
  9. 新显卡出世,谈谈与深度学习有关的显卡架构和相关技术
  10. js获取某一天是星期几