Back to course overview
3
Module 3 of 7

Building Mule Flows & Connectors

Understand Mule flows, sub-flows, and error handling scopes. Connect to HTTP endpoints, databases, and Salesforce using the Anypoint Connector Library.

3 lessons~4 hours
1

Anatomy of a Mule Flow

A Mule flow is the fundamental processing unit — a chain of message processors triggered by a source.

Every flow starts with a source (also called a trigger or listener): an HTTP Listener, a Scheduler, a JMS Consumer, etc. When the source fires, it creates a Mule Event containing a payload, attributes, and a set of variables.

The event passes through a sequence of processors: transformers, routers (Choice, Scatter-Gather), scopes (Try, Async), and operation components (connectors). Each processor can read and modify the event.

Sub-flows are re-usable flow fragments with no source. They are invoked with the Flow Reference component and execute synchronously in the same thread as the caller.

Basic HTTP listener flowxml
<flow name="get-orders-flow">
  <http:listener config-ref="HTTP_Listener_config"
                 path="/api/v1/orders"
                 allowedMethods="GET" />

  <logger level="INFO"
          message="Received GET /orders from #[attributes.remoteAddress]" />

  <db:select config-ref="Database_Config">
    <db:sql>SELECT * FROM orders WHERE status = :status</db:sql>
    <db:input-parameters>#[{ status: attributes.queryParams.status default 'active' }]</db:input-parameters>
  </db:select>

  <ee:transform>
    <ee:message>
      <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
payload map (row) -> {
  orderId:  row.ORDER_ID,
  customer: row.CUSTOMER_NAME,
  amount:   row.TOTAL_AMOUNT
}]]></ee:set-payload>
    </ee:message>
  </ee:transform>
</flow>
2

Error Handling Strategies

Proper error handling separates prototype code from production-grade integrations.

Mule 4 uses typed errors in a hierarchy (e.g. `HTTP:CONNECTIVITY`, `DB:QUERY_EXECUTION`). The On Error Propagate scope re-throws the error after executing its processors; On Error Continue swallows it and continues normal flow execution.

A global error handler applies to all flows in an application. Flow-level and Try-scope error handlers override it for specific scenarios.

Best practice: always return a standardised error payload from your global handler so consumers receive consistent error responses regardless of the underlying error type.

Global error handler with standardised responsexml
<error-handler name="global-error-handler">
  <on-error-propagate type="HTTP:CONNECTIVITY, HTTP:TIMEOUT">
    <ee:transform>
      <ee:message>
        <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
  timestamp: now() as String,
  status:    503,
  error:     "UPSTREAM_UNAVAILABLE",
  message:   error.description
}]]></ee:set-payload>
      </ee:message>
      <ee:variables>
        <ee:set-variable variableName="httpStatus">503</ee:set-variable>
      </ee:variables>
    </ee:transform>
  </on-error-propagate>

  <on-error-propagate type="ANY">
    <ee:transform>
      <ee:message>
        <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
  timestamp: now() as String,
  status:    500,
  error:     "INTERNAL_ERROR",
  message:   error.description
}]]></ee:set-payload>
      </ee:message>
      <ee:variables>
        <ee:set-variable variableName="httpStatus">500</ee:set-variable>
      </ee:variables>
    </ee:transform>
  </on-error-propagate>
</error-handler>
3

HTTP, Database & Salesforce Connectors

Anypoint Connectors are pre-built modules that handle authentication, connection pooling, and protocol specifics for you.

The HTTP Connector covers both listener (inbound) and request (outbound) operations. It handles OAuth 2.0, Basic Auth, TLS, and custom headers transparently through a named configuration element.

The Database Connector supports any JDBC-compatible database. Use parameterised queries (`<db:input-parameters>`) — never string concatenation — to prevent SQL injection.

The Salesforce Connector maps to Salesforce REST and SOAP APIs. Common operations include `salesforce:query` (SOQL), `salesforce:upsert`, and `salesforce:subscribe` (Platform Events). Connection uses OAuth 2.0 with JWT bearer token for server-to-server flows.

Salesforce SOQL query examplexml
<salesforce:query config-ref="Salesforce_Config"
                   doc:name="Query Accounts">
  <salesforce:salesforce-query>
    SELECT Id, Name, Industry, AnnualRevenue
    FROM Account
    WHERE Industry = :industry
    AND AnnualRevenue > :minRevenue
  </salesforce:salesforce-query>
  <salesforce:parameters>#[{
    industry:   vars.targetIndustry,
    minRevenue: 1000000
  }]</salesforce:parameters>
</salesforce:query>