我使用了一个消息转换器将XML消息从队列转换为Java对象,它工作正常。
由于我的JMSMessageListener直接获取POJO,我想知道有没有办法可以访问最初放在队列中的原始XML。
作为邮件跟踪的一部分,我需要维护原始xml邮件的副本。
在spring jms中是否有可用的回调,以便我可以在将xml消息转换为POJO之前保留它?
我的应用程序是spring boot,我在下面的代码中配置了消息转换器
@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;
}
}
这是听众代码
@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);
}
答案 0 :(得分:1)
这样的事情应该有用......
@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;
}
}
}
...但您应该添加更多支票(例如,在投票前验证类型)。