RabbitMQ消费消息坑:failed to convert serialized Message content

RabbitMQ消费消息坑:failed to convert serialized Message content文章目录一、问题描述二、解决方案方案一:共同使用一个对象方案二:消息JSON序列化(推荐)2.1.生产者发送消息JSON序列化2.2.消费者接收消息JSON反序列化三、测试一、问题描述2022-05-0314:01:40.630WARN16876—[ntContainer#0-2]s.a.r.l.ConditionalRejectingErrorHandler:ExecutionofRabbitmessagelistenerfailed.org.springfram

大家好,又见面了,我是你们的朋友全栈君。


一、问题描述

2022-05-03 14:01:40.630  WARN 16876 --- [ntContainer#0-2] s.a.r.l.ConditionalRejectingErrorHandler : Execution of Rabbit message listener failed.

org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Failed to convert message
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:155) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1665) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1584) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1572) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1563) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1507) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:967) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:914) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1600(SimpleMessageListenerContainer.java:83) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1291) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1197) [spring-rabbit-2.4.2.jar:2.4.2]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_301]
Caused by: org.springframework.amqp.support.converter.MessageConversionException: failed to convert serialized Message content
	at org.springframework.amqp.support.converter.SimpleMessageConverter.fromMessage(SimpleMessageConverter.java:117) ~[spring-amqp-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.extractMessage(AbstractAdaptableMessageListener.java:342) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter$MessagingMessageConverterAdapter.extractPayload(MessagingMessageListenerAdapter.java:365) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.support.converter.MessagingMessageConverter.fromMessage(MessagingMessageConverter.java:132) ~[spring-amqp-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.toMessagingMessage(MessagingMessageListenerAdapter.java:242) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:145) ~[spring-rabbit-2.4.2.jar:2.4.2]
	... 11 common frames omitted
Caused by: java.lang.IllegalStateException: Could not deserialize object type
	at org.springframework.amqp.utils.SerializationUtils.deserialize(SerializationUtils.java:97) ~[spring-amqp-2.4.2.jar:2.4.2]
	at org.springframework.amqp.support.converter.SimpleMessageConverter.fromMessage(SimpleMessageConverter.java:113) ~[spring-amqp-2.4.2.jar:2.4.2]
	... 16 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.example.cloud.producer.rabbitmq.entity.RabbitOrder
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_301]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_301]
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) ~[na:1.8.0_301]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_301]
	at java.lang.Class.forName0(Native Method) ~[na:1.8.0_301]
	at java.lang.Class.forName(Class.java:348) ~[na:1.8.0_301]
	at org.springframework.util.ClassUtils.forName(ClassUtils.java:284) ~[spring-core-5.3.16.jar:5.3.16]
	at org.springframework.core.ConfigurableObjectInputStream.resolveClass(ConfigurableObjectInputStream.java:76) ~[spring-core-5.3.16.jar:5.3.16]
	at org.springframework.amqp.support.converter.SimpleMessageConverter$1.resolveClass(SimpleMessageConverter.java:183) ~[spring-amqp-2.4.2.jar:2.4.2]
	at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1984) ~[na:1.8.0_301]
	at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1848) ~[na:1.8.0_301]
	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2158) ~[na:1.8.0_301]
	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1665) ~[na:1.8.0_301]
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:501) ~[na:1.8.0_301]
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:459) ~[na:1.8.0_301]
	at org.springframework.amqp.utils.SerializationUtils.deserialize(SerializationUtils.java:91) ~[spring-amqp-2.4.2.jar:2.4.2]
	... 17 common frames omitted

2022-05-03 14:01:40.630  WARN 16876 --- [ntContainer#0-2] ingErrorHandler$DefaultExceptionStrategy : Fatal message conversion error; message rejected; it will be dropped or routed to a dead letter exchange, if so configured: (Body:'[serialized object]' MessageProperties [headers={spring_listener_return_correlation=b2329a07-f83e-4999-b7b4-5d1c7b65c767, spring_returned_message_correlation=1651557699966$3b0f387d-b17f-42df-b060-06ffa0a71910}, contentType=application/x-java-serialized-object, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=order-exchange, receivedRoutingKey=order.user, deliveryTag=1, consumerTag=amq.ctag-sxIMscspKJ98oEOaFyIyPQ, consumerQueue=order-queue])
2022-05-03 14:01:40.630 ERROR 16876 --- [ntContainer#0-2] o.s.a.r.l.SimpleMessageListenerContainer : Execution of Rabbit message listener failed, and the error handler threw an exception

org.springframework.amqp.AmqpRejectAndDontRequeueException: Error Handler converted exception to fatal
	at org.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler.handleError(ConditionalRejectingErrorHandler.java:146) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeErrorHandler(AbstractMessageListenerContainer.java:1469) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.handleListenerException(AbstractMessageListenerContainer.java:1753) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1528) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:967) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:914) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1600(SimpleMessageListenerContainer.java:83) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1291) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1197) [spring-rabbit-2.4.2.jar:2.4.2]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_301]
Caused by: org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Failed to convert message
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:155) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1665) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1584) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1572) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1563) [spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1507) [spring-rabbit-2.4.2.jar:2.4.2]
	... 6 common frames omitted
Caused by: org.springframework.amqp.support.converter.MessageConversionException: failed to convert serialized Message content
	at org.springframework.amqp.support.converter.SimpleMessageConverter.fromMessage(SimpleMessageConverter.java:117) ~[spring-amqp-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.extractMessage(AbstractAdaptableMessageListener.java:342) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter$MessagingMessageConverterAdapter.extractPayload(MessagingMessageListenerAdapter.java:365) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.support.converter.MessagingMessageConverter.fromMessage(MessagingMessageConverter.java:132) ~[spring-amqp-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.toMessagingMessage(MessagingMessageListenerAdapter.java:242) ~[spring-rabbit-2.4.2.jar:2.4.2]
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:145) ~[spring-rabbit-2.4.2.jar:2.4.2]
	... 11 common frames omitted
Caused by: java.lang.IllegalStateException: Could not deserialize object type
	at org.springframework.amqp.utils.SerializationUtils.deserialize(SerializationUtils.java:97) ~[spring-amqp-2.4.2.jar:2.4.2]
	at org.springframework.amqp.support.converter.SimpleMessageConverter.fromMessage(SimpleMessageConverter.java:113) ~[spring-amqp-2.4.2.jar:2.4.2]
	... 16 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.example.cloud.producer.rabbitmq.entity.RabbitOrder
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_301]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_301]
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) ~[na:1.8.0_301]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_301]
	at java.lang.Class.forName0(Native Method) ~[na:1.8.0_301]
	at java.lang.Class.forName(Class.java:348) ~[na:1.8.0_301]
	at org.springframework.util.ClassUtils.forName(ClassUtils.java:284) ~[spring-core-5.3.16.jar:5.3.16]
	at org.springframework.core.ConfigurableObjectInputStream.resolveClass(ConfigurableObjectInputStream.java:76) ~[spring-core-5.3.16.jar:5.3.16]
	at org.springframework.amqp.support.converter.SimpleMessageConverter$1.resolveClass(SimpleMessageConverter.java:183) ~[spring-amqp-2.4.2.jar:2.4.2]
	at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1984) ~[na:1.8.0_301]
	at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1848) ~[na:1.8.0_301]
	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2158) ~[na:1.8.0_301]
	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1665) ~[na:1.8.0_301]
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:501) ~[na:1.8.0_301]
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:459) ~[na:1.8.0_301]
	at org.springframework.amqp.utils.SerializationUtils.deserialize(SerializationUtils.java:91) ~[spring-amqp-2.4.2.jar:2.4.2]
	... 17 common frames omitted

从异常信息中可以看到是消费者对消息反序列化的时候失败了。虽然两个项目中的发送和接收对象是完全一样的,但在进行反序列化的时候还是失败了


下图可以看到RabbitMQ默认消息编码是base64,消费者有没做任何处理,所以会导致消费者接收到的消息会无法识别

img-blog.csdnimg.cn/979d190afb9f442981f70d074d26e3e9.png)

二、解决方案

方案一:共同使用一个对象

描述:我目前生产者和消费者都有一个对象,内容是一模一样的包括序列化,可就是消费者接收时报序列化错误
解决方案:把这个对象放到公共模块,然后生产者和消费者服务引入这个公共模块,生产者和消费者发送和消费时共用一个对象。这样完全保证了两个项目中JavaBean是一致的,所以能解决反序列失败的问题
缺点:局限性太小,这种模式生产者和消费者只能在同一个微服务下才能使用

方案二:消息JSON序列化(推荐)

2.1.生产者发送消息JSON序列化

生产者添加配置:

/** * RabbitMQ配置 * * @author Tang * @version 1.0 * @date 2022/05/02 23:23:27 */
@Configuration
public class RabbitMQConfig implements InitializingBean { 
   

    /** * 自动注入RabbitTemplate模板 */
    @Resource
    private RabbitTemplate rabbitTemplate;

    /** * 发送消息JSON序列化 */
    @Override
    public void afterPropertiesSet() { 
   
        //使用JSON序列化
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
    }
}

2.2.消费者接收消息JSON反序列化

消费者添加配置:

/** * RabbitMQ配置 * * @author Tang * @version 1.0 * @date 2022/05/02 23:23:27 */
@Configuration
public class RabbitMQConfig { 
   

    @Bean
    public MessageConverter jsonMessageConverter(ObjectMapper objectMapper) { 
   
        return new Jackson2JsonMessageConverter(objectMapper);
    }
}

三、测试

生产者推送消息:
在这里插入图片描述

rabbitmq控制台查看消息类型:
在这里插入图片描述

消费者成功消费:
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/148725.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Awvs详细使用教程「建议收藏」

    Awvs详细使用教程「建议收藏」Awvs的是一款非常好用的web漏洞扫描工具,他的扫描速度比较快,可以自己选择扫描速度,比较灵活。Awvs分为老的版本和新版本,下面我介绍的是新版本的功能和用法。功能介绍如下:仪表盘(监视器)功能,添加目标功能,漏洞排序功能,扫描功能,发现功能,用户功能,扫描配置功能,网络扫描功能,追踪器功能,防火墙设置,邮件设置,引擎,时间排除功能,代理功能,常规设置主要使用的功能是前面的6个,后面的根据个人的需要进行配置详细介绍如下:Dashboard功能:翻译意思仪表盘(监视器),可以对扫描对扫描

    2022年10月22日
  • 武道释义 · 零散

    武道释义 · 零散

  • declare-styleable的使用

    declare-styleable的使用declare-styleable的使用-carlosk-博客园declare-styleable是给自定义控件添加自定义属性用的1.首先,先写attrs.xml…

  • openssl 创建pfx证书

    openssl 创建pfx证书在存储证书时,为了不被泄露,一般证书的密钥不直接以文件形式(.key文件)存储,我们可以将证书文件和密钥文件合并生成pfx个人正式格式。opensslpkcs12-export-inmy.cer-inkeymy.key-outmy.pfx

  • 手机上好用的java编译器_手机上好用的java编译器

    手机上好用的java编译器_手机上好用的java编译器手机上好用的java编译器安卓版是一款专为java开发人员服务的编辑客户端应用,利用手机上好用的java编译器手机安卓版实时进行相关编辑还能进行简单的编译,运行单个小程序等,提供您的效率。功能介绍手机上好用的java编译器安卓版是一款将openjdk中关于编译java工程的代码移植到了安卓平台。手机上好用的java编译器手机安卓版支持添加jar格式的lib文件,并且将编译后的程序dex化,以便在安…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号