赞
踩
Springboot整合RabbitMQ,启动不报错,收到消息就报错
ConditionalRejectingErrorHandler : Execution of Rabbit message listener failed
- 2022-02-23 18:57:59.718 WARN 24956 --- [ntContainer#0-1] 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_281]
- Caused by: org.springframework.amqp.AmqpException: No method found for class [B
- at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.getHandlerForPayload(DelegatingInvocableHandler.java:207) ~[spring-rabbit-2.4.2.jar:2.4.2]
- at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.getMethodFor(DelegatingInvocableHandler.java:339) ~[spring-rabbit-2.4.2.jar:2.4.2]
- at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.getMethodFor(HandlerAdapter.java:117) ~[spring-rabbit-2.4.2.jar:2.4.2]
- at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandlerAndProcessResult(MessagingMessageListenerAdapter.java:205) ~[spring-rabbit-2.4.2.jar:2.4.2]
- at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:146) ~[spring-rabbit-2.4.2.jar:2.4.2]
- ... 11 common frames omitted
网上搜了一下,说是
@RabbitListener(queues="hello")
应该放在方法上,不应该放在类上。如果不放在方法上,应该设置isDefault = true
Annotation that marks a method to be the target of a Rabbit message listener on the * specified {@link #queues()} (or {@link #bindings()}). The {@link #containerFactory()} * identifies the * {@link org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory * RabbitListenerContainerFactory} to use to build the rabbit listener container. If not * set, a <em>default</em> container factory is assumed to be available with a bean name * of {@code rabbitListenerContainerFactory} unless an explicit default has been provided * through configuration.
类注释上也是Annotation that marks a method to be the target of a Rabbit message listener
将 @RabbitListener(queues="hello") 放在方法上,用String接收,测试一下果然可以。
假如想用Message接收也可以。
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.amqp.core.Message;
- import org.springframework.amqp.rabbit.annotation.RabbitHandler;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Component;
-
- @Slf4j
- @Component
- @RabbitListener(queues = "hello")
- public class Receiver {
- @RabbitHandler(isDefault = true)
- public void process(Message msg){
- log.info("Receive rabbitmq message:"+new String(msg.getBody()));
- }
- }
所以结论是:
将 @RabbitListener(queues="hello") 放在方法上,或者在@RabbitHandler上设置isDefault = true
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。