Thursday, 7 October 2021

How to quick start your Mulesoft professional journey

Mulesoft is leading and Fast growing Application integration framework in the industry.

Do you want to start professional journey in Mulesoft in very less time at faster pace , you need to adopt below 5 tips

  1. Find good trainter and mentor to provide right direction and resources, it makes your journey easy and saves lot of time.
  2. Go thorugh mulesoft documentation on topic discussed while training and resolve doubts on regular basis
  3. Practice is the MOST IMPORTANT activity, you will struck a lot in initial days, once you pass the initial hurdles your journey would be smooth. Replicate the same example trainer explained in the session. play around it with different configuration changes and note the observations.
  4. Use social media efficiently, join in all whatsapp, Facebook and telegram groups, there are numerous youtube channels available, watch the practice the concepts you learned in training sessions
  5. Have positive mindset, motivate yourselves when you not progressing, allocate dedicated time and bounce back soon. check job portals to get know the industry requirements.

Tuesday, 14 January 2020

How Dataweave operators helped Arun to pickup right match

Dataweave transformations

Contents: 
1) Iterating payload using Map Operator 
2) Iterating Payload using MapObject Operator
3) Processing payload using Pluck Operator 
4) Filtering the Payload 
5) Using OrderBy operator
6) Using GroupBy operator
7) quiz

Enterprise systems are quite complex, they consist of multiple applications interact with each other. The messages those flown between these systems are complex in nature. Consists of multiple records information. 
Example: Collection of orders, Collection of items, Collection of persons etc.

Scenario: Mr. Arun working as a software engineer, he is smart and handsome guy desperately looking for marriage alliances. He developed an application that will call the matrimony site and gets the girls details, Now Arun wants to choose his life partner. Let us understand how Dataweave helps him in this process.

The Data he got from matrimony service is as below

<?xml version="1.0" encoding="UTF-8"?>
<matrimony>
   <brides>
      <bride>
         <name>Rakulpreet</name>
         <age>25</age>
         <educationQualification>Btech</educationQualification>
         <profession>BankPO</profession>
         <height>170</height>
         <city>Hyderbabad</city>
      </bride>
      <bride>
         <name>PoojaHegde</name>
         <age>26</age>
         <educationQualification>MBA</educationQualification>
         <height>168</height>
         <monthlyIncome>350000</monthlyIncome>
         <city>Bangalore</city>
      </bride>
      <bride>
         <name>Katrina</name>
         <age>30</age>
         <educationQualification>MA</educationQualification>
         <siblings>2</siblings>
         <height>171</height>
         <city>Mumbai</city>
      </bride>
      <bride>
         <name>Sonakshi</name>
         <age>24</age>
         <educationQualification>MBBS</educationQualification>
         <height>167</height>
         <monthlyIncome>250000</monthlyIncome>
         <city>Mumbai</city>
      </bride>
   </brides>

</matrimony>

The transformation engine needs to Iterate the data and process each record. Dataweave map operator is used to process each record in the collection.

1) Iterating payload (Brides) using Map Operator:
Arun want to save brideName, age, qualifications to csv file.  To save these details, need to pull them from payload and save to file
In this example using map operator we will iterate over each bride and retrieves the required data. Map operates needs to collection to iterate, so all brides should be given as input ( payload.matrimony.brides.*bride) . 
here $ represents the bride object from which information getting pulled.

Ex: while retrieving the first bride details, the first bride data becomes $


Expand : $ means

Expand here : Map Example

The Flow code as below. Change the highlighted path according your machine

<flow name="basic_transformFlow1" doc:id="323d0dfb-1ed6-42b3-9b73-7f29ba5a1db9" >
<http:listener doc:name="Listener" doc:id="0548b13c-1729-424d-b325-5988be8652c9" config-ref="HTTP_Listener_config" path="/bride" outputMimeType="application/xml"/>
<ee:transform doc:name="Transform Message" doc:id="d2963243-4907-4def-9d2d-72b1bf317f1a" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/csv
---
payload.matrimony.brides.*bride map { 

BrideName: $.name,
BrideAge: $.age,
Qualification: $.educationQualification
}]]></ee:set-payload>
</ee:message>
</ee:transform>
  <file:write doc:name="Write" doc:id="9168432e-c2f6-4d2a-802a-4ebe42c027ab" path="C:\AnypointStudio\AnypointStudio\My-workspace\basic_transform\src\main\resources\brides.csv"/> 

</flow>



Iterating payload (Brides) using MapObject Operator:
Arun wants to save the entire brides information to the csv file. He needs to define the column names in the csv file,
Column names should be derived from the Key’s of the payload.
mapObject operator is used to iterate over key-value pairs.

Here MapObject operator is used to iterate over a single bride (Key-value pair). whereas Map operator is used to iterate over collection of brides.  In Below example 


Expand image here : Map Object demo


The dataweave code as below:
%dw 2.0
output application/csv
---
payload.matrimony.brides.*bride map( 
$ mapObject {
'$$' : $
}

)

3) Processing payload using Pluck Operator :
The details set provided by girls differs from one to another, for example some girls providing monthly income details and income field missing in some girl’s details, similarly details like profession, siblings etc. Now Arun  wants to get the fields provided by the girls. Using pluck operator, he will fetch the details by passing girl name as input  

Expand image : Pluck Example The Dataweave code as below:
%dw 2.0
output application/json skipNullOn="everywhere"
var brideName = attributes.queryParams.brideName
---
{
Details_Provided :  payload.matrimony.brides.*bride map(if ($.name == brideName)
 { provided_details : $ pluck $$ } 
 else null)

}

4) Filtering the Payload:

Arun needs to take decision by girl’s qualification, age, city etc. Dataweave filter operator will help Arun to pick up his right choice. Let us see how he is going to apply filter operator to shortlist the bride 

Arun has different kinds of preferences, so he will pass his preferences at runtime, Dataweave need to filter data according to his requirement. Example: he is looking for girl aged less than 26 years and belongs to Mumbai 

The below code as various filter to show up all his requirements 


Expand image here: Filter image

The Dataweave code as below: 

%dw 2.0
output application/json
var age = attributes.queryParams.age
var height = attributes.queryParams.height
var city = attributes.queryParams.city
var qualification = attributes.queryParams.qualification
---
{
 ageQualified : if (age !=null) payload.matrimony.brides.*bride filter ($.age <= age)  else 0, 
 heightQualified : if(height !=null) payload.matrimony.brides.*bride filter ($.height >= height) else 0,
 preferredCity:  if(city != null) payload.matrimony.brides.*bride filter ($.city== city) else 0,
 qualification: if(qualification !=null) payload.matrimony.brides.*bride filter 
  ($.educationQualification== qualification) else 0,
 City_and_age_satisfied: if(age !=null and city !=null) 
  payload.matrimony.brides.*bride filter ($.age <= age and $.city == city) else 0

}

5) Using OrderBy operator:


Arun wants to sort the bride details on basis of age, salary to decide. Arun will send this order parameter through query param. this parameter would be collected through the dataweave variable.

The Dataweave code as follows


%dw 2.0
output application/json
var brides = payload.matrimony.brides.*bride
var param = attributes.queryParams.param
---
if(param=='name') brides orderBy $.name else 

if(param=='age') brides orderBy $.age else brides

The request 
http://localhost:8081/orderBy?param=name
returns the below response, ordered by bride name

[ { "name": "Katrina", "age": "30", "educationQualification": "MA", "siblings": "2", "height": "175", "city": "Mumbai" }, { "name": "PoojaHegde", "age": "26", "educationQualification": "MBA", "height": "168", "monthlyIncome": "350000", "city": "Bangalore" }, { "name": "Rakulpreet", "age": "25", "educationQualification": "Btech", "profession": "BankPO", "height": "170", "city": "Hyderbabad" }, { "name": "Sonakshi", "age": "24", "educationQualification": "MBBS", "height": "167", "monthlyIncome": "250000", "city": "Mumbai" } ]

please test this code using this url
http://localhost:8081/orderBy?param=age

6) Using GroupBy operator:
Arun want to see the brides grouped by city, qualification etc .


%dw 2.0
output application/json
var brides = payload.matrimony.brides.*bride
var param = attributes.queryParams.param
---
if(param=='city') brides groupBy $.city else 

if(param=='qualification') brides groupBy $.educationQualification else brides

http://localhost:8081/groupBy?param=city

{
    "Bangalore": [
        {
            "name": "PoojaHegde",
            "age": "26",
            "educationQualification": "MBA",
            "height": "168",
            "monthlyIncome": "350000",
            "city": "Bangalore"
        }
    ],
    "Mumbai": [
        {
            "name": "Katrina",
            "age": "30",
            "educationQualification": "MA",
            "siblings": "2",
            "height": "175",
            "city": "Mumbai"
        },
        {
            "name": "Sonakshi",
            "age": "24",
            "educationQualification": "MBBS",
            "height": "167",
            "monthlyIncome": "250000",
            "city": "Mumbai"
        }
    ],
    "Hyderbabad": [
        {
            "name": "Rakulpreet",
            "age": "25",
            "educationQualification": "Btech",
            "profession": "BankPO",
            "height": "170",
            "city": "Hyderbabad"
        }
    ]

}

Quiz:
 1) If Matrimony service returns duplicate brides information then How Arun will get distinct brides, please provide the dataweave expression.

2) How to get average age of brides ?

3) How to get total number of brides returned from the service ?

4) How to retrieve brides who earning more than 100000

Sunday, 10 March 2019

Target Variable in Mule-4 connector

Significance of Target Variable in Mule-4 Connector:

This article explains about Target variable in Mule-4 connector. Target Variable is used to store the contents of service call. In earlier versions of Mule, Message Enricher component need to used to achieve this. This process is extremely simplified by introducing the target variable to the connector in Mule -4.

Let me explain this functionality with an example:

 


The example contains two flows Flow-1 and Flow-2. Flow-1 flow as VM publish-Consume endpoint
that invokes the VM listener of flow-2. once the Flow-2 finishes its execution, the payload return to Flow-1 and it replaces the existing payload of Flow-1, so the existing flow-1 payload vanishes.

Collecting the response from Flow-2 to a Variable rather than replacing it to the existing payload is a great idea, it simplifies the developer life to a greater extent. to achieve the similar functionality in Mule earlier versions there are 2 options.
a) Use Message Enricher scope to configure such calls
b) Save the current payload to a variable before calling Service, after the service call, the ,existing payload is replaced by service call payload, Override the service call payload with payload stored in variable.

Code without Target variable :
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
<vm:config name="VM_Config" doc:name="VM Config" doc:id="eaa6afe4-3cbf-4de0-b904-449c354a92e9" >
<vm:queues >
<vm:queue queueName="myqueue" />
</vm:queues>
</vm:config>
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="08907f91-2a21-434c-96d0-eaa5840ba384" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="Flow-1" doc:id="6ebd4e1b-2cdb-4ad2-ba9b-08ef2663f947" >
<http:listener doc:name="Listener" doc:id="05a7dd8e-b363-4153-83bd-f33f8cac1dd3" config-ref="HTTP_Listener_config" path="/"/>
<set-payload value="payload set in main flow" doc:name="Set Payload" doc:id="a94833c0-2de2-4c18-ad89-d3ddac7ca487" />
<vm:publish-consume doc:name="Publish consume" doc:id="81ab6415-d9e0-4c74-af9f-3361c1abd485" config-ref="VM_Config" queueName="myqueue"/>
<logger level="INFO" doc:name="Logger" doc:id="32d83206-906f-493d-ba8f-3fbfc8342e53" message="After VM call  #[payload]  #[vars.flow2Value_variable]"/>
</flow>
<flow name="Flow-2" doc:id="50f343ec-bbb4-48d5-99e5-05383f3ceb06" >
<vm:listener queueName="myqueue" doc:name="Listener" doc:id="f8618bee-2005-4966-ae8b-da8ede2d367a" config-ref="VM_Config"/>
<set-payload value="payload set in VM flow" doc:name="Set Payload" doc:id="2f3a59df-2748-45e8-8ba7-2f543d2ee7ba" />
</flow>
</mule>

Logger Statement. showing the payload returned from Flow-2:
.LoggerMessageProcessor: After VM call  payload set in VM flow  null


Target Variable configuration:



<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
<vm:config name="VM_Config" doc:name="VM Config" doc:id="eaa6afe4-3cbf-4de0-b904-449c354a92e9" >
<vm:queues >
<vm:queue queueName="myqueue" />
</vm:queues>
</vm:config>
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="08907f91-2a21-434c-96d0-eaa5840ba384" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="Flow-1" doc:id="6ebd4e1b-2cdb-4ad2-ba9b-08ef2663f947" >
<http:listener doc:name="Listener" doc:id="05a7dd8e-b363-4153-83bd-f33f8cac1dd3" config-ref="HTTP_Listener_config" path="/"/>
<set-payload value="payload set in main flow" doc:name="Set Payload" doc:id="a94833c0-2de2-4c18-ad89-d3ddac7ca487" />
<vm:publish-consume doc:name="Publish consume" doc:id="81ab6415-d9e0-4c74-af9f-3361c1abd485" config-ref="VM_Config" queueName="myqueue" target="flow2Value_variable"/>
<logger level="INFO" doc:name="Logger" doc:id="32d83206-906f-493d-ba8f-3fbfc8342e53" message="After VM call  #[payload]  #[vars.flow2Value_variable]"/>
</flow>
<flow name="Flow-2" doc:id="50f343ec-bbb4-48d5-99e5-05383f3ceb06" >
<vm:listener queueName="myqueue" doc:name="Listener" doc:id="f8618bee-2005-4966-ae8b-da8ede2d367a" config-ref="VM_Config"/>
<set-payload value="payload set in VM flow" doc:name="Set Payload" doc:id="2f3a59df-2748-45e8-8ba7-2f543d2ee7ba" />
</flow>
</mule>

Logger Statement. showing the  existing payload and  target variable
.LoggerMessageProcessor: After VM call  payload set in main flow  payload set in VM flow

Friday, 8 March 2019

Mule 4 Event Model




Mule Event and Mule Message:
When a request reaches the Event source of a flow (such as an HTTP request
 or a change to a database or file).  A mule event will be generated it creates mule event object. Mule event object primarily consists of information required to process by the Mule runtime. Mule event object travels through all the components configured in mule flow.

Mule event object is immutable, so every change to an instance of a Mule event object results in the creation of a new instance.
A Mule Event object is composed of these objects:
  1. A Mule Message contains a message payload and its associated attributes.
  2.  Variables are Mule event metadata that you use in your flow.

The sequence of the activities happens when event source receives the trigger
  • A trigger reaches the event source.    
  • The event source produces a Mule event.
  •  The Mule event travels sequentially through the components of a flow.Each component interacts in a pre-defined manner with the Mule event




How the Mule Event object get created from the incoming request ?
Standard message structure consists of Header and Payload parts, where Payload is core business information that need to processed and Header contains details about payload like encryption schemes, security credentials etc. Header content also called metadata. 

When a standard message reaches the Mule Event source (in other words its called Inbound Endpoint), Event source converts that message to Mule Event Object as Mule Event processors can only understand only Mule Event object. and handovers to next event processor.

Mule Event source creates Mule Event object by coping Header contents to Attributes section and Payload contents to Payload section



<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<file:config name="File_Config" doc:name="File Config" doc:id="ce7b6ee8-e9f2-4370-b17f-2adf413d9826" >
</file:config>
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="825c4787-27c3-4595-baa3-5fa66d3ca97f" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="mule_event_demoFlow" doc:id="1bbf3a21-775f-49eb-9d06-48bb9f9a51dd" >
<http:listener doc:name="Listener" doc:id="a287d233-9054-423c-818d-e73a1fa67b51" config-ref="HTTP_Listener_config" path="/"/>
<logger level="INFO" doc:name="Logger" doc:id="cb41a866-d0a9-4a06-b7f4-5f75b3c62272" message="The City from source is #[message.attributes.headers.City]"/>
<set-payload value="This payload is returned to client" doc:name="Set Payload" doc:id="2ab7bb3e-3943-4da1-9c54-c76907348924" />
</flow>
</mule>


Monday, 9 July 2018

Choice Router in Mule

The choice flow control dynamically routes messages based on message payload or properties. It adds conditional programming to a flow, similar to an if/then/else code block.
A choice flow control uses expressions to evaluate the content of a message, then it routes the message to one of the routing options within its scope (see image below). It directs messages to the first routing option in the scope that matches the routing configurations (evaluates to true). If none of expressions evaluate to true, the choice flow control directs the message to the default (else) route.
The implementation in Mule as follows
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="choicedemoFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <logger message="#[message.inboundProperties.'http.query.params'.get('source')]" level="INFO" doc:name="Logger"/>
        <choice doc:name="Choice">
            <when expression="#[message.inboundProperties.'http.query.params'.get('source')=='File']">
                <file:outbound-endpoint path="src/main/resources" responseTimeout="10000" doc:name="File"/>
            </when>
            <otherwise>
                <vm:outbound-endpoint exchange-pattern="one-way" path="abc" doc:name="VM"/>
            </otherwise>
        </choice>
    </flow>
    <flow name="choicedemoFlow1">
        <vm:inbound-endpoint exchange-pattern="one-way" path="abc" doc:name="VM"/>
        <logger message="THe message retrived is #[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>








Monday, 2 July 2018

The Pluck Operator in Dataweave

The Pluck Operator is used to retrieve the Keys in an array or to retrieve the Values to an array
If key and value parameters are not named, the key is defined by default as $$ and the value as $.

Example:
The Following input is processed by the dataweave code defined below input
Input:

<prices>

    <basic>9.99</basic>
    <premium>53</premium>
    <vip>398.99</vip>
</prices>

Dataweave code:

%dw 1.0
%output application/json
---
result: {
  keys: payload.prices pluck $$,
  values: payload.prices pluck $
}

The output as follows
{

  "result": {
    "keys": [
      "basic",
      "premium",
      "vip"
    ],
    "values": [
      "9.99",
      "53",
      "398.99"
    ]
  }
}

The implementation is as below

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8082" basePath="pluck" doc:name="HTTP Listener Configuration"/>
    <flow name="pluck_exampleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
result: {
  keys: payload.prices pluck $$,
  values: payload.prices pluck $
}

]]></dw:set-payload>
        </dw:transform-message>
    </flow>
</mule>







How to quick start your Mulesoft professional journey

Mulesoft is leading and Fast growing Application integration framework in the industry. Do you want to start professional journey in Mulesof...