消息头指的就是channel.basicPublish()的第三个参数。
功能参考http协议,消息头中,包含了非常多有用的信息,例如:数据格式、编码格式、用户信息等等。
以ContentType为例,用于区分当前的数据类型,比如:Xml、JSON。
RabbitMQ也有这样的设计,需要用到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 的 header,可能会感觉 headers 中包含 contentType,
而 BasicProperties 里面也有一个 contentType ,往 header 中设置 contentType 会不会产生啥特殊的效果?
实际上,二者没啥关联,headers就是一个单纯的Map。