Chapter 6. Working with Message Marshalers
Abstract
When using JMS endpoints, you may want to customize how messages are processed as they are passed into and out of the ESB. The Red Hat JBoss Fuse JMS binding component allows you to write custom marshalers for your JMS endpoints.
Important
The Java Business Integration components of Red Hat JBoss Fuse are considered deprecated. You should consider migrating any JBI applications to OSGi.
6.1. Consumer Marshalers
Overview
Consumer endpoints use an implementation of the
org.apache.servicemix.jms.endpoints.JmsConsumerMarshaler
interface to process the incoming JMS messages and convert them into normalized messages. Consumer marshalers also convert fault messages and response messages into JMS messages that can be returned to the remote endpoint. The JMS binding component comes with two consumer marshaler implementations:
DefaultConsumerMarshaler
- The
DefaultConsumerMarshaler
class provides the marshaler used by generic consumer endpoints and the JCA consumer endpoints. JmsSoapConsumerMarshaler
- The
JmsSoapConsumerMarshaler
class provides the marshaler used by SOAP consumer endpoints.NoteThe default SOAP marshaler does not support the full range of SOAP messages nor does it support marshaling map based messages into JMS messages.
When the default consumer marshaler does not suffice for your application you can provide a custom implementation of the
JmsConsumerMarshaler
interface.
Implementing the marshaler
To create a custom consumer marshaler, you implement the
org.apache.servicemix.jms.endpoints.JmsConsumerMarshaler
interface. The JmsConsumerMarshaler
interface, shown in Example 6.1, “The Consumer Marshaler Interface”, has five methods that need implementing:
Example 6.1. The Consumer Marshaler Interface
public interface JmsConsumerMarshaler { public interface JmsContext { Message getMessage(); } JmsContext createContext(Message message) throws Exception; MessageExchange createExchange(JmsContext jmsContext, ComponentContext jbiContext) throws Exception; Message createOut(MessageExchange exchange, NormalizedMessage outMsg, Session session, JmsContext context) throws Exception; Message createFault(MessageExchange exchange, Fault fault, Session session, JmsContext context) throws Exception; Message createError(MessageExchange exchange, Exception error, Session session, JmsContext context) throws Exception; }
createContext()
- The
createContext()
method takes the JMS message and returns an object that implements theJmsContext
interface. createExchange()
- The
createExchange()
creates a message exchange using the JMS message and the JBI context. Creating a message exchange entails the creation of the exchange, populating the exchange's in message, specifying the message exchange pattern to use, and setting any other required properties. createOut()
- The
createOut()
method takes the response message from the message exchange and converts it into a JMS message. The method takes the message exchange, the outgoing message, the active JMS session, and the JMS context. createFault()
- The
createFault()
method is called if a fault message is returned. It takes the message exchange, the fault message, the active JMS session, and the JMS context and returns a JMS message that encapsulates the fault message. createError()
- The
createError()
method is called if an exception is thrown while the message exchange is being processed. It takes the message exchange, the exception, the active JMS session, and the JMS context and returns a JMS message that encapsulates the exception.
In addition to implementing the methods, you need to provide an implementation of the
JmsContext
interface. The JmsContext
interface has a single method called getMessage()
which returns the JMS message contained in the context.
Example 6.2, “Consumer Marshaler Implementation” shows a simple consumer marshaler implementation.
Example 6.2. Consumer Marshaler Implementation
package com.widgetVendor.example; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import javax.jbi.component.ComponentContext; import javax.jbi.messaging.Fault; import javax.jbi.messaging.MessageExchange; import javax.jbi.messaging.NormalizedMessage; import javax.jms.Message; import javax.jms.Session; import javax.jms.TextMessage; import javax.xml.transform.Source; import org.apache.servicemix.jbi.jaxp.SourceTransformer; import org.apache.servicemix.jbi.jaxp.StringSource; import org.apache.servicemix.jbi.messaging.MessageExchangeSupport; public class widgetConsumerMarshaler implements JmsConsumerMarshaler { public JmsContext createContext(Message message) throws Exception { return new Context(message); } public MessageExchange createExchange(JmsContext jmsContext, ComponentContext jbiContext) throws Exception { Context ctx = (Context) jmsContext; MessageExchange exchange = jbiContext.getDeliveryChannel().createExchangeFactory().createExchange(MessageExchangeSupport.IN_ONLY); NormalizedMessage inMessage = exchange.createMessage(); TextMessage textMessage = (TextMessage) ctx.message; Source source = new StringSource(textMessage.getText()); inMessage.setContent(source); exchange.setMessage(inMessage, "in"); return exchange; } public Message createOut(MessageExchange exchange, NormalizedMessage outMsg, Session session, JmsContext context) throws Exception { String text = new SourceTransformer().contentToString(outMsg); return session.createTextMessage(text); } public Message createFault(MessageExchange exchange, Fault fault, Session session, JmsContext context) throws Exception { String text = new SourceTransformer().contentToString(fault); return session.createTextMessage(text); } public Message createError(MessageExchange exchange, Exception error, Session session, JmsContext context) throws Exception { throw error; } protected static class Context implements JmsContext { Message message; Context(Message message) { this.message = message; } public Message getMessage() { return this.message; } } }
Configuring the consumer
You configure a consumer to use a custom marshaler using its
marshaler
attribute. The marshaler
attribute's value is a reference to a bean
element specifying the class of your custom marshaler implementation.
Example 6.3, “Configuring a Consumer to Use a Customer Marshaler” shows configuration for a consumer that uses a custom marshaler.
Example 6.3. Configuring a Consumer to Use a Customer Marshaler
<beans xmlns:jms="http://servicemix.apache.org/jms/1.0" ... > ... <jms:soap-consumer wsdl="classpath:widgets.wsdl" destinationName="widgetQueue" connectionFactory="#connectionFactory" marshaler="#myConsumerMarshaler" /> <bean id="myConsumerMarshaler" class="com.widgetVendor.example.widgetConsumerMarshaler" /> ... </beans>
Note
You can also configure a consumer to use a custom marshaler by adding a child
marshaler
element to the consumer's configuration. The marshaler
element simply wraps the bean
element that configures the marshaler.