Java – spring jmstemplate – add custom attributes

I use the spring API's JMS template and mappingjackson2messageconverter (version: spring-jms-4.3.4. Release. Jar) to publish messages

Topicpublisher class:

@Component
public class Topicpublisher {

    @Autowired
    private jmstemplate jmstemplate;

    @Autowired
    private MessageConverter messageConverter;

    public void send() {
        Product product = new Product();
        product.setName("abcd");
        product.setPrice(10);

        jmstemplate.setMessageConverter(messageConverter);
        jmstemplate.convertAndSend("product.topic",product);
    }
}

Mappingjackson2messageconverter class:

@Configuration
public class JMSTextMessageConverter {

    @Bean
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter mappingJackson2MessageConverter 
             = new MappingJackson2MessageConverter();
        mappingJackson2MessageConverter.setTargetType(MessageType.TEXT);
        mappingJackson2MessageConverter.setTypeIdPropertyName("_type");
        return mappingJackson2MessageConverter;
    }   
}

Now, I want to set some custom headers for JMS messages published to the topic I use Google search and can't find any examples of this Can you help me?

Solution

You can use convertandsendmethod to add custom properties from jmtemplate by sending messagepostprocessor, as shown below:

jmstemplate.convertAndSend("product.topic",product,new MessagePostProcessor() {
        @Override
        public Message postProcessMessage(Message message) throws JMSException {
            message.setStringProperty("my_property","my_value");
            return message;
        }
    });
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>