Contact Us 1-800-596-4880

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.

Prerequisites

This document assumes that you are familiar with Mule and its basic concepts.

Benefits of Using MEL

Use MEL instead of a scripting language such as Groovy, or a programming language such as Java, to manipulate the contents of a message object. Using MEL makes your application more consistent, and provides an efficient and standardized method for evaluating expressions in Mule applications.

  • MEL is an expression language that provides a consistent, standardized way for developers to work with a Mule message’s payload, properties and variables.

  • MEL makes use of Mule-specific context objects, you can code it with the help of auto-complete

  • Great tool for evaluating expressions in your flows

  • Most importantly, MEL enforces consistency when accessing information in the Mule 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.

Using MEL

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:

    • #[payload]

    • #[message.inboundProperties.'propertyName']

    • #[payload.methodCall(parameters)]

    • #[xpath3('//root/element1')]

  • 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

    • #[flowVars.output]

Shortcut for payload

Mule accepts the expression #[payload] as a shortcut for #[message.payload]. This shortcut only applies to the payload field.

The following examples illustrate a few ways in which you can use MEL.

  1. Use an expression to extract data 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>
  2. 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"/>
  3. 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()]" />

Expressions in Mule

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.

Learning MEL

If you’re already familiar with Java, learning MEL is not difficult. That said, it’s important to comprehend some Mule-specific details before you learn how to apply MEL expressions in your application.

  1. 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.

  2. Understand how to see the contents of the Mule message. To be able to act upon it, you need 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 are ready to begin learning about MEL basic syntax, and start using expressions in your application.

See Also