使用Spring JmsMessagingTemplate的自定义MessageConverter无法正常工作

时间:2018-05-23 14:17:06

标签: spring spring-boot spring-jms spring-messaging

我尝试将实现org.springframework.jms.support.converter.MessageConverter的自定义消息转换器附加到JmsMessagingTemplate

我已经在某个地方读过,我们可以通过调用MessagingMessageConverter将消息转换器附加到setPayloadConverter,然后通过{{1}将该消息传递转换器附加到JmsMessagingTemplate }}。在那之后,我打电话给setJmsMessageConverter,但我注意到它没有转换有效载荷。

当我调试代码时,我注意到设置Jms Message Converter并没有在convertAndSend中设置converter实例变量。因此,当JmsMessagingTemplate方法调用convertAndSend并尝试doConvert时,它将获取默认的简单消息转换器,而不是我的自定义消息转换器。

我的问题是,我可以使用getConverterorg.springframework.jms.support.converter.MessageConverter的实现吗?或者我是否需要使用JmsMessagingTemplate的实现?

我使用的是Spring Boot 1.4.1.RELEASE和Spring 4.3.3.RELEASE。代码如下。

配置

org.springframework.messaging.converter.MessageConverter

MessageProducer类

@Configuration
@EnableJms
public class MessagingEncryptionPocConfig {
    /**
     * Listener ActiveMQ Connection Factory
     */
    @Bean(name="listenerActiveMqConnectionFactory")
    public ActiveMQConnectionFactory listenerActiveMqConnectionFactory() {
        return new ActiveMQConnectionFactory("admin","admin","tcp://localhost:61616");
    }

    /**
     * Producer ActiveMQ Connection Factory
     */
    @Bean(name="producerActiveMqConnectionFactory")
    public ActiveMQConnectionFactory producerActiveMqConnectionFactory() {
        return new ActiveMQConnectionFactory("admin","admin","tcp://localhost:61616");
    }   

    /**
     * Caching Connection Factory
     */
    @Bean
    public CachingConnectionFactory cachingConnectionFactory(@Qualifier("producerActiveMqConnectionFactory") ActiveMQConnectionFactory activeMqConnectionFactory) {
        return new CachingConnectionFactory(activeMqConnectionFactory);
    }

    /**
     * JMS Listener Container Factory
     */
    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(@Qualifier("listenerActiveMqConnectionFactory") ActiveMQConnectionFactory connectionFactory, MessagingMessageConverter messageConverter) {
        DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
        defaultJmsListenerContainerFactory.setConnectionFactory(connectionFactory);
        defaultJmsListenerContainerFactory.setMessageConverter(messageConverter);
        return defaultJmsListenerContainerFactory;
    }

    /**
     * Jms Queue Template
     */
    @Bean(name="queueTemplate")
    public JmsMessagingTemplate queueTemplate(CachingConnectionFactory cachingConnectionFactory, MessageConverter messagingMessageConverter) {
        JmsMessagingTemplate queueTemplate = new JmsMessagingTemplate(cachingConnectionFactory);
        queueTemplate.setJmsMessageConverter(messagingMessageConverter);
        return queueTemplate;
    }

    @Bean
    public MessageConverter encryptionDecryptionMessagingConverter(Jaxb2Marshaller jaxb2Marshaller) {
        MessageConverter encryptionDecryptionMessagingConverter = new EncryptionDecryptionMessagingConverter(jaxb2Marshaller);
        MessagingMessageConverter messageConverter = new MessagingMessageConverter();
        messageConverter.setPayloadConverter(encryptionDecryptionMessagingConverter);
        return messageConverter;
    }

    /**
     * Jaxb marshaller
     */
    @Bean(name="producerJaxb2Marshaller")
    public Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setPackagesToScan("com.schema");
        return jaxb2Marshaller;
    }
}

单元测试

@Component 
public class MessageProducer {

    private static final Logger LOG = LoggerFactory.getLogger(MessageProducer.class);

    @Autowired
    @Qualifier("queueTemplate")
    private JmsMessagingTemplate queueTemplate;

    public void publishMsg(Transaction trx, Map<String,Object> jmsHeaders, MessagePostProcessor postProcessor) {
        LOG.info("Sending Message. Payload={} Headers={}",trx,jmsHeaders);
        queueTemplate.convertAndSend("queue.source", trx, jmsHeaders, postProcessor);
    }
}

1 个答案:

答案 0 :(得分:3)

converter字段用于将输入参数转换为弹簧消息Message<?>

稍后(在MessagingMessageCreator中)使用JMS转换器,然后从消息传递Message创建JMS Message<?>