Tutorials A to Z » Blog Archives

Tag Archives: Mule 4

Mule 4 Mulesoft Tutorial

Salesforce – Job Info, Batch Info, Batch Result

Published by:

In our previous tutorial “CREATE BULK JOB SALESFORCE CONNECTOR” we covered on creating bulk jobs in salesforce via mule 4. In this tutorial to fetch the details related to the job created, like how many records/batches failed, successful records/batches and its number  or current status of the job; we will be using salesforce connector components provided by Mule.

We will be covering following salesforce connector in Mule 4:

  1. Job Info
  2. Batch Info List
  3. Batch Info
  4. Batch result stream
  5. Batch result


Job Info

Salesforce Job Info connector is used to get the details for a particular job that has been created in salesforce. This operation enables you to track the execution status.


On successful execution of the “job info” below in the output:

Configuration –

Output –


Batch Info List

Salesforce Batch Info List connector get information about all batches in a job.


On successful execution of the “job info” below in the output:

Configuration –

Output –


Batch Info

Salesforce Batch Info connector get information about a particular batch inside a job.


Batch Info Parameter should contain Job Id and Batch Id for which details needs to be fetched.

On successful execution of the “job info” below in the output:

Configuration –
We will be sending JobId and id (batch Id) to Batch Info, to retrieve batch details.

Output –


Batch result

Salesforce Batch result connector get the result of the records processed inside a particular batch.


Batch To Retrieve Parameter should contain Job Id and Batch Id for which details needs to be fetched.

On successful execution of the “job info” below in the output:

Configuration –
We will be sending JobId and id (batch Id) to Batch result, to retrieve batch result.

Output –


Batch Result Stream

Salesforce Batch Result Stream connector get the result of the records processed inside a particular batch. Best used when there are huge records result to be pulled.


Batch To Retrieve Parameter should contain Job Id and Batch Id for which details needs to be fetched.
Streaming Strategy can store data in Memory with “Repeatable In Memory Stream” Config and stores data in file with “Repeatable File Store Stream”

On successful execution of the “job info” below in the output:

Configuration –
We will be sending JobId and id (batch Id) to Batch Result Stream, to retrieve batch result.

Output –


Download Mule Project for this tutorial


Official Mule 4 documentation on Jobs and Batch. Link.
Also refer to Bulk API Guide on Salesforce. Link


Mule 3 Mule 4 Mule Interview Question Mulesoft Basics Mulesoft Tutorial

RAML Interview Questions For Mulesoft Developers Advanced

Published by:

RAML Interview Questions ADVANCED.

Here are the  advanced interview questions and answers about RAML during Mulesoft interviews. This are just a list of some common questions but not the entire list.

1.  How to use RAML in anypoint studio?

RAML can be imported while creating a new application from scratch either from local machine or from Anypoint Design Center which will create flows and error handlers using APIKit module. RAML can also be imported into existing applications by right clicking on the application and  selecting “Anypoint Platform” -> “Import form Design Center” (in Anypoint Studio 7.* and Mule 4.*).

2. What are Traits in RAML?

Traits are functions in RAML which defines common properties for HTTP methods, can be declared once and used at multiple places by keyword “is“. traits should be defined in a separate file and import it into the main RAML to follow Best practices.

3. Explain Request/Response lifecycle in mule based on RAML? 

ApiKit router plays a key role in mapping the resources(RAML) and Mule flows. Any Request entering into mule app through the inbound will hit ApiKit router and the request will be validated according to the RAML description.

Success Requests will be mapped to their corresponding flow and Bad requests will be mapped to their corresponding exception flow and respond back with appropriate HTTP Status code.

4.  What are the parameters defined for methods in RAML?

URI parameters and Query parameters can be defined in RAML.

URI Parameters: Unique Resource Identifier as the name suggests, it should get a unique resource. It is sent as a part of URL and expects unique id each time. They are defined by using keyword “uriParameters”.

Query Parameters: The question mark, the parameter, and its real value make what is referred to as the query string. Query parameters come with two distinguishing features from the hierarchy parameters: They are optional. They are non-unique, in the sense that they can be used to specify any one parameter multiple times. They are defined by using keyword “queryParameters”.

5.Difference between POST and PUT?

POST is used while Inserting or creating a new record in the Database or System of record where as PUT is used while editing or updating an existing record. Both methods will support URI and Query parameters.

Mulesoft Tutorial

Externalizing Error Handling and Flows In Mule 4

Published by:

While building multiple APIs, we might want to export common flows that are used across or global error handling framework that is common for all the APIs. This is helpful incase; any changes to common flow, we need not end up making code changes to all the APIs across.

In this “Externalizing Common Flows In Mule 4” tutorial we will be looking in how we can export common flows/error handling to an external JAR and import them in our APIs.

In the below example we have a flow “commonlogggerFlow” and exception handler “commonExceptionHandler” that is been used across multiple API as is and needs to be externalized.

For externalizing the flows, we need to create a new mule 4 Project and add the flows that we need to externalize in here. In this case we will be adding commonloggerFlow and commonExceptionHandler into new mule 4 project commonFlows.

Once done, then we need to export our new project as Mule 4 Deployable Archive. This will give us a  Mule 4 JAR file that needs to be imported into our projects.
Right click on the Project > Select Export > Select Anypoint Studio to Mule Deployable Archive >  Select path to be exported

Now, in our main project we will import this jar as a maven dependency.
Right click on Project > Mule > Add Maven Dependency > Select the JAR that we have exported.

Above steps will added the jar to local maven repository and also create a dependency in pom.xml

Once we have imported the jar as maven dependency, we need to specify Mule file name present in the JAR in our Mule Project.
Go to Global Elements > Create > Expand Global Configuration > Import

With this configuration we can now refer to common flows or common error handling defined inside the JAR.

Mule 4 Mulesoft Basics Mulesoft Tutorial

Retry Mechanism – Until Success Vs Flow Reference

Published by:

Retry Mechanism – Until Success Vs Flow Reference

In mule 3 we have roll back exception strategy which enable’s the ability to retry the execution in case of error and define a separate flow to be executed once the retry count has exceeded.

In mule 4 you do have re-connection strategy which we can define on the connectors but that only retries in case of failure in connection. In Mule 4 we do not have roll back exception strategy, so in this tutorial we will be looking on how we can implement the same functionality in Mule 4.

To achieve this retry mechanism, we can use Until Successful, but the issue we will face are:

  1. We would not be able to specify any specific condition on which retry should happen . For Example: We will not be able to define retry only when HTTP status code is 202.
  2. We also cannot implement error flow, once an error has occurred. For Example: Every time an error is generated we need to send the error message on to a queue before retrying.

Scenario 1: We want to implement retry mechanism on Web service call, in case of error if HTTP status code is 502 then, API should retry its Web Service Call only 3 times.

To complete the above scenario, we will be using Flow Reference.

Flow Reference in Mule 3 was not able to call its own flow in which it was defined. But in Mule 4 you can call any flow even its own flow.

Flow Diagram:

All we need is to use is flow reference to call its own flow when an error is generated. We have moved HTTP Request to another flow “HTTPFlow” and is referred by flow reference in main flow “get:\users:test-config”.

Inside HTTPFlow we have HTTP Request call on which we have implement retry mechanism. In Error handling part, “On Error Continue” is checking for the retry count if it has reached to its max or not. Inside error flow of “On Error Continue” retry count value is getting incremented and after some seconds of sleep; flow reference will again call HTTPFlow. Once the retry count has reached to its max “On Error Continue” will no longer catch the error and the final error is throw back to its parent flow.

    <flow name="get:\users:test-config">
    <ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd" doc:id="86de922d-7d4d-4d0a-b010-e1cf9e23a79d">
                <ee:set-payload><![CDATA[%dw 2.0
output application/json
  userID: [
  userName: "Varun",
  subject: [
  class: {
    name: "Class 10"
    <logger level="INFO" doc:name="Logger" doc:id="897eb15a-c379-4051-ae78-21ebbbf33cd1" />	
      <set-variable value="1" doc:name="SetRetryCount" doc:id="ae08693c-0c8e-4397-b5e2-235b8b288821" variableName="retryCount" />
    <flow-ref doc:name="HTTPFlow" doc:id="84ab16f4-0fa5-4ac4-a73e-80dd7ab20ea0" name="HTTPFlow"/>
    <logger level="INFO" doc:name="Logger" doc:id="92727a36-d8ed-4ea1-8616-3c0537598400" />
  <flow name="HTTPFlow" doc:id="610bee6d-59f2-4f77-a29e-d60b88aaea01" >
    <logger level="INFO" doc:name="Logger" doc:id="38537854-3f21-48a7-a6a6-31907d8bca90" message="Calling HTTP request count - #[(vars.retryCount default 0)]" />
    <http:request method="GET" doc:name="HTTPCall" doc:id="c766093c-c7ac-444f-914d-cd4d1b70676d" config-ref="HTTP_Request_configuration" path="/abc">
      <reconnect />
    <error-handler >
      <on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="8d23329f-b006-4a56-b6a7-6e33eb748957" when="#[(vars.retryCount as Number default 0) &lt; 3 and error.muleMessage.attributes.StatusCode == 503]">
        <logger level="INFO" doc:name="Logger" doc:id="1be75ffe-a4bf-4fe1-9802-ae1309d76341" message="#[error.description]"/>
        <set-variable value="#[(vars.retryCount default 0) +1]" doc:name="Increment retryCount" doc:id="a9877e1d-d1f5-4786-93e9-58126d08f3f4" variableName="retryCount"/>
        <scripting:execute doc:name="Sleep" doc:id="531bc61a-937d-4a0c-81ce-1ea0685ce64f" engine="groovy">
          <scripting:code >def duration = Long.valueOf('3000');
return message.payload;</scripting:code>
        <flow-ref doc:name="HTTPFlow" doc:id="3f37c302-ec9a-4751-ab4e-dcdefb2607f5" name="HTTPFlow"/>


Scenario 2: Here we want to implement retry mechanism on Web service call when a specific value is received. Example if a web service call returns a value 5 then retry should happen maximum 3 times else not.


We have moved HTTP Request to sub flow “testSub_Flow” and is referred by flow reference in its parent flow “post:\users:application\json:test-config”.

Inside testSub_Flow  we are using flow reference to call itself. Once we have received the response from web service call “Request“, Choice router we are routeing flow processing based on response received and number of retires number.


Mule 4 Mulesoft Basics Mulesoft Tutorial

Parallel For Each in Mule 4

Published by:

The Parallel For-Each scope enables you to process a collection of messages by splitting the collection into parts that are simultaneously processed in separate routes. After all messages are processed, the results are aggregated following the same order they were in before the split, and then the flow continues.

In the below tutorial we will see who we can use parallel for each in you project.

Download Parallel For Each Example


<parallel-foreach doc:name="parallel For Each" collection="payload">
<!-- Code to be Processed parallel -->

Parallel For Each

In this example we will send a JSON message as an array, which will be split by parallel for each and executed in parallel.
Inside parallel for each we are transforming the message received with a delay of 5 sec, so that we can clearly see in logs in our API has processed messages in parallel or not.

<sub-flow name="addUsersParallelForEach" doc:id="5820e110-740b-48aa-baf2-b4f0fa68716a" >
  <logger level="INFO" doc:name="Log Request" doc:id="53992e7f-84cf-4c29-bb74-2be27a2ececf" message="'request received - ' #[payload]"/>
    <parallel-foreach doc:name="parallel For Each" doc:id="81acc47f-7b50-4806-95ea-6e7f24cd6683" collection="payload">
      <ee:transform doc:name="Transform Message" doc:id="751032f3-11f6-4ce3-b136-73e534bd6224" >
        <ee:message >
          <ee:set-payload ><![CDATA[%dw 2.0
import * from dw::Runtime
output application/json
msg : payload.username ++ ' processed' wait 5000]]></ee:set-payload>
        <ee:variables >
      <logger level="INFO" doc:name="for-each output" doc:id="bfe598c4-6b02-4d36-8fa9-00d9cb2a8cce" message="for-each output:  #[payload]"/>
    <set-payload value="#[%dw 2.0
output application/json
payload]" doc:name="Set Payload" doc:id="989fedef-40e2-4e74-87e6-577bebca3b4c" />
    <logger level="INFO" doc:name="Logger" doc:id="fa4eb2fb-a70c-4489-aaff-81eafa03213f" message="#[payload]"/>



Logs: In the logs we can see that the messages are processed in parallel.

Parallel Processing in Batches

In this example we will execute parallel processing but in batches. If we are connecting to an external system (suppose salesforce), and need to send the request in batches of 200 and all the batches should be executed in parallel.
How can we achieve this? is simple, by using divideBy function.

<parallel-foreach doc:name="parallel For Each" doc:id="3641e6b1-e499-4528-b6f6-d9ad7545368e" collection="#[import * from dw::core::Arrays output application/json --- payload divideBy 2]">

Here for example, if we receive 10 records. 10 records will be spit/divided into sets of 2 and 5 jobs will be created that will executed in parallel and processed.

In the below code we are dividing the payload received into set of 2, then transforming the message received with a delay of 5 sec so that we can clearly see in API logs if messages processed in parallel or not.

<sub-flow name="addUsersBatchParallelForEach" doc:id="aedbbefb-d38f-4ee1-a7e3-dc537645da5e" >
  <logger level="INFO" doc:name="Log Request" doc:id="1e9de3c9-cce7-4744-9010-c3b9b2a100ab" message="'request received - ' #[payload]"/>
    <parallel-foreach doc:name="parallel For Each" doc:id="3641e6b1-e499-4528-b6f6-d9ad7545368e" collection="#[import * from dw::core::Arrays output application/json --- payload divideBy 2]">
      <flow-ref doc:name="Flow Reference" doc:id="2bf73bb0-1916-47fd-967d-4cdde18428f3" name="addUsersSub_Flow_BatchParallelForEach"/>
    <set-payload value="#[%dw 2.0
output application/json
flatten (payload.payload)]" doc:name="Set Payload" doc:id="bbee3431-7975-4528-93cf-3955ee4011cc" />
    <logger level="INFO" doc:name="Logger" doc:id="fffae7bf-573d-4b27-9a69-26ecedde5d78" message="#[payload]"/>
  <sub-flow name="addUsersSub_Flow_BatchParallelForEach" doc:id="eb15de26-5035-47ab-8183-4e6efbe49b80">
  <ee:transform doc:name="Transform Message" doc:id="eaaebb0c-539a-4269-8295-2701b5c6397a" >
        <ee:message >
          <ee:set-payload ><![CDATA[%dw 2.0
import * from dw::Runtime
output application/json
(payload map {
  msg : $.username ++ ' processed' 
}) wait 5000 
        <ee:variables >
      <logger level="INFO" doc:name="for-each output" doc:id="198b6135-10e6-4882-bd1d-1686dd3f49fd" message="for-each output:  #[payload]"/>

To get only the message payload received after processing we are using flatten (payload.payload)


Mule 4 Mulesoft Basics Mulesoft Tutorial

Executing Dataweave Dynamically

Published by:

In case we want our Dataweave expression outside mule project, load and process it at runtime then you would need Dynamic Evaluate component.

Download Dynamic Evaluate Project Example

In a scenario wherein dataweave mapping conditions are expected to change frequently based on client’s requirements and you don’t want to redeploy running APIs again and again, in such scenario we can store our dataweave expression in a DB or S3 or other location and access and process it dynamically in our Mule API. Any changes made to this external datawave will be picked up by Mule while reading it from external source and processed.

In the below example we are using variable dynamic_dw to store the datawave expression as a String. In a real world this datawave expression should be coming from an external source like DB or SFTP or others and getting stored in a variable.




<sub-flow name="dynamic-evaluateSub_Flow" doc:id="2145c3c0-5196-418f-9dd3-adf06966cc4a" >
  <set-variable value="#[%dw 2.0 
output application/json 
payload]" doc:name="Store payload" doc:id="18b55a69-28fd-48ae-9344-f80e9be3ffc6" variableName="reqReceived"/>
  <set-variable value="#['%dw 2.0 output application/json --- vars.reqReceived.username']" doc:name="datawave received from external source" doc:id="29aebfba-73e4-41b3-9f3f-e508f98da413" variableName="dynamic_dw"/>
  <logger level="INFO" doc:name="datawave received" doc:id="672895f7-42e8-4c25-8dca-b11bd61634b3" message="#['script - ' ++ vars.dynamic_dw]"/>
  <ee:dynamic-evaluate doc:name="Dynamic Evaluate" doc:id="59d877bb-36ba-4193-bf72-df5083a06d22" expression="#[vars.dynamic_dw]"/>
  <logger level="INFO" doc:name="output" doc:id="fea7a7e5-b8d3-4726-80b8-a846c3794a71" message="#[payload]"/>


Mule 4 Mulesoft Tutorial

Creating MUnits Mule 4

Published by:

In this tutorial, we will be creating Munits for a simple flow that listens over REST HTTP, send the request to salesforce (via a salesforce connector) and returns a JSON Message in response. The response returned, will be asserted with the expected response.

Creating Munits

To build Munits you need to right click API router and select “Create Test Suite for [File Name] from RAML”. This will auto create a basic structure of Munits for you.

The Munits auto created will generally have:

  • All the Munits generated for flows that are mentioned in that RAML or WSDL.
  • Each Munit flow will have “Set Payload” component that will contain the request message that is needed before starting the flow. This request message is auto picked from RAML if example is defined. MunitTools::getResourceAsString reads the file been specified.
  • In Execution section in each Munit flow there will be a flow starter, that will send the request message to a specific flow that needs to be tested out. It can be a flow reference, VM, HTTP “Request” in case of REST service or “Consumer” incase it’s a SOAP service based on the flow to be tested. Since we have build a REST service using RAML; Mule 4 automatically adds an HTTP “Request” component to it. **You might need to configure HTTP Requester or Web Consumer inside Munits so it can call your API’s endpoint.
  • In Validation section; Mule 4 auto adds assertions. One checks for the HTTP status code been returned by the API and other on checks for the final response returned by the Mule flow and compared it with the expected response. The expected response is auto picked by Mule 4 if its already defined in RAML inside response example.


Running Munits

You can go ahead and run Munits by right clicking and selection “Run MUnit Suite”.

You can also run Munits from command prompt, just open your command prompt and go to the project root folder and type “mvn test”. This will run you MUnits from command prompt.

Why To Mock Connectors?

If we are not mocking our connectors, on running munits mule 4 will actually connect post request to the external environment through connectors used in out project. In this project since we are using salesforce connector to connect to salesforce environment, on running munits the flow connects to salesforces environment and post its request there. This can be a problem if we want to deploy our application on Production servers; data can get modified even before Mule APIs is deployed successfully.
Thus, mocking all your connectors, ensures that it doesn’t connect to external environment and uses predefined response every time.

How to Mock?

To Mock a connector, we need to place “Mock When” in Behavior section. And define its configuration.

You can set the processor attribute to define the processor to mock with the connector namespace and operation; and the with-attribute element to define the connector’s attribute name and value so that mule can identify which connector is to be mocked. “Then-return” you can define the message that is to be returned by the connector.

With this much configuration we are done with our MUnits.

Mule 4 Mulesoft Basics Mulesoft Tutorial

Creating Mule 4 Project with RAML

Published by:

Creating Mule 4 Project with RAML

In this Mule tutorial we will learn how to Create Mule 4 project with RAML and a detailed walk-through on how the Mule flow works in case of a success or error scenario:

Mule ESB – What is RAML and why it’s used

RAML stands for RESTful API Modeling Language and is similar to WSDL. A RAML provides a structure to the API which is useful for developers to start there development process and also helps client who is invoking the API to know before hand what the API does.

A RAML contains:

  1. Endpoint URL with its Query parameters and URI parameters,
  2. HTTP methods to which API is listening to (GET, POST, PUT, DELETE),
  3. Request and response schema and sample message,
  4. HTTP response code that an API will return (eg: 200, 400, 404, 500). Continue reading
Mule 4 Mulesoft Basics Mulesoft Tutorial

Inbound Outbound Properties

Published by:

In this “Inbound Outbound Properties” tutorial of Mule 4 we will look on how we can set and modify Mule Inbound and Outbound Properties.

In Mule Inbound properties referees to the additional information that comes to an Mule API along with the message body/payload itself. It may consist of inbound Headers, Query Params, URI Params, HTTP method etc.
In Mule Inbound properties are preset by the sender of the message thus cannot be added or modified.

Mule Outbound Properties are headers and properties that Mule API set before ending its request to other external systems.

Inbound Properties
In Mule 3 we used to access inbound properties by #[message.inboundProperties]

Whereas in Mule 4 we access these properties by #[attributes]

We have create a simple project using RAML.
The GET method of the RAML has URI Param – user_id, which can assess by #[attributes.uriParams['user_id']]

Similarly to access Query Param we do it by #[attributes.queryParams['code']]

To view all the Inbound Properties that are received by a Mule API:


Output :


Outbound Properties
As in Mule 3 we used to set outbound properties via using Set Property Component.
In Mule 4, outbound properties no longer exist. Instead, the headers or properties (e.g. HTTP headers or JMS properties) that you wish to send as part of a request or message (e.g. HTTP request or JMS message) respectively are now configured explicitly as part of the connector operation configuration. 
To Set the outbound HTTP headers and HTTP status code for a Mule API we need to modify the HTTP Listener Configuration.

SoapUI Output –