Mule Expression Language (MEL)
MEL is a lightweight, Mule-specific expression language that you can use to access and evaluate the data in the payload, properties and variables of a Mule message. Accessible and usable from within virtually every message processor in Mule, MEL enables you to quickly and elegantly filter, route, or otherwise act upon the different parts of the Mule message object.
This document assumes that you are familiar with Mule ESB and its basic concepts.
Once upon a time, when developing Mule applications, you would have had little other option than to use a scripting language, such as Groovy, or a programming language such as Java, to manipulate the contents of a message object. Though effective, using these languages was not always efficient because there was no consistent, standardized method for evaluating expressions.
Enter MEL, an expression language that provides a consistent, standardized way for developers to work with a Mule message’s payload, properties and variables. It makes use of Mule-specific context objects, it’s easy to use (auto-complete everywhere!) and it behaves well when testing your application. Further, and most importantly, MEL enforces consistency when accessing information on the message: rather than using multiple expression evaluators, you can use MEL, the default expression evaluator, to consistently access and manipulate information.
MEL expressions work within message processors to modify the way the processors act upon the message such as routing or filtering. The example below demonstrates how you can use MEL to log the value of a flow variable at runtime.
<logger message="The value in my first variable is #[flowVars.myVar1]" level="INFO"/>
|An expression language, such as MEL, is similar, but not the same as, a scripting language: it allows you to write short scripts that resolve dynamically to get values, set values, or perform an operation on data. There is an overlap in functionality between an expression language and a scripting language, but scripting languages are generally more useful if you are writing something complex enough to require more than just a few lines of code, or if you need to include conditional logic. If, however, you need to get or set values, invoke methods, or perform functions, you can do so quickly and easily using an expression language.|
Based on information Mule extracts from the message or its environment at runtime, Mule evaluates expressions to complete three types of tasks:
extract information that it can use to process the current message; this can be useful when set inside an expression component or expression transformer:
evaluate conditions using the contents of the current message; this can be extremely useful to filter messages from being processed:
#[payload.age > 21]
#[message.inboundProperties.'locale' == 'en_us']
define a target, other than the message payload, in which to store the result of an expression evaluation; typically, this is helpful when set inside a message enricher
Mule accepts the expression
The following examples illustrate a few ways in which you can use MEL.
Use an expression to extract data in order to decide on a course of action based on the contents, properties, or properties of a message. For example, a router (a.k.a. flow control) can route purchase orders for different types of products to different JMS queues. In the example below, a message carries information about purchases from an online store which can be either a hardcopy book or an mp3 file. The following code classifies the messages into different queues based on the type of purchase.
<choice> <when expression="#[payload.getPurchaseType() == 'book']"> <jms:outbound-endpoint queue="bookPurchases" /> </when> <when expression="#[payload.getPurchaseType() == 'mp3']"> <jms:outbound-endpoint queue="songPurchases" /> </when> </choice>
Use an expression to extract a value from the payload, properties, or context of a message and its variables or attachments. For example, a connector might extract a specific piece of information from the the current message to use as an argument. In the example below, expressions extract information from a message and pass each piece to the SMTP endpoint.
<smtp:outbound-endpoint from="#[flowVars.from]" to="#[flowVars.to]" subject="#[payload.subjectLine]" responseTimeout="10000" doc:name="SMTP"/>
Use an expression to extract information, then use it to replace a token with an actual value. For example, a logger can extract information from the payload contents and place it into a logging message, as shown below.
<logger message="#[payload]" />
If the payload is an object and you can call a method to extract information, the expression to extract may appear as follows.
<logger message="#[payload.getLoggingInformation()]" />
It’s important to know that MEL has not always been the de facto expression language Mule uses. Prior to version 3.3.0, expressions in Mule varied in syntax according to their specific evaluator. Thus, although these expressions supported a wide variety of functionality, the variation in syntax rules was time-consuming to learn. This older style of expression was written in the format:
#[evaluator:expression]. A colon separated the evaluator from the expression itself.
Mule 3.3.0 introduced Mule Expression Language (MEL), which implements a single set of syntax rules that no longer contains an evaluator. The evaluator, by default, is MEL itself, and it can interpret expressions formerly associated with dozens of different evaluators. MEL expressions are written according to the following format:
#[expression]. Note the absence of the colon, which signals to Mule that this is a MEL expression; thus Mule evaluates the expression using MEL logic rather than the rules of a specific evaluator.
Note: Only use MEL expressions in your application.
If you’re already familiar with Java, learning MEL will likely not be difficult. That said, it’s important to comprehend some Mule-specific details before you learn how to apply MEL expressions in your application.
Understand the Mule message structure. Because you use MEL to act upon the contents of a Mule message object (payload, properties and variables), you first need to understand the basic structure of the message. If you are not already familiar with it, read about the Mule message structure.
Understand how to see the contents of the Mule message. In order to be able to act upon it, you need to recognize the type of data the message contains. Is the payload an array? Does the message contain a flow variable? What inbound properties exist?
After having absorbed this material, you will be ready and able to begin learning about MEL basic syntax, and start using expressions in your application.