Search code examples
javaibm-mqspring-jmsjmstemplate

Add an outgoing interceptor on JMS messages


I'm working on some project where I'm building some JMS messages that I want to send using JmsTemplate, which is initialized in a Spring Boot app, via JNDI naming.

As my Broker is IBM MQ Series, I need to change the encoding value in the outgoing XML message, so I'm sure the broker will understand the message.

I haven't seen anywhere on the JmsTemplatesettings, or the ConnectionFactory JmsTemplate need to be initialized, where to put some outgoing interceptors.

If anyone got some ideas, or any workaround or some framework/library that could help, feel free to give me some hint.

Thanks


Solution

  • I assume you are using JAXB to create the XML message.

    Configure your JmsTemplate with a MarshallingMessageConverter that uses a Jaxb2Marshaller where you can set the marshaller property JAXB_ENCODING:

    Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); 
    Map properties = new HashMap<String, String>();
    properties.put(javax.xml.bind.Marshaller.JAXB_ENCODING, "YOUR-ENCODING");
    jaxb2Marshaller.setMarshallerProperties(properties);
    jaxb2Marshaller.setPackagesToScan("your.package");
    
    JmsTemplate jms = new JmsTemplate(connectionFactory);
    jms.setMessageConverter(new MarshallingMessageConverter(jaxb2Marshaller));
    ...
    jms.convertAndSend(yourObject);
    ...