RabbitMQ——消息头


channel.basicPublish()的第三个参数,就是请求头了,推送消息的时候,我们可以设置一个请求头。

功能与 http 协议的 header 类似,消息头中,可以添加很多有用的信息,例如:数据格式、编码格式、用户信息等等。
以 ContentType 为例,用于区分当前的数据类型,比如:application/json。

不过,RabbitMQ 的 header 不叫 header,而是 MessageProperties 对象,关键代码如下:

AMQP.BasicProperties properties = MessageProperties.BASIC.builder().build();

可选参数类型如下:

(不需要都有,按照自己的需求设置即可)

private String contentType;
private String contentEncoding;
private Map<String,Object> headers;
private Integer deliveryMode;
private Integer priority;
private String correlationId;
private String replyTo;
private String expiration;
private String messageId;
private Date timestamp;
private String type;
private String userId;
private String appId;
private String clusterId;

其中值得注意的是 headers,习惯了 http 协议,会往 headers 设置 contentType,
而 BasicProperties 里面,既有headers,又有 contentType ,
如果headers里面设置contentType ,会不会产生啥特殊的效果?
实际上,二者没啥关联,headers就是一个单纯的Map。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM