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。