1

I have used a message converter to convert the XML message from queue to a Java Object and it works fine.

Since my JMSMessageListener get the POJO directly, I would like to know is there any way I can have access to the raw XML which was originally placed in queue.

As part of message tracking, I need to maintain a copy of the raw xml message.

Is there any call back available in spring jms so that I can persits the xml message before it is converted into POJO ?

My application is spring boot and I am configuring the message convertor in the below code

@Configuration
@EnableJms
public class JMSConfig {

    @Bean
    public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
            DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        // This provides all boot's default to this factory, including the message
        // converter
        configurer.configure(factory, connectionFactory);
        // You could still override some of Boot's default if necessary.
        return factory;
    }

    @Bean
    public MarshallingMessageConverter createMarshallingMessageConverter(final Jaxb2Marshaller jaxb2Marshaller) {
        return new MarshallingMessageConverter(jaxb2Marshaller);
    }

    @Bean
    public Jaxb2Marshaller createJaxb2Marshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();

        jaxb2Marshaller.setPackagesToScan("com.mypackage.messageconsumer.dto");

        Map<String, Object> properties = new HashMap<>();
        properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxb2Marshaller.setMarshallerProperties(properties);

        return jaxb2Marshaller;
    }
}

This is the listener code

@Component
public class NotificationReader {

    @JmsListener(destination = "myAppQ")
    public void receiveMessage(NotificationMessage notificationMessage) {
        System.out.println("Received <" + notificationMessage.getStaffNumber() + ">");
        // how to get access to the raw xml recieved by sender ? 
        persistNotification(notificationMessage);
    }
lives
  • 1,243
  • 5
  • 25
  • 61
  • `>I have used a message converter to convert the XML message from queue to a Java Object and it works fine.` If you have implemented your own message converter then you already have access to the original message there. What am I missing? You need to show your configuration when asking questions like this. – Gary Russell Dec 31 '17 at 15:01
  • Agree. I have updated the question with the code sample. The actual conversion is done by Spring. I provide the needed configuration for the JAXB marshaller. – lives Jan 01 '18 at 06:07

1 Answers1

3

Something like this should work...

@Bean
public MarshallingMessageConverter createMarshallingMessageConverter(final Jaxb2Marshaller jaxb2Marshaller) {
    return new MarshallingMessageConverter(jaxb2Marshaller) {

        @Override
        public Object fromMessage(Message message) throws JMSException, MessageConversionException {
            Object object = super.fromMessage(message);
            ((MyObject) object).setSourceXML(((TextMessage) message).getText());
            return object;
        }

    }
}

...but you should add more checks (e.g. verify types before casting).

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • yep. for casting , code may need to have dependency on the underlying implementation ( active mq / ibm mq ) – lives Jan 02 '18 at 05:54
  • What is MyObject here, is it something like JMSTextMessage. Sorry to post in comments, I have exact similar requirement – Nagendra Busam Mar 04 '21 at 19:11
  • 1
    `MyObject` is a POJO unmarshalled from the incoming XML using Jaxb. The converter can handle `TextMessage` and `BytesMessage` out of the box, and other types of JMS `Message` with a subclass overriding `unmarshalFromMessage()`. His/her requirement was to retain the original XML as well as unmarshalling it. – Gary Russell Mar 04 '21 at 19:40