Module 6: Accessing and Modifying Mule Events

  • Home /
  • Duras /
  • Module 6: Accessing and modifying Mule events

About

1.

Description

  • Log event data
  • Debug Mule applications
  • Read and write event properties
  • Write expressions with the DataWeave expression language
  • Create variables

Notes

Intro

Debugging is an essential part of development

Anypoint Studio includes an integrated debugger

At the end of this module, you should be able to

  • Log event data
  • Debug Mule applications
  • Read and write event properties
  • Write expressions with the DataWeave expression language
  • Create variables

Check in

Viewing information about Mule 4 events

Topic video


Mule 4 Event Structure

  • The mule event is an object structure that is generated at the source and passed between the processors
  • The mule message is populated with data and metadata that is passed by the application trigger
  • The variables are created and processed by processors within the flow

    • If the mule application is triggered by an HTTP Request, for example, the attributes will be populated by the HTTP Request attributes including headers, query parameters, uri parameters, and message
    • If the message is POST, PUT or PATCH, the payload will be populated with the body of the HTTP Request
  • Viewing event data

    • At design time in Anypoint Studio

      • At design time in Anypoint Studio using DataSense

        • In the Transform Message component
        • In the DataSense Explorer
        • When writing expressions using auto-completion
      • DataSense is the ability to proactively discover metadata from internal and external resources

        • Keeps you from having to manually discover info about the data
        • Facilitates transformations by providing DataWeave expected input or output
      • DataSense Notification and Indicator

        • If DataSense is timing out, a notification will appear
        • A red dot DataSense indicator will also appear in the lower-left corner
        • Clicking on this indicator allows for checking on the DataSense process and restarting it if necessary
    • At run time

      • In the client when making a request
      • For deployed applications, in the log files
      • In the Anypoint Studio console by using a Logger
      • In Anypoint Studio using the Visual Debugger

        • Most comprehensive way
    • View event data by logging it

      • Add a Logger component to a flow and view its output
      • Logged values are displayed

        • For an application run from Anypoint Studio with embedded runtime, in the Console view
        • For applications deployed to CloudHub or customer-hosted runtimes, in the log files
    • Walkthrough 1-1: View event data

      • Create a new Mule project with an HTTP Listener and set the event payload
      • View event data in the DataSense Explorer
      • Use a Logger to view event data in the Anypoint Studio console

Extras

  • The Mule Event Structure Explained | Lightboard Series


    Mule Event Structure Lightboard

    • Mule Event

      • Message

        • Attributes (everything fed across HTTP)

          • Query parameters
          • Headers
          • Parts of the URI
          • Method
        • Payload (HTTP Body)

          • If the method has a body e.g. POST, PUT, and PATCH, the contents will be in the payload
      • Variables

        • Snippets of data or variables
    • This is set by the event source e.g. the Listener based on the nature of the protocol or whatever is triggering the flow

Debugging Mule applications

Topic video

  • Debugging applications with the Mule Debugger

    • Can add breakpoints to processors and step through the application

      • Watch event properties and values
      • Watch and evaluate DataWeave expressions
      • Can be managed from the breakpoints list
    • By default, Debugger listens for incoming TCP connections on localhost port 6666

      • Can change this in a project’s run configuration
    • Walkthrough 1-2: Debug a Mule application

      • Locate the port used by the Mule Debugger
      • Add a breakpoint, debug an application, and step through the code
      • Use the Mule Debugger to view event properties
      • Pass query parameters to a request and locate them in the Debugger
      • Increase the request timeout for Advanced REST Client

Tracking event data as it moves in and out of Mule applications

Topic video

  • Changes to the event object

    • We examined changes to an event as it moved through a flow and we set the payload
    • What happens to the event object when calls are made to an external resource from a flow?

      • For example, you call a web service, and get return data?
  • Walkthrough 1-3: Track event data as it moves in and out of a Mule application

    • Create a second flow with an HTTP Listener
    • Make an HTTP request from the first flow to the new HTTP Listener
    • View the event data as it moves through both flows

Walkthrough

  • I observed that the variables, and message (payload+attributes) values are not passed to a calling flow when an HTTP Request is used
  • However, when the event is passed back to the parent flow, the variables will be available and the payload will have been changed, but the attributes won’t be there output => Goodbye and myVar

Setting request and response data

Topic video

  • Setting the event payload

    • Use the Set Payload transformer to set or modify the payload
  • Setting data returned from HTTP listeners

    • Set in the HTTP Listeners properties view > Responses
    • When a flow uses an HTTP Listener, it begins processing when it receives an HTTP request, and it completes the process by sending an HTTP response
    • By default a status code of 200 is included in the success response, and a body populated by the payload
    • The response can be modified by editing the body, adding custom headers, and customising the HTTP status code and reason for it
  • Setting data sent to HTTP requests

    • Set in the HTTP Request properties view > General
    • When making an out call, you can set what to send e.g. headers, queryParameters, and URI parameters
  • Walkthrough 1-4: Set request and response data

    • View the default setting for a response body
    • Set a response header
    • View the default setting for a request body
    • Set a request query parameter

Using DataWeave expressions to read and write event data

Topic video

  • The DataWeave expression language

    • A Mule-specific expression and transformation language
    • Can be used to access and evaluate the data in the payload, attributes and variables of a Mule event
    • Accessible and usable from all event processors and global elements

      • Is used to modify the way the processors act upon the event such as routing
    • Case-sensitive
    • Easy to use with auto-complete everywhere
  • Types of DataWeave expressions

    • Standalone scripts

      • Can be generated using the Transform Message graphical editor
    • Inline expressions

      • Are used to dynamically set the value of properties in an event processor or global configuration element
      • Are enclosed in #[]

        • Many places in the product offer an expression mode button
    • Referencing Mule objects in DataWeave expressions


      Referencing Mule objects in DataWeave expressions

  • Accessing event attributes


    Accessing event attributes

  • Using selectors in DataWeave expressions


    Using selectors in DataWeave

  • Using operators in DataWeave expressions

    Using operators in DataWeave expressions

  • Using conditional logic statements in DataWeave

    • Use if/ else if/ else statements


      Use if else if/ else statements/

  • Using DataWeave functions

    • Functions are packaged in modules
    • Functions in the Core module are imported automatically into DataWeave scripts
  • Walkthrough 1-5: Get and set event data using DataWeave expressions

    • Use expressions to set the payload and a logged value
    • Use expressions to set a response header and a request query param
    • In expressions, reference values for the event payload and attributes
    • Use the DataWeave upper() function, the concatenation, as, and default operators

Walkthrough steps

  • Query params disappear as soon as the execution returns to the main flow even when passed into a calling flow via HTTP Request

Creating variables

Topic video

  • Creating variables

    • Create variables to store metadata for the Mule event
    • Use the Set Variable transformer to create a variable
    • In expressions, reference as vars #[var.foo]
  • Walkthrough 1-6: Set and get variables

    • Use a Set Variable transformer to create a variable
    • Reference a variable in a DataWeave expression
    • Use a variable to dynamically set a response header
    • Use the Mule Debugger to see the value of a variable
    • Track variables across a transport boundary

Summary

The best way to view event data is to add breakpoints to a flow and use the Mule Debugger

Use the Logger component to display data in the console

Use the Set Payload transformer to set the payload

Use the properties view to set response data for an HTTP Listener and request data for an HTTP Request operation

Use the DataWeave language to write inline expression in #[]

Use the Set Variable transformer to create variables

Test your knowledge

Q1 A Mule application has a flow named parentFlow. The parentFlow contains an HTTP Request operation at the end of the flow. The parentFlow also contains a Set Variable operation right before the HTTP Request operation. What is the scope of the variable to the server receiving the HTTP Request from parentFlow?

  • The variable is NOT accessible in the server

Q2 A Set Variable component saves the current payload to a variable with the name images. What is the DataWeave expression to access the images variable?

  • #[vars.images]

Q3 A flow contains an HTTP Listener as the event source. What is the DataWeave expression to log the Content-Type header using a Logger component?

  • #['Content Type:' ++ attributes.headers.'content-type']

Q4 A Mule application has two flows named parentFlow and childFlow. The childFlow begins with an HTTP Listener. A variable is defined in parentFlow, then an HTTP Request is made to childFlow’s HTTP Listener with some headers set. What is thet scope of the variable and attributes in the parentFlow after childFlow returns a response?

  • The variable is accessible. All the attributes passed to childFlow are removed or replaced.

Q5. Refere to the exhibit. What is the correct DataWeave expression for accessing the city Cleveland from the JSON payload?

  • #[payload[1].city]

Q6. What happens to the attributes of a Mule event in a flow after an outbound HTTP Request is made?

  • Attributes are replaced with new attributes from the HTTP Request response (which might be null)

Q7. What is NOT part of a Mule 4 event?

  • outboundProperties

Q8. A web service implements an API to handle requests to http://acme.com/customers/%7Bstate }. A web client makes a request to this API implementation at http://acme/customers/CA . What is the correct DataWeave expression to retrieve the value CA?

  • #[attributes.uriParams.state]

Check out

Apply your knowledge: DIY

Anki

References


  1. . “Module 6: Accessing and modifying Mule events”. Available at: . (Accessed: [2025-03-09 Sun 20:34]). ↩︎

Random Posts