LibraryLink ToToggle FramesPrintFeedback

Consumer Marshalers

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:

When the default consumer marshaler do not suffice for you application you can provide a custom implementation of the JmsConsumerMarshaler interface.

To create a custom consumer marshaler you implement the org.apache.servicemix.jms.endpoints.JmsConsumerMarshaler interface. The JmsConsumerMarshaler interface is shown in Example 6.1. It has five methods that need implementing:


createContext()

The createContext() method takes the JMS message and returns an object that implements the JmsContext 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 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;
        }
    }

}

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 shows configuration for a consumer that uses a custom marshaler.


[Note]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.