Delivery mode: 是否持久化,1 - Non-persistent,2 - Persistent
Headers:Headers can have any name. Only long string headers can be set here.
__TypeId__: 消息體實體類類型 當需要在消費端對應實體類接受時
需要設置該屬性 值為對應實體類在項目下的全路徑 ${包名.類名}
不加這個也可以
-
Properties: You can set other message properties here
(delivery mode and headers are pulled out as the most common cases). Invalid properties will be ignored. Valid properties are:
content_type : 消息內容的類型 text/json application/json
content_encoding: 消息內容的編碼格式 utf-8
priority: 消息的優先級
correlation_id:關聯id
reply_to:用於指定回復的隊列的名稱
expiration: 消息的失效時間
message_id: 消息id
timestamp:消息的時間戳
type:類型
user_id:用戶id
app_id:應用程序id
cluster_id: 集群id -
Payload: 消息內容(必須是以字符串形式傳入)
`MQ消費端轉換報錯:
主要錯誤信息:
Caused by: org.springframework.messaging.converter.MessageConversionException: Cannot convert from [[B] to [com.***.***.***.***] for GenericMessage
[payload=byte[12], headers={amqp_receivedDeliveryMode=NON_PERSISTENT, amqp_receivedRoutingKey=ENT_***_NOTICE, amqp_deliveryTag=4, amqp_consumerQueue=ENT_***_NOTICE,
amqp_redelivered=false, id=cf822382-2a6e-8030-add4-b59bfa561e34, amqp_consumerTag=amq.ctag-2BJwxJlkUYKxiAkxPc67kA, timestamp=1559721028305}]
解決方法替換點mq默認的序列化器(Application中加入)。
/
**
* {@link org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration}
* 會自動識別
* @param objectMapper json序列化實現類
* @return mq 消息序列化工具
*/
@Bean
public MessageConverter jsonMessageConverter(ObjectMapper objectMapper) {
return new Jackson2JsonMessageConverter(objectMapper);
}
//或者
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}`
如果出現異常
` no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?`
被序列化的實體類要有顯式使用默認無參構造方法;
import lombok.Data;
import java.io.Serializable;
/**
* @Author: SimonHu
* @Date: 2020/9/27 13:39
* @Description:
*/
@Data
public class ActiveOrder implements Serializable {
/***RabbitMQ 管理頁面手動推送消息**/
/***
* Publish message
* Properties: content_type = application/json
* Payload {"orderNo":"20200261400027361027","status":0}
*
* JsonUtils.toJsonNf(new ActiveOrder("20200261400027321021"));
*/
private static final long serialVersionUID = -5921741921463757174L;
private String orderNo;
private int status;
private String key;
/**
* MessageConverter
* json轉換需要默認無參構造方法
**/
public ActiveOrder() {
}
public ActiveOrder(String orderNo) {
this.orderNo = orderNo;
}
public ActiveOrder(String orderNo, int status, String key) {
this.orderNo = orderNo;
this.status = status;
this.key = key;
}
@Override
public String toString() {
return "ActiveOrder{" +
"orderNo='" + orderNo + '\'' +
", status=" + status +
", key='" + key + '\'' +
'}';
}
}
生產者發送消息要指定content_type
RabbitMQ控制台發送Json消息
public void sendQueneMsg(String queueName, ActiveOrder activeOrder) {
rabbitTemplate.setQueue(queueName);
// 生產者發送消息的時候需要設置消息id
rabbitTemplate.setMandatory(true);
String json = JsonUtils.toJsonNf(activeOrder);
Message message = MessageBuilder.withBody(json.getBytes())
.setContentType(MessageProperties.CONTENT_TYPE_JSON).setContentEncoding("utf-8")
.setMessageId(UUID.randomUUID() + "").build();
log.info(queueName+"=======activeOrder:=========="+json);
log.info(queueName+"========message:========="+message.toString());
rabbitTemplate.convertAndSend(queueName, message);
}```