如何使用 Spring RabbitMQ MessageListener 获取 MessageProperties
How to get MessageProperties with Spring RabbitMQ MessageListener
我正在创建一个 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");
}
目前可以接收消息,但前提是我的监听器的 returning 类型是 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() 的 return 类型更改为 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;
}
我正在创建一个 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");
}
目前可以接收消息,但前提是我的监听器的 returning 类型是 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() 的 return 类型更改为 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;
}