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感谢~:)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。