记录一下入门RabbitMQ问题
问题如下
2019-08-28 16:51:26.495 [SimpleAsyncTaskExecutor-1] WARN o.s.a.r.l.ConditionalRejectingErrorHandler - Execution of Rabbit message listener failed.
org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener method could not be invoked with the incoming message
Endpoint handler details:
Method [public void com.xuecheng.test.rabbitmq.mq.ReceiveHandler.send_email(java.lang.String,org.springframework.amqp.core.Message,java.nio.channels.Channel)]
Bean [com.xuecheng.test.rabbitmq.mq.ReceiveHandler@525bbd40]
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:185)
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:120)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1414)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1337)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1324)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1303)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:785)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:769)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$700(SimpleMessageListenerContainer.java:77)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1010)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.messaging.converter.MessageConversionException: Cannot convert from [java.lang.String] to [java.nio.channels.Channel] for GenericMessage [payload=send email message to user , headers={amqp_receivedDeliveryMode=PERSISTENT, amqp_receivedRoutingKey=inform.email, amqp_contentEncoding=UTF-8, amqp_receivedExchange=exchange_topics_inform, amqp_deliveryTag=4, amqp_consumerQueue=queue_inform_email, amqp_redelivered=false, id=8430fc82-e3b5-05b4-e766-bfd5a3620efa, amqp_consumerTag=amq.ctag-68FiFiTCUqFmuzuqH8x4sg, contentType=text/plain, timestamp=1566982286494}]
at org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver.resolveArgument(PayloadArgumentResolver.java:144)
at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:116)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:137)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:109)
at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:51)
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:182)
... 10 common frames omitted
2019-08-28 16:51:26.496 [SimpleAsyncTaskExecutor-1] WARN o.s.a.r.l.ConditionalRejectingErrorHandler$DefaultExceptionStrategy - Fatal message conversion error; message rejected; it will be dropped or routed to a dead letter exchange, if so configured: (Body:'send email message to user ' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange_topics_inform, receivedRoutingKey=inform.email, deliveryTag=4, consumerTag=amq.ctag-68FiFiTCUqFmuzuqH8x4sg, consumerQueue=queue_inform_email])
环境
Springboot 集成 RabbitMQ中的入门程序,生产者是正常的,消费者在接收的有问题。
这个问题的跟网上别人的不一样的地方就是:消费者已经确认了消息被消费了,也就是说消费者已经拿到了信息,RabbitMQ基本已经完成了自己的任务。
解决方案:
检查消费者代码中的import包路径是否正确
错误案列:
正确案例
Message,Channel两个包容易到错。
附上我的消费者代码:
package com.xuecheng.test.rabbitmq.mq;
import com.rabbitmq.client.Channel;
import com.xuecheng.test.rabbitmq.config.RabbitmqConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class ReceiveHandler {
@RabbitListener(queues = {RabbitmqConfig.QUEUE_INFORM_EMAIL})
public void send_email(String msg, Message message, Channel channel) {
System.out.println("receive message is: " + msg);
}
}
关注公众号 soft张三丰