Thursday, 2 July 2020

Notes: Module 5: Mule 3 Fundamentals: Accessing and Modifying Mule Messages



DataSense is Anypoint Studio’s ability to proactively discover metadata from internal and external resources.

5-1) Setting Message Data

Structure of Mule messages:
- Inbound properties
--- Set from the message source (read-only & persist throughout the flow)
- Outbound properties
--- Added by the message processor (read/write access & can set/remove/copy)
- Payload
--- The core of the message (Java Object)
- Attachments
--- Ancillary info to the message.

Image: Example Inbound Message Properties

Payload representation:
- Raw data
--- E.g.: String, InputStream, Byte[] ...
- Structured data:
--- E.g.: Map, Structured Java Object ...

Setting message properties:
- Set Payload (Transformer)
--- message.payload
- Property (Transformer): Sets/removes/copies properties on the outbound scope of a message
-- message.outboundProperties

Image: Examples Flow

5-2) Debugging Mule applications

Can add breakpoints to processors and step through the application.
By default, Debugger listens for incoming TCP connections on localhost port 6666.

Image: ‘Debug project...’ from the Canvas

Image: Debug As > Mule Application (configure) > Mule Debug... (Tab) > Debugger Port: 6666

Image: Toggle breakpoint

Image: Expression Evaluator box

5-3) Using Expressions to read and write message data

MEL = Mule Expression Language

Note: MEL is case-sensitive.

Basic MEL syntax:
#[] Encapsulates all Mule expressions.
#[message] Holds a context object.
#[message.payload] Dot notation to access fields or methods.

Context objects:
server      Operating system that message processor is running.
mule        The mule instance that the application is running.
application User application the current flow is deployed in.
message     The Mule message that the message processor is processing.

Image: Accessing message data

Image: Accessing message payload data

Image: Accessing relational map data

Image: Operators: +, -, /, *, %, ==, != ...

Testing for emptiness:
The literal empty tests the emptiness of a value.
== empty

Data Extraction:
- XPath: #[xpath(‘expression’)]
- RegEx: #[regex(‘expression’)]

Image: Example: Getting ‘Logger’ to just log http.query.params name

Note: You can only set outbound properties in a flow. So if you use the ‘Property’ element to set something, the logger then needs to read it in outbound properties.

5-4) Creating Variables

Context variables:
flowVars
sessionVars
recordVars

Variable Transformer to set Flow Variable.
Session Variable Transformer to set Session Variable.

flow variables - tied to the message event as it moves through flows: #[flowVars.X]
session variables - also tied to the message event, but they persist across some but not all transport barriers: #[sessionVars.foobar]

Image: Variable persistence

Image: Using flowVars in Logger

“Session variables are typically not used - mainly if you want a variable to persist over a VM queue. Example: It does not persist across HTTP transport.”

No comments:

Post a comment