Spring JmsTemplate - add custom Property
Asked Answered
C

2

16

I am using the Spring API's JmsTemplate and MappingJackson2MessageConverter (version: spring-jms-4.3.4.RELEASE.jar) to publish messages to an ActiveMQ topic as shown in the below code.

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 few custom headers to the JMS message being published to the topic. I googled and could not find any example which does this. Can you help ?

Chondro answered 21/2, 2017 at 12:25 Comment(1)
Any reason why typeIdPropertyName = _type?Gilbertine
C
29

You can add custom properties by using convertAndSendmethod from JmsTemplate 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;
            }
        });

Alternative lambda syntax:

jmsTemplate.convertAndSend(
            "product.topic", product,
            message -> { message.setStringProperty("my_property", "my_value"); return message;}
    );
Cherub answered 21/2, 2017 at 13:11 Comment(4)
message.clearProperties(); Should Add this line before setStringProperty . Otherwise javax.jms.MessageNotWriteableException would be thrownEdvard
jmsTemplate.convertAndSend("product.topic", product, m -> { m.setStringProperty("my_property", "my_value"); return m });Fluoridate
what if I want to add ByteBuffer property, for example SQS supports Binary message attribute datatype, but I'm not sure that Spring JMS abstraction supports those?Gellman
Thank you very much!!!!!!!!!!!!!Shantell
R
3

Try something like this:

@SendTo("product.topic")
public Message<Product> send() {
    Product product = new Product();
    product.setName("abcd");
    product.setPrice(10);

    return MessageBuilder
            .withPayload(product)
            .setHeader("code", 1234) // custom header name and value
            .build();
}

Reference: https://spring.io/blog/2014/04/30/spring-4-1-s-upcoming-jms-improvements

Rasia answered 21/2, 2017 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.