• 欢迎访问SimonHu的博客,推荐使用360浏览器和Chrome浏览器访问本网站

RabbitMQ管理页面发送消息

java SimonHu 4个月前 (10-27) 150次浏览 0个评论 扫描二维码
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
    RabbitMQ管理页面发送消息

`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 + '\'' +
                '}';
    }
}
 @Override
    public void sendQueneMsg(String queueName, ActiveOrder activeOrder) {
        rabbitTemplate.setQueue(queueName);
        rabbitTemplate.setMandatory(true);
        String json = JsonUtils.toJsonNf(activeOrder);
        //指定CONTENT_TYPE_JSON
        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);
    }

RabbitMQ管理页面发送消息
RabbitMQ控制台发送Json消息


版权所有丨如未注明 , 均为原创丨
转载请注明原文链接:RabbitMQ管理页面发送消息
喜欢 (0)
[]
分享 (0)
关于作者:
Do you think that guy looks like a dog???
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址