[英]Spring JMS json message converter
I have some problems to convert messages from RabbitMQ using spring-jms module. 使用spring-jms模块从RabbitMQ转换消息时遇到一些问题。 Previously I send my messages using Rest API endpoint, this endpoint send the message to RabbitMQ queue and process it using @JmsListener methods. 以前,我使用Rest API端点发送消息,此端点将消息发送到RabbitMQ队列并使用@JmsListener方法进行处理。
Internally this behaviour add a field to determine the Java type, managed by Spring libraries. 在内部,此行为添加一个字段来确定Java类型,该字段由Spring库管理。 However now I want to avoid the Rest API call because it's not necessary and I can send the message directly to RabbitMQ. 但是现在我想避免Rest API调用,因为这不是必需的,我可以直接将消息发送到RabbitMQ。
The problem is when spring try to convert JMS message for @JmsListener parameter. 问题是当spring尝试为@JmsListener参数转换JMS消息时。 I didn't set the type on the message because the sender doesn't need that information but now I cannot convert it directly because MappingJackson2MessageConverter want a property for that. 我没有在消息上设置类型,因为发件人不需要该信息,但是现在我无法直接转换它,因为MappingJackson2MessageConverter想要一个属性。
How can I avoid this problem? 如何避免这个问题? I want to be able to process all message without needs to know the type in all the "senders". 我希望能够处理所有消息,而无需知道所有“发件人”中的类型。
Message converter bean: 消息转换器bean:
@Bean
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
Listener sample code: 侦听器示例代码:
@JmsListener(concurrency = "listener.concurrency", destination = "queue.name", containerFactory = "container.factory")
public void processFile(MyDtoClass dto) {
....
}
Subclass the converter and override getJavaTypeForMessage()
to return the type you want... 子类化转换器并重写getJavaTypeForMessage()
以返回所需的类型...
/**
* Determine a Jackson JavaType for the given JMS Message,
* typically parsing a type id message property.
* <p>The default implementation parses the configured type id property name
* and consults the configured type id mapping. This can be overridden with
* a different strategy, e.g. doing some heuristics based on message origin.
* @param message the JMS Message to set the type id on
* @throws JMSException if thrown by JMS methods
* @see #setTypeIdOnMessage(Object, javax.jms.Message)
* @see #setTypeIdPropertyName(String)
* @see #setTypeIdMappings(java.util.Map)
*/
protected JavaType getJavaTypeForMessage(Message message) throws JMSException {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.