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.

Assumptions

This document assumes that you are familiar with Mule ESB and its basic concepts. Consider reading the chapter, Anypoint Fundamentals to gain familiarity before proceeding.

Benefits of Using MEL

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.

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)]

    • #[xpath('//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'] ]

As a shortcut, Mule accepts an expression such as #[payload], rather than #[message.payload] because it knows to automatically evaluate the expression for the message context object. This shortcut only applies with the payload field.

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

  1. 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().equals('book')]">
              <jms:outbound-endpoint queue="bookPurchases" />
          </when>
         <when expression="#[payload.getPurchaseType().equals('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.getSubjectLine]" 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 is written in this format: #[evaluator:expression]. Note that a colon separates 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 requires an explicitly specified 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.

Mule continues to support both the older style of expressions (which specify evaluators), and the new style that uses MEL as the default evaluator. Though both are supported, best practice dictates that you use MEL expressions in your application, taking advantage of changes and improvements to syntax as MEL continues to evolve.

Learning MEL

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.

  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. In order to be able to act upon it, you need to be able to figure out what type of data the message contains. Is the payload an array? Does the message contain a flow variable? What inbound properties exist? The Mule Message Tutorial document describes the tools you can use to see inside the message, so that you know how to use MEL expressions to manipulate the data.

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.

See Also