Mule Expression Language (MEL) in Mule3
    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.
  • Benefits
    • MEL is an expression language that provides a consistent, standardised 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.
  • Use of MEL
  • 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]

  • How to learn 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.
    • 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.
      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?

  • MEL Basic Syntax
  • Essentially, you use MEL expressions in combination with other things such as operands and operators to dynamically return a value.
    Mule evaluates the expression at runtime, and the message processor within which the MEL expression resides uses the result to act upon the message payload, properties or variables. 
  • STUDIO Visual Editor

  • XML Editor or Standalone

  • Context Objects, Variables, and Fields
    • The term Context Object forms the first part of the simplest form of a MEL expression
    • The second part is the Field.
    • Use a Variable in a MEL expression to access information contained within a Flow Variable or Session Variable on your Mule message..
    Context Objects Field Description
    server dateTime
    env
    fileSeparator
    host
    ip
    locale
    javaVersion
    javaVendor
    nanoSeconds
    osName
    osArch
    osVersion
    systemProperties
    timeZone
    tmpDir
    userName
    userHome
    userDir
    The operating system on which the message processor is running.
    mule clusterId
    home
    nodeId
    version
    The Mule instance on which the application is running.
    application encoding
    name
    standalone
    workDir
    registry
    The user application within which the current flow is deployed.
    message id
    rootId
    correlationId
    correlationSequence
    correlationGroupSize
    replyTo
    dataType
    payload
    inboundProperties
    inboundAttachments
    outboundProperties
    outboundAttachments
    The package (payload, attachments, properties) that the message processor is processing.
    Variables Field Description
    flowVars N/A A flow variable set on the message; flow variables persist only within the flow in which they were created.
    sessionVars N/A A session variable set on the message; session variables persist across flows within an application.
  • Basic Syntax Rules
  • For a full list of syntax rules, see full MEL reference material.
    Example Description
    #[ ] #[message.id] Always bounds an expression.
    Simple expressions [message.field]
    [sessionVars.age]
    The simplest type of expression, these consist of just a context object and a field, or simply a variable.
    Multi line expressions #[calendar = Calendar.getInstance(); message.payload = ``new org.mule.el.datetime.DateTime(calendar);] You can include several lines in a single expression, each must end with a ;
    Operators #['Cookie' + flowVars.cookie] Performs operations in expressions. Can be unary, comparison, logical, bitwise, arithmetic, and more.
    Boolean expressions #['foo'=='bar']
    #[2 + 2 == 4]
    Produces Boolean values.
    Bean Property Access #[payload.property1.property2] Access information from bean.
    Method invocations #[message.header.get()] Calls a method, then performs it on an object according to the parameter (if any) specified within the parentheses. Method calls always follow the syntax: object.method().
    Assignments #[payload = 'sample'] Evaluates to assign a value. The example at left resolves dynamically to set the payload to sample.
    Literals 'expression'
    255
    null
    Strings, numbers, Boolean values, types, and nulls.
    xpath and regex xpath3('/orders/order[0]') xpath3 and regex provide ways of extracting context information that doesn’t already exist as a single value.
    Wildcards wildcard("Hello*") Matches a value (the message palyoad, by default) against a wildcard pattern, these use the metacharacters '?' to represent any single character and '*' for a repetition of any character. It’s case sensitive by default.
  • Examples
  • As the following table of examples demonstrates, the values that MEL expressions return can be numerical values, logical values (true or false), strings, or virtually any data type. 
    MEL expressions can also perform operations, invoke methods, and execute functions.
    Access full examples that illustrate how to use MEL expressions in applications.
    Expression Description
    #[2 + 2] This expression evaluates to 4.
    #[2 + 2 == 4] This expression uses an operator to perform a comparison. It evaluates to true.
    #[message] This expression references a context object in MEL (message, app, mule, and server). The value of this expression is the message.
    #[message.id] This expression accesses the id field of the message context object. The value of this expression is the unique message id that Mule automatically assigns to the message.
    #[payload.firstname] This expression accesses an object within the field (payload) associated with the context object (message). If the object is a map item whose key is 'firstname' then this expression evaluates to the value associated with the key 'firstname'. If the object is a bean, the property will be returned.
    #[payload[4]] Same as above, but in this case – provided the field is a list – the expression returns the value of the 5th item in the list.
    #[message.header.get()] This expression calls the "get" method and performs it on the object, according to the parameter (if any) specified within the parentheses.
  • MEL Auto-Complete
  • If you are configuring a field that supports expressions and need help with syntax, you can access MEL suggestions by one of two methods.
    • place your cursor inside the brackets in a field that has #[] pre-populated for you, then press Ctrl + Space Bar.
    • enter #[ to open a new MEL expression and display suggestions, as shown below.
If you got some knowledge about MEL, please follow with me in next topic to understand about Difference between Flow, Sub Flow and Private Flow.

Comments