当前位置:   article > 正文

RabbitMQ Failed to convert message.No method found for class java.lang.String问题解决

failed to convert message

问题描述:

        org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Failed to convert message
        Caused by: org.springframework.amqp.AmqpException: No method found for class java.lang.String

问题分析:

1、消息生产者发送的消息类型为String,消息消费者接收的消息类型为Message,导致接收的时候类型转换不对。

  1. @Component
  2. public class MessageProvider {
  3. @Autowired
  4. private RabbitTemplate rabbitTemplate;
  5. public void send(String exchange,String routingKey,String content){
  6. rabbitTemplate.convertAndSend(exchange,routingKey,content);
  7. }
  8. }
  1. @Component
  2. @RabbitListener(queues = "directqueue2")
  3. public class MessageReceiver {
  4. @RabbitHandler
  5. public void process(Message message) throws InterruptedException {
  6. Thread.sleep(1000);
  7. }
  8. }

解决办法:消息生产者发送的消息类型和消息消费者接收的消息类型一致即可。

  1. @Component
  2. public class MessageProvider {
  3. @Autowired
  4. private RabbitTemplate rabbitTemplate;
  5. public void send(String exchange,String routingKey,String content){
  6. rabbitTemplate.convertAndSend(exchange,routingKey,content.getBytes());
  7. }
  8. }
  1. @Component
  2. @RabbitListener(queues = "directqueue2")
  3. public class MessageReceiver {
  4. @RabbitHandler
  5. public void process(byte[] message) throws InterruptedException {
  6. Thread.sleep(1000);
  7. }
  8. }

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

闽ICP备14008679号