Reading Time: 4 minutes

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.

latest report
Learn why we are the Leaders in API management and