rabbitmq管理页面发送消息

Delivery mode: 是否持久化,1 - Non-persistent,2 - Persistent
Headers:Headers can have any name. Only long string headers can be set here.

__TypeId__: 消息体实体类类型 当需要在消费端对应实体类接受时 
需要设置该属性 值为对应实体类在项目下的全路径 ${包名.类名}
不加这个也可以
  1. 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

  2. Payload: 消息内容(必须是以字符串形式传入)

    demo:content_type application/json
    BM7bFS.png

`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 + '\'' +
                '}';
    }
}

BQW081.png
生产者发送消息要指定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);
    }```
posted @ 2020-10-27 12:55  748573200000  阅读(4791)  评论(0编辑  收藏  举报