我正在创建一个Spring MessageListenerAdapter,它正在侦听XML消息的队列。
这是我的amqp配置:
@Bean()
SimpleMessageListenerContainer simpleMessageListenerContainer(ConnectionFactory connectionFactory,
MessageListenerAdapter messageListener) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames("queueA", "queueB");
container.setMessageListener(messageListener);
container.setChannelTransacted(true);
return container;
}
@Bean
MessageListenerAdapter messageListener(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
当前,接收消息有效,但是仅当我的侦听器的返回类型为
String
时才可以。通过以下侦听器,我收到了我的XML消息,所以很好。但是我无法获得原始消息的
messageProperties
:@Component
public class Receiver {
public void receiveMessage(String message) {
try {
if (message.isEmpty()) {
log.info("---> Received message is empty!");
} else {
log.info("---> Received message: <{}>", message);
}
} catch (Exception e) {
log.error("---> Exception in processing receiv
ed message!", e);
}
}
}
如果我将receiveMessage()的返回类型更改为
bytes[]
或Message
,则会收到以下错误消息:org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Failed to invoke target method 'receiveMessage' with argument type = [class java.lang.String], value = [{myXmlMessage}]
我已经尝试过MessageConverters,但是我是RabbitMQ的新手。谢谢你的帮助!
最佳答案
我自己找到了解决方案:默认情况下,MessageConverter用于MessageListenerAdapter。为了防止这种情况,必须将MessageConverter设置为null。现在它可以工作了,我收到包含所有messageProperties的纯消息!
@Bean
MessageListenerAdapter messageListener(Receiver receiver) {
MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(receiver, "receiveMessage");
messageListenerAdapter.setMessageConverter(null);
return messageListenerAdapter;
}