栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Fatal message conversion error;message rejected;it will be dropped or routed to a dead letter exchan

Fatal message conversion error;message rejected;it will be dropped or routed to a dead letter exchan

在使用rabbitmq的时候出现消息反序列化失败,如下异常:
Fatal message conversion error; message rejected; it will be dropped or routed to a dead letter exchange, if so configured

经过定位分析,原因是在MQ消息的生产端,设置了序列化转换Jackson2JsonMessageConverter,而默认的序列化类为SimpleMessageConverter。且在消费端没有设置反序列化转换。

解决方法:
因为消息是json串,使用String接收参数,再使用json工具类转化为对象;

import com.alibaba.nacos.client.utils.JSONUtils;
import com.atguigu.rabbit.common.constant.MqConst;
import com.atguigu.yygh.sms.service.SMSService;
import com.atguigu.yygh.vo.sms.SmsVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SmsReceiver {

    @Autowired
    private SMSService smsService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_SMS, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_SMS),
            key = {MqConst.ROUTING_SMS}
    ))
    public void send(
            String json,//接收传输过来的内容
            Message message, Channel channel) {
        SmsVo smsVo = null;
        try {
            //把json串转化为对应的对象
            //JSonUtils 包名(com.alibaba.nacos.client.utils.JSONUtils)
            smsVo = (SmsVo) JSONUtils.deserializeObject(json, SmsVo.class);
            System.out.println(smsVo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/460238.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号