当前位置:   article > 正文

Rabbitmq发送邮件遇到格式序列化问题_org.springframework.amqp.rabbit.support.listenerex

org.springframework.amqp.rabbit.support.listenerexecutionfailedexception: fa

目录

问题描述

解决方法


问题描述

最近跟着B站大佬青空敲代码,在使用rabbitmq发送邮件验证码时遇到了问题,控制台疯狂报错。

  1. org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Failed to convert message
  2. at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:158) ~[spring-rabbit-3.0.10.jar:3.0.10]
  3. at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1663) ~[spring-rabbit-3.0.10.jar:3.0.10]
  4. at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1582) ~[spring-rabbit-3.0.10.jar:3.0.10]
  5. at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1570) ~[spring-rabbit-3.0.10.jar:3.0.10]
  6. at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1561) ~[spring-rabbit-3.0.10.jar:3.0.10]
  7. at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListenerAndHandleException(AbstractMessageListenerContainer.java:1506) ~[spring-rabbit-3.0.10.jar:3.0.10]
  8. at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.lambda$executeListener$8(AbstractMessageListenerContainer.java:1484) ~[spring-rabbit-3.0.10.jar:3.0.10]
  9. at io.micrometer.observation.Observation.lambda$observe$0(Observation.java:493) ~[micrometer-observation-1.11.5.jar:1.11.5]
  10. at io.micrometer.observation.Observation.observeWithContext(Observation.java:603) ~[micrometer-observation-1.11.5.jar:1.11.5]
  11. at io.micrometer.observation.Observation.observe(Observation.java:492) ~[micrometer-observation-1.11.5.jar:1.11.5]
  12. at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1484) ~[spring-rabbit-3.0.10.jar:3.0.10]
  13. at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:994) ~[spring-rabbit-3.0.10.jar:3.0.10]
  14. at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:941) ~[spring-rabbit-3.0.10.jar:3.0.10]
  15. at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1323) ~[spring-rabbit-3.0.10.jar:3.0.10]
  16. at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1225) ~[spring-rabbit-3.0.10.jar:3.0.10]
  17. at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
  18. Caused by: java.lang.SecurityException: Attempt to deserialize unauthorized class java.util.CollSer; add allowed class name patterns to the message converter or, if you trust the message orginiator, set environment variable 'SPRING_AMQP_DESERIALIZATION_TRUST_ALL' or system property 'spring.amqp.deserialization.trust.all' to true
  19. at org.springframework.amqp.utils.SerializationUtils.checkAllowedList(SerializationUtils.java:165) ~[spring-amqp-3.0.10.jar:3.0.10]
  20. at org.springframework.amqp.support.converter.AllowedListDeserializingMessageConverter.checkAllowedList(AllowedListDeserializingMessageConverter.java:61) ~[spring-amqp-3.0.10.jar:3.0.10]
  21. at org.springframework.amqp.support.converter.SimpleMessageConverter$1.resolveClass(SimpleMessageConverter.java:151) ~[spring-amqp-3.0.10.jar:3.0.10]
  22. at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:2045) ~[na:na]
  23. at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1909) ~[na:na]
  24. at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2235) ~[na:na]
  25. at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1744) ~[na:na]
  26. at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:514) ~[na:na]
  27. at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:472) ~[na:na]
  28. at org.springframework.amqp.utils.SerializationUtils.deserialize(SerializationUtils.java:102) ~[spring-amqp-3.0.10.jar:3.0.10]
  29. at org.springframework.amqp.support.converter.SimpleMessageConverter.fromMessage(SimpleMessageConverter.java:83) ~[spring-amqp-3.0.10.jar:3.0.10]
  30. at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.extractMessage(AbstractAdaptableMessageListener.java:342) ~[spring-rabbit-3.0.10.jar:3.0.10]
  31. at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter$MessagingMessageConverterAdapter.extractPayload(MessagingMessageListenerAdapter.java:385) ~[spring-rabbit-3.0.10.jar:3.0.10]
  32. at org.springframework.amqp.support.converter.MessagingMessageConverter.fromMessage(MessagingMessageConverter.java:132) ~[spring-amqp-3.0.10.jar:3.0.10]
  33. at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.toMessagingMessage(MessagingMessageListenerAdapter.java:258) ~[spring-rabbit-3.0.10.jar:3.0.10]
  34. at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:148) ~[spring-rabbit-3.0.10.jar:3.0.10]
  35. ... 15 common frames omitted
  36. 进程已结束,退出代码为 -1

解决方法

生成者代码:

  1. @Override
  2. public String registerEmailVerifyCode(String type, String email, String ip) {
  3. synchronized (ip.intern()){
  4. if (!this.verifyLimit(ip)) return "请求频繁,请稍后再试";
  5. Random random = new Random();
  6. int code = random.nextInt(899999) + 100000;
  7. Map<String, Object> data = Map.of("type", type, "email", email, "code", code);
  8. amqptemplate.convertAndSend("mail",data);
  9. stringRedisTemplate.opsForValue().set(Const.VERIFY_EMAIL_DATA + email,
  10. String.valueOf(code), 3, TimeUnit.MINUTES);
  11. return null;
  12. }
  13. }

消费者代码:

  1. @RabbitHandler
  2. public void sendMailMessage(Map<String, Object> data){
  3. String email = (String) data.get("email");
  4. Integer code = (Integer) data.get("code");
  5. String type = (String) data.get("type");
  6. SimpleMailMessage message = switch (type){
  7. case "register" ->
  8. createMessage("注册", "您的验证码为:" + code + "有效时间为一分钟,请勿向他人泄露验证码", email);
  9. case "reset" ->
  10. createMessage("重置密码", "您的验证码为:" + code + "有效时间为一分钟,请勿向他人泄露验证码", email);
  11. default -> null;
  12. };
  13. if (message == null) return;
  14. sender.send(message);
  15. }

rabbitmq配置代码:

  1. @Bean("emailQueue")
  2. public Queue emailQueue(){
  3. return QueueBuilder
  4. .durable("mail")
  5. .build();
  6. }

报错原因是因为生产者,消费者格式序列化,通过导入依赖

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.core</groupId>
  3. <artifactId>jackson-databind</artifactId>
  4. </dependency>

以及在rabbitmq配置类中添加Bean可以解决

  1. @Bean
  2. public MessageConverter jsonMessageConverter(){
  3. return new Jackson2JsonMessageConverter();
  4. }

解决方法引用了https://blog.csdn.net/Ammw_13/article/details/134586936

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/1018318
推荐阅读
相关标签
  

闽ICP备14008679号