Listener method could not be invoked with the incoming message RabbitMQ消息错误

IT黑名单 2017-12-22 13:28:00

Dec 22, 2017 11:15:18 AM org.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler handleError
WARNING: 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.xxx.xxx.handler.NewMessageHandler.onMessage(java.util.Map<java.lang.String, java.lang.String>)]
Bean [com.xxx.xxx.handler.NewMessageHandler@732d0d24]
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:128)
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:105)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:777)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:700)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:95)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:187)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1187)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:681)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1165)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:1149)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1100(SimpleMessageListenerContainer.java:95)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1312)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.amqp.support.converter.MessageConversionException: Cannot handle message
	... 13 more
Caused by: org.springframework.messaging.converter.MessageConversionException: Cannot convert from [java.lang.String] to [java.util.Map] for GenericMessage [payload=james, headers={amqp_receivedDeliveryMode=PERSISTENT, amqp_receivedRoutingKey=xxx.xxx.pusher, amqp_contentEncoding=UTF-8, amqp_receivedExchange=xxx.xxx, amqp_deliveryTag=1, amqp_consumerQueue=xxx.xxx.pusher, amqp_redelivered=false, id=f3ead76a-8b0c-1304-20cc-d156a007d9f1, amqp_consumerTag=amq.ctag-R8sGYgl7sBlisA64mdjx2w, contentType=text/plain, timestamp=1513912518627}]
	at org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver.resolveArgument(PayloadArgumentResolver.java:124)
	at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:98)
	at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:138)
	at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:107)
	at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:49)
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:125)
	... 12 more

Dec 22, 2017 11:15:18 AM org.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler$DefaultExceptionStrategy isFatal
WARNING: Fatal message conversion error; message rejected; it will be dropped or routed to a dead letter exchange, if so configured: null

原因:消息生产者和消息消费者发送/接收消息类型不匹配,两者使用同一类型接收即可。

值得注意的是,我这里报错接收方是Map,发送方也是Map,但是泛型不一致也会报错。

// 生产者
Map<String, Object> msg = new HashMap<String, Object>();
producer.send("", msg);

// 消费者  报错
@RabbitListener(queues = "queuesName")
public void onMessage(Map<String, String> data) {
	// TODO
}

// 消费者  正确
@RabbitListener(queues = "queuesName")
public void onMessage(Map<String, Object> data) { // TODO }

转载请注明来源【IT黑名单

本文链接:http://blog.itblacklist.cn/20171222/8466.html

© Copyright 2016 IT黑名单 Inc.All Rights Reserved. 豫ICP备15018592号-2