Introducing Expression Transformers
November 26th, 2008
The Mule expression framework was one of the new features in Mule 2. It provides a very powerful way to make queries on incoming messages and use the results to control how Mule behaves. Mule already supports a variety of expression languages such as Xpath and Groovy. There is also expressions for navigating the current message headers, payload and attachments.
Expressions in Mule are best understood with an example. I am going to demonstrate using the Expression Transformer which is available in Mule 2.1.
The expression transformer executes one or more expressions on the current message where the result of the expression(s) will become the payload of the current message.
For example, imagine you have a service component with a message signature that accepts three arguments:
Public class ShippingService { public ShippingConfirmation makeShippingRequest(Customer customer, Item[] items, DataHandler supportingDocumentation) { //do stuff } } |
And the message being passed to you component looks like this:
public interface ShippingRequestMessage { public Customer getCustomer(); public Item[] getShippingItems(); //etc } |
The expression-transformer can be used to extract the fields from the ShippingRequestMessage to invoke the ShippingService. Note that we can only get two of the arguments from the ShippingRequestMessage: Customer and Item[]. The supporting documentation, which could be something like a Microsoft Word or Excel document, is an attachment to the ShippingRequestMessage. Attachments can be associated with any message within Mule.
Here we execute three separate expressions to obtain the three arguments required to invoke the ShippingService.makeShippingRequest() method. The first two expressions use the bean evaluator to extract objects from the message. The last argument uses the attachment evaluator to obtain a single attachment. Note that supportDocuments can be null, so we set required=”false” on the return argument.
The bean evaluator evaluates a bean path (defined in the expression attribute) on the current message payload. The attachment evaluator will query the message for a named attachment called supportingDocs.
Once the transformer has returned the 3 arguments, Mule will then invoke the ShippingService with the arguments. Mule works out which service method to call based on the object types of the current message. Easy.
Tags: transformers
December 9th, 2008 at 12:20 pm
Can you provide an example of using expressions??
I’m trying to use expressions with a REST serviceURL and I cannot get the configuration straight.
My config file is here: http://pastebin.com/d45510c3a
I get an orderId from HTTP and use it in a REST URL call.
Thanks for the help.
December 10th, 2008 at 8:27 am
Mule’s test suite is a great source. Check out this config: http://tinyurl.com/5bk89q
January 23rd, 2009 at 4:09 am
Why the following Jxpath is never true?????
…….
The input xml sent via soap http is the following:
hello
Thanks for the help.
January 23rd, 2009 at 10:05 am
Hi dev007,
Thanks for following our blog and having an interest in our products and features. You may want to consider joining the mailing list at http://mulesource.org/display/MULE/Mailing+Lists for ongoing communication.
Cheers,
The Mule Community