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。

 

posted on 2022-04-01 11:15  疯狂的妞妞  阅读(788)  评论(0编辑  收藏  举报

导航