Mule Imp Notes
Mule Imp Notes
Application Network - network of applications, data, and devices connected with API's to make
them pluggable and to create reusable services.
Mule - an Application that processes API's across an ESB.
ESB - Enterprise Service Bus
Anypoint Platform - Web interface for designing, creating, and managing Mule applications
and API's.
Anypoint Studio - IDE for designing, building, creating, and managing Mule Applications.
API-LED CONNECTIVITY
● From the bottom -> up beginning with:
● System API's - SAP customers, Salesforce customers, UPS shipments... (Central I.T.) -
unlock assets and decentralize access.
● Process API's - Order history, Order status, shipment status... (LOB - Line of Business
I.T.) - discover, reuse assets and compose information
● Experience API's - Mobile API, Web app API... (Developers) - discover, self-serve, reuse
and consume.
API Term Usage
● Web service API (definition of specification)
● Web service interface implementing the API
● Web service implementation itself
● Web service proxy to control access to the API
Web Service - A method of communication that allows two software systems to exchange data
over the Internet.
● Defined rules of communication
● How data is requested
● What parameters are required
● The structure of the data returned
Types of API's
● RESTful - The more up-to-date and simpler to use HTTP communication protocols.
● SOAP - communication rules are defined in an XML-based WSDL (Web Services
Description Language) file.
C4E - Center for enablement is a cross functional team:
● Central I.T.
● LOB - Line of Business
● Developers / Innovation teams
RESTful - Representational State Transfer - An architectural style where clients and servers
exchange representations of resources using standard HTTP.
Other systems interact with the web service using HTTP. The HTTP request method indicates
which operation should be performed on the object identified by the URL.
MuleSoft Blueprint:
Anypoint Platform: Exchange
● Library of assets
● Central repository
● Mulesoft public assets
● Private assets
REST API's - added to Exchange gets an API portal.
API portal - API documentation auto generated.
API console - for consuming and testing API's.
API endpoints - auto generated, use a mocking service for testing API's.
API portals - internal and external users.
RAML 1.0
API Specification - add to Exchange to get a connector created.
Connector - used in Mule applications to make calls to API conversion by REST Connect.
Anypoint Platform: Design Center
Web app for building Flow Designs and API Designs.
Create a Mule Application - create new or import. (Flow Designer)
Create an API Specification - interface for designing, documenting, and testing API
specifications. Create api specification or create fragments. (API Designer)
Mule applications are deployed to a Mule runtime.
● Cloud hosted
● On-Premises hosted
Lightweight Java based ESB
● Connect apps to exchange data
● Connect systems
○ JMS, JDBC, HTTP
Mule ESB becomes the transit system, the bus that drives all messages to their destination
converting and translating the data to the required format.
Anypoint Platform: API Manager
● Manage API's
● Create proxy applications
● Deploy proxies to an API gateway runtime Throttling, security, and other policies Tiers
with different types of access Approve, reject, or revoke access to API's
● Promote managed API
● Define SLA tiers
● Apply runtime policies
● API Gateway enforces the policies
Policies
● Rate limiting
● Spike control
● security
● SLA tiers defines the number of requests that can be made per time frame to an API. Set
to automatic (free) or manual (for tiers that cost $)
● SLA based rate limiting policies require all applications that consume the API to...
Register and pass their client credentials
● Add client ID enforcement to the API spec so the REST connector can enforce the
authentication
CloudHub Worker
● 1 application per CloudHub worker Dedicated instance of Mule that runs an app
● Runs in a separate container from other applications
● Deployed and monitored independent
● Run on a cloud specific to a region in the world
● Different memory capacity and processing power
● Different memory capacity and processing power
○ scaled vertically by changing worker size
○ scaled horizontally by adding multiple workers
Worker Environments
● Design environment (Flow Designer apps only), sandbox, production
Anypoint Studio: Mule Applications
Mule receives events, processes them, and routes them to other endpoints.
Mule Event Source - initiates the execution of the Flow.
Mule Event Processors - transform, filter, enrich, and process the event and it's message
plugged together with a source in a Flow:
● source->processor(s)->connector endpoint
Mule runtime - hosted by MuleSoft called CloudHub worker.
Mule Applications: Mule Events
A Mule event consist of:
● A Mule message:
○ Attributes
● Payload
● Variables
Mule applications receive, process and route events to other endpoints through a series of
event processors plugged together in a Flow using:
● A Mule Event Source - initiates execution of the flow.
● A Mule Event Processor(s) - that transforms, filters, enriches, and processes the event
and its message.
DataWeave
DataWeave 2.0 - Mule 4 (2017) - transformation expression language for Mule to access, query,
and transform Mule 4 event data
DW 1.0 - Mule 3.7 (2015)
M.E.L. - Mule Expression Language (early)
DataWeave: Graphical editor
● Transform message in the Flows
● Drag and drop
● Script editors
● Input
● Output
● Preview
DataWeave Expressions: Script editor
Standalone scripts:
● auto-generated when using the graphical editor
● Can be written in manually in code editor
Inline expressions
● Dynamically set the value of properties in an event processor or global configuration
element
● are enclosed in #[]
● offers an expression mode button
DataWeave selectors:
● Single Value selector - #[payload.name], #[attributes.queryParams]
● Array index selector - #[payload[0].name]
● Array multi value selector (key:value pairs) - #[payload.*name]
● Array descendants selector - #[payload..zip]
DataWeave Expressions: Script editor
DataWeave operators
● Arithmetic (+, -, /, *) #[payload.age * 2] Equality (==,=, ~=) #[payload.name == "max]
● Relational (>, >=, <, <=, is) #[payload.age > 30]
● Conditional (and, or, not) #[(payload.name == "max") and (payload.age > 30)]
● Type coercison (as) #[(payload.age as Number) * 2]
● Default value default (default) #[payload.type default "student"]
DataWeave conditional logic -
if /else if/else statements
● if (payload.age < 15) group: "child"
else if (payload.age < 25)
group: "youth"
else if (payload.age < 65)
group: "adult"
Else
group: "senior"
DataWeave Expressions
DataWeave functions -
● Packaged in modules
● function reference
○ https://docs.mulesoft.com/mule4-user-guide/v/4.1/dataweave
● core dw functions examples:
○ Contains
○ daysBetween
○ distinctBy
○ ends With
○ isLeapYear
○ isOdd
○ orderBy
○ Sum
○ Sqrt
○ Write
○ Upper
● functions with 2 parameters
○ #[contains(payload,ax)]
○ #[payload contains "max"]
Anypoint Studio: Set Variable
● Store metadata for the Mule event
● Set Variable is a transformer
● Persist through all Flows unless the event cross a transport boundary
● Expression referenced as vars - #[vars.foo]
Flows -
● Error Handling strategy
● No event source considered private
Subflows -
● Error handling comes from the parent Flow
● Executed exactly as if the processors were still in the calling Flow
Modules are extensions to the Mule runtime used when building Mule apps:
● HTTP, Database, Salesforce, SAP, Slack
REST CONNECT - A tool that generates connectors for RESTful APIs, RAML 1.0 or OAS API
specifications (Mule 3 and 4)
● Anypoint Studio
● Anypoint Platform Flow Designer
Connectors pre-installed in Studio - HTTP, Database, Salesforce, Validation, Java, search for
others in "Search in Exchange".
Consume RESTful Web Service not in Exchange:
● Add a HTTP connector
● Configure its Request operation
○ operation and/or global element configuration
● Specify headers, query parameters, or URI parameters to pass to the call
Consume RESTful Web Service with API and connector in Exchange:
● create flow to call RESTful web service
● Add REST connector from Exchange
● Configure REST connector
● Set headers, query parameters, uri parameters
Consume SOAP Web Service:
● Add Web Service Consumer connector Module
● Configure global element, location of WSDL
● Use Consume operation
● SOAP operation to invoke
● Use Transform message to pass arguments to the SOAP
● Transform before Consume, DataSense creates metadata for input that includes the
arguments
Combining data from multiple services:
● Canonical
● standard
Anypoint Studio: Controlling Event Flow
Routing Events send events to one or more groups of event processors(routes):
● Choice - one route executed based on conditional logic
● First Successful - sequentially until one is successfully executed
● Round Robin - One route executed, which one is selected by iterating through a list
maintained across executions
● Scatter-Gather - All routes executed concurrently and returns a collection of all results
● Collection is an object of objects containing attributes and payload from each Mule event
returned from a flow
● Scatter-Gather then use DataWeave to flatten multiple collections into one collection
Anypooint Studio: Control Event Flow
● Choice Router: Route based on conditional logic written with DataWeave to set router
paths.
● Validation Events: Test that some conditions are met and throw an error if the validation
fails:
○ Add validation module to project
○ Select validation operation (Is true)
○ Return custom error messages if condition is not true or met
Anypoint Studio: Handling Errors
When an event in a Mule flow throws an error:
● Normal flow execution stops
● Event passed to 1st processor in error handler
Mule Default Error Handlers are used when an error handler in the application is not explicitly
defined:
● Implicitly and globally handles messaging errors
● Stops execution and logs information
● cannot be configured
Error Object is created and contains two properties:
● error.description - a string
● error. errorType - an object
● identified by a namespace and an identifier
○ HTTP:UNAUTHORIZED
○ HTTP:CONNECTIVITY
○ HTTP:INVALID_BOOLEAN - (NAMESPACE:IDENTIFIER)
Error type hierarchy:
Any Errors
General - can be handled
● Unknown
● Transformation
● Routing
● Connectivity
○ RETRY EXHAUSTED
● Expression Security
○ CLIENT_SECURITY
○ SERVER_SECURITY
Critical Errors - Cannot be handled
● Fatal
● Overload
Error Handling Scopes:
On Error Continue
1. All processors in the error handling scope are executed
2. At the end of the scope
a. the rest of the flow that threw the error is not executed
b. Event passed up to the next level as if flow execution completed successfully 3
HTTP Listener returns a successful response
On Error Propagate
1. All processors in the error handling scope are executed
2. At the end of the scope
a. Flow is not executed
b. Error rethrown up to the next level and handled there
3. HTTP Listener returns an error response
Error Handling Scenarios
1. Default - Error occurs
a. Mule default error handler executed
b. Error gets rethrown
c. Error response returned
2. On Error Continue Scope - error occurs
a. On Error Continue scope executed
b. Subsequent processors NOT executed
c. Success response returned
3. On Error Propagate Scope - error occurs
a. On Error Propagate scope executed
b. Subsequent processors NOT executed
c. Error gets rethrown
d. Error response returned
4. On Error Propagate Scope - child Flow error occurs
a. O-E-P scope executed
b. Subsequent processors NOT executed
c. Error gets thrown up to calling flow
d. On Error Propagate Scope calling Flow
e. O-E-P scope executed
f. Error gets rethrown
g. Subsequent processors NOT executed
h. Error response returned
5. On Error Propagate Scope - child Flow error occurs
a. O-E-P scope executed
b. Subsequent processors NOT executed
c. Error gets rethrown up to calling flow On Error Continue Scope calling Flow
d. O-E-C scope executed
e. Subsequent processors NOT executed
f. Success response returned
6. On Error Continue Scope - childFlow error occurs
a. O-E-C scope executed
b. Subsequent processors NOT executed
c. Success response returned On Error Continue or On Error Propagate Scope
calling Flow
d. Because Success response was received No Error Handler is called in calling
Flow
e. Subsequent processors are executed
f. Success or error response returned
Application Error Handling - Add error handler outside a flow or typically placed in the global
configuration file and specify the application's default error handler.
Flow Level Error Handling - All flows except subflows can have error handlers, any number of
error scopes can be added to a flow's error handler.
● Flow w/ error handler - 1st error scope evaluated to true, if no scope is true Mule Default
handles the error propagating the error up the execution chain, where there may or may
not be any error handlers.
● Flow wo/ error handler - error is handled by the Application's Global Default handler who
1st evaluates to true, if not the case, the Mule Default handles the error.
Event Processor Error Handling - Try scope provides more specific error handling, unlimited
number of processors can be added to an error handler scope and…
Specify scope execution for specific error types -
● Any - default
● Types - WSC:BAD_REQUEST
● WSC:BAD_RESPONSE
● WSC:CONNECTIVITY
● WSC:INVALID_WSDL
Specify scope execution for a specific error condition -
Set the when condition to a Boolean DataWeave expression i.e.
● HTTP:UNAUTHORIZED
○ error.errorType.namespace == 'HTTP'
○ error.errorType.identifier == 'UNAUTHORIZED'
○ error.cause.message contains 'request unauthorized'
○ error.cause.class contains 'http'
DataWeave: Errors
● Scripting Errors - problem with syntax
● Formatting Errors - problem with the written transformation
● application/dw is a good way to test errors
● Comments - // single line, /**multi-line*/
DataWeave: Writing Transformations