Writing a Mule Transformer

December 3 2008


Transformers in Mule are simple objects that convert the current message from one type to another. The interface for a transformer is simple, but there are some tips and tricks for getting the most out of . For this post we will define a transformer the converts from an Order object to HTML so that we can email the details of an order to a customer.

First, there are two abstract transformer classes you can extend.

The AbstractTransformer gives you access to the source payload to transform and the encoding to use (if required).

The AbstractMessageAwareTransformer gives you access to the current message and the encoding to use. The advantage of this transformer is that you can transform the message headers and attachments too. If you do change the headers or attachments you can change them directly on the message passed in, you always return the transformed message payload from the transform() method.

Registering Source and Return Types

Mule transformers allow the developer to specify which source types the transformer can accept and what type it will return. This allows Mule to validate the incoming message before invoking the transformer and validate the output before sending the message on. Also, Mule supports transformation discovery (automatic transformations) and this requires the source and return types to be set. I will write more about this in another post.

For example, using our Order bean to HTML transformer, you would specify in the constructor that the transformer only know how to convert message payloads of type Order:


This means I don’t need to do any type checking in the transform() method:

You can add more than one source type, in which case you need to check for each type in the transform() method.

Notice also that we set the name of the transformer. Usually, this is done from the Xml configuration when the transformer is declared. If no name is set Mule will generate one based on the first source type and return class; in this case the generated name would be “OrderToString”.

Full Implementation

We are extending the AbstractMessageAwareTransformer, this means we have access to the message headers and can use them in our transformer. In our example, we grab the transactionId from the message header and we use the encoding in the output HTML.

We'd love to hear your opinion on this post

3 Responses to “Writing a Mule Transformer”

  1. […] week I posted about Writing Mule Transformers, this week I’m going to continue with some more advanced features users can take advantage […]

  2. Helpful suggestions ! I Appreciate the details ! Does anyone know if I might get a blank a form document to edit ?