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);
}
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).