ActiveMQ数据接收类型问题
一、问题描述
最近开发了一个工具,功能是监听ActiveMQ消息然后做相应的处理,本地自测没有问题,但是部署在现场出现如下报错:
[WARN ] [2020-08-27 19:49:42] [org.springframework.jms.listener.AbstractMessageListenerContainer.invokeErrorHandler(AbstractMessageListenerContainer.java:937)] Execution of JMS message listener failed, and no ErrorHandler has been set.
org.springframework.jms.listener.adapter.ListenerExecutionFailedException: Listener method could not be invoked with incoming message
Endpoint handler details:
Method [public void com.hikvision.js.facecompare.service.impl.ConsumerListenerImpl.consumer(java.lang.String)]
Bean [com.hikvision.js.facecompare.service.impl.ConsumerListenerImpl@6c591143]
; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot convert from [java.util.HashMap] to [java.lang.String] for org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage@2ebb7cc7, failedMessage=org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage@2ebb7cc7
at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:118)
at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:77)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:744)
at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:704)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:674)
at org.springframework.jms.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:645)
at org.springframework.jms.listener.SimpleMessageListenerContainer.processMessage(SimpleMessageListenerContainer.java:322)
at org.springframework.jms.listener.SimpleMessageListenerContainer.lambda$createListenerConsumer$2(SimpleMessageListenerContainer.java:299)
at org.apache.activemq.ActiveMQMessageConsumer.dispatch(ActiveMQMessageConsumer.java:1404)
at org.apache.activemq.ActiveMQSessionExecutor.dispatch(ActiveMQSessionExecutor.java:131)
at org.apache.activemq.ActiveMQSessionExecutor.iterate(ActiveMQSessionExecutor.java:202)
at org.apache.activemq.thread.PooledTaskRunner.runTask(PooledTaskRunner.java:133)
at org.apache.activemq.thread.PooledTaskRunner$1.run(PooledTaskRunner.java:48)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.messaging.converter.MessageConversionException: Cannot convert from [java.util.HashMap] to [java.lang.String] for org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage@2ebb7cc7, failedMessage=org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage@2ebb7cc7
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.jms.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:114)
... 15 more
阅读报错很容易发现是数据类型转换报错Cannot convert from [java.util.HashMap] to [java.lang.String],于是猜测代码希望我们使用map来接收,而我使用的是String,报错代码如下:
/** * 消息接收处理方法 */ @JmsListener(destination = "${activeMqTopic}", containerFactory = "topicListener") public void consumer(String message) { log.info("接收到一条消息:" + message); ... }
二、解决方法
将数据接收类型修改为ActiveMQMapMessage,修改后代码如下:
/** * 消息接收处理方法 */ @JmsListener(destination = "${activeMqTopic}", containerFactory = "topicListener") public void consumer(ActiveMQMapMessage message) throws Exception { log.info("接收到一条MQ消息"); ... }
三、总结
第一时间发现问题后,我将数据接收类型修改为Map,然后打包发给现场技术支持,技术支持重新部署后,称还是报相同的错,但实际使用Map接收也是可以的(估计是技术支持操作有问题)。
ActiveMQ的5种消息类型:
- TextMessage:java.lang.String对象,如xml文件内容。
- MapMessage:key/value键值对的集合,key是String对象,值类型可以是Java任何基本类型。
- BytesMessage:字节流。
- StreamMessage:Java 中的输入输出流。
- ObjectMessage:Java中的可序列化对象。