开发者社区> 问答> 正文

spring boot中集成rabbitMQ启动报错 ?报错

图片说明
一直无线重启,测试方式用debug模式,程序能跑完,还是报错

展开
收起
爱吃鱼的程序员 2020-06-23 21:00:39 839 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    HelloReceiver类的注解使用有问题,----@RabbitListener注解是方法级别的,不能用在class上。
    正确的代码如下:

    @Component

    publicclassObjectReceiver{

    @RabbitListener(queues="object")
    @RabbitHandler
    publicvoidprocess(Useruser){

    System.out.println("Receiverobject:"+user);

    }

    }

    2017-03-2917:59:53.250WARN30172---[cTaskExecutor-1]s.a.r.l.ConditionalRejectingErrorHandler:ExecutionofRabbitmessagelistenerfailed.

    org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException:Listenerthrewexception
    atorg.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapToListenerExecutionFailedExceptionIfNeeded(AbstractMessageListenerContainer.java:876)~[spring-rabbit-1.7.1.RELEASE.jar:na]
    atorg.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:786)~[spring-rabbit-1.7.1.RELEASE.jar:na]
    atorg.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:706)~[spring-rabbit-1.7.1.RELEASE.jar:na]
    atorg.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:96)[spring-rabbit-1.7.1.RELEASE.jar:na]
    atorg.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:187)~[spring-rabbit-1.7.1.RELEASE.jar:na]
    atorg.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1274)[spring-rabbit-1.7.1.RELEASE.jar:na]
    atorg.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:687)~[spring-rabbit-1.7.1.RELEASE.jar:na]
    atorg.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1217)[spring-rabbit-1.7.1.RELEASE.jar:na]
    atorg.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:1187)[spring-rabbit-1.7.1.RELEASE.jar:na]
    atorg.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1600(SimpleMessageListenerContainer.java:96)[spring-rabbit-1.7.1.RELEASE.jar:na]
    atorg.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1414)[spring-rabbit-1.7.1.RELEASE.jar:na]
    atjava.lang.Thread.run(Thread.java:745)[na:1.8.0_121]
    Causedby:org.springframework.amqp.AmqpException:Nomethodfoundforclass[B
    一直报错找不到没有找到方法类

    Sender代码
    @Component
    publicclassHelloSender{

    @AutowiredprivateAmqpTemplateamqpTemplate;publicvoidsend(){Stringcontext="hello"+newDate();System.out.println("Sender:"+context);this.amqpTemplate.convertAndSend("hello",context);}

    Receiver代码
    @Component
    @RabbitListener(queues="hello")
    publicclassHelloReceiver{

    @RabbitHandlerpublicvoidprocess(Stringhello){System.out.println("Receiver:"+hello);}

    }

    config配置代码
    @Configuration
    publicclassRabbitConfig{

    @BeanpublicQueuehelloQueue(){returnnewQueue("hello");}@BeanpublicQueueneoQueue(){returnnewQueue("neo");}@BeanpublicQueueobjectQueue(){returnnewQueue("object");}

    }

    测试代码
    @RunWith(SpringRunner.class)
    @SpringBootTest
    publicclassHelloTest{

    @AutowiredprivateHelloSenderhelloSender;@Testpublicvoidhello()throwsException{helloSender.send();}

    }

    @Component
    @RabbitListener(queues="object")
    publicclassObjectReceiver{

    @RabbitHandlerpublicvoidprocess(Useruser){System.out.println("Receiverobject:"+user);}

    }

    @Component
    publicclassObjectSender{

    @AutowiredprivateAmqpTemplateamqpTemplate;publicvoidsend(Useruser){System.out.println("Senderobject:"+user.toString());this.amqpTemplate.convertAndSend("object",user);}

    }

    这是代码

    可能是实体没有序列化,实现序列化试试

    真的是@RabbitListener注解问题,我也写到class类上了,换到方法上果然ok感谢~:)

    2020-06-23 21:00:55
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载