Sending Beans as XML with JmsTemplate
Join the DZone community and get the full member experience.
Join For FreeWe often want to send XML via web services. We may already have the schema or annotated JAXB2 classes configured in our application. What if we want to send the same format via JMS? By default Spring JMS is configured to send & receive objects serialized. How can we switch to using JAXB2 (or any other OXM marshaling strategy)?
The following example assumes we are going from annotations first instead of from XML Schema.
Quick Overview
- Annotate Bean with JAXB2
- Configure OXM Converter
- Integration Test
- Visualize Results
- Logging Configuration
- Maven Configuration
1. Annotate Bean with JAXB2
- Use JAXB2 annotations for our bean
2. Configure OXM Converter
- Define our Marshalers – We see <oxm:jaxb2-marshaller ...> defines JAXB2 as our marshaller for the Account class
- Register our MarshallingMessageConverter – We register the MarshallingMessageConverter to use the JAXB2 marshaller for both inbound and outbound data
- Register our Converter – In the JmsTemplate, we register our oxmMessageConverter as the messageConverter. This replaces the default SimpleMessageConverter which will relies on Serialization
- Notice the ActiveMQ namespace?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<amq:broker persistent="false" useJmx="true">
<amq:transportConnectors>
<amq:transportConnector uri="tcp://localhost:61616" />
</amq:transportConnectors>
</amq:broker>
<amq:connectionFactory brokerURL="vm://localhost" id="jmsFactory" />
<!-- Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory" />
<property name="defaultDestination" ref="oxmTestQueue" />
<property name="messageConverter" ref="oxmMessageConverter" />
</bean>
<amq:queue id="oxmTestQueue" physicalName="oxm.test.queue" />
<bean id="oxmMessageConverter"
class="org.springframework.jms.support.converter.MarshallingMessageConverter">
<property name="marshaller" ref="marshaller" />
<property name="unmarshaller" ref="marshaller" />
</bean>
<oxm:jaxb2-marshaller id="marshaller">
<oxm:class-to-be-bound name="com.gordondickens.jmswithoxm.Account" />
</oxm:jaxb2-marshaller>
</beans>
3. Integration Test
- Populate the Account bean
- Calls convertAndSend on the JmsTemplate to marshal & send the Account
- Includes a postProcessor callback to log the XML data
- Calls receiveAndConvert on the JmsTemplate to get & unmarshal the Account
- Asserts end state
4. Visualizing Results
- Run: mvn clean test
- See Account XML between the block <!-- MSG START --> & <!--MSG END -->
- Output has been Formatted for clarity
5. Logging Configuration
- Logback is very similar to Log4J
- Logback patterns use Log4J characters also
- See: Logback Pattern Layout Documentation
6. Maven Configuration
- Using Logback to support Log4J, SLF4J, Apache (JCL) & Java Util Logging
- Included IDE builders for STS/Eclipse & IntelliJ IDEA
6. Getting the code
- Code is in my Git Repo:
https://github.com/gordonad/core-spring-demos/tree/master/demos/JmsOxmDemo
Further Reading
- Spring Reference for JMS – Section 21.3.1 Using Message Converters
- Core Spring Training – Where I teach JMS with Spring
- Enterprise Integration with Spring Training – Where I teach JMS with Spring
From http://gordondickens.com/wordpress/2011/02/07/sending-beans-as-xml-with-jmstemplate/
Opinions expressed by DZone contributors are their own.
Comments