【发布时间】:2017-12-31 06:04:55
【问题描述】:
我使用消息转换器将 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);
}
【问题讨论】:
-
>I have used a message converter to convert the XML message from queue to a Java Object and it works fine.如果您已经实现了自己的消息转换器,那么您已经可以访问那里的原始消息。我错过了什么?提出此类问题时,您需要展示您的配置。 -
同意。我已经用代码示例更新了这个问题。实际的转换是由 Spring 完成的。我为 JAXB 编组器提供所需的配置。
标签: spring spring-jms spring-oxm