Thursday, July 20, 2017

Mule ESB - How to read JSON payload content?

In this blog, I'm going to explain how to read json payload in the mule flow.

For an example, we will assume that json payload receives as below
{
    "persons": [{
            "person": {
                "name": "Tharanga Wijeweear"
            }
        }]
}

You have to use the JSON prefix to access the json payload.

If you need to access the name of the first person, you need to write the mule expression as below.
#[json:persons[0]/person/name]

You can use the same steps followed in the previous blog (Mule ESB - How to read JSON payload content?)

Enjoy..!!

Mule ESB - How to read xml payload content?

In this blog, I'm going to explain the way we can access the XML payload content and assign into a variable.

For an example, we will assume that mule flow received a xml message as below
<persons>
    <person>
        <name>Tharanga</name>
        <city>Colombo</city>
        <address>
            <street>1st Cross Street</street>
            <postalCode>10250</postalCode>
            <country>Sri Lanka</country>
        </address>
    </person>
</persons>

If we need to read the name of the person, have to write a mule expression as below
#[xpath://persons/person/name]

Since this is a xml payload, we need to use the xpath prefix to access the payload.

I used below sample flow to check this scenario.
<mule 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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8089" doc:name="HTTP Listener Configuration"/>
    <flow name="demo-readpayloadFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/payload" doc:name="HTTP"/>
        <set-variable variableName="messageid" value="#[xpath://persons/person/name]" doc:name="Variable"/>
        <logger message="#[flowVars.messageid]" level="INFO" doc:name="Logger"/>
        <set-payload value="#[flowVars.messageid]" doc:name="Set Payload"/>
    </flow>
</mule>

When invoking the flow, we can see the value in the console according to the above sample.
INFO  2017-07-21 09:24:16,065 [[demo-readpayload].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Tharanga

Likewise, you can read the xml payload.

If the payload comes with the namespaces, you need to define the namespaces in the mulexml:namespace-manager tag as below
<mulexml:namespace-manager includeConfigNamespaces="true">
 <mulexml:namespace prefix="abc" uri="http://sample.lk/test" />
</mulexml:namespace-manager>
For an example, we will assume that payload comes as follows
<persons xmlns:abc="http://sample.lk/test">
    <abc:person>
        <abc:name>Tharanga</abc:name>
        <abc:city>Colombo</abc:city>
        <abc:address>
            <abc:street>1st Cross Street</abc:street>
            <abc:postalCode>10250</abc:postalCode>
            <abc:country>Sri Lanka</abc:country>
        </abc:address>
    </abc:person>
</persons>
And then need to change the mule expression as below
#[xpath://persons/abc:person/abc:name]

Likewise, you can read the xml payload when comes with the namespaces.

Enjoy..!!


Mule ESB - How to use batch element?

In this blog, I'm going to explain how to use the batch element in a flow.

In the Anypoint studio, you can search the batch element in the 'Mule Palette'. Once you drag and drop the 'Batch' element, you can see there are three (3) component on it.
1. Input
2. Process Records (In this step, you can have multiple steps)
3. On Complete

For this example, I'm going to use the 'Database element' as the input.

You cannot invoke the batch:job directly, so you have to use batch:execute to trigger the batch:job. In this example, I'm going to use the HTTP element to trigger this.

Find the blow configuration xml which I used to explain this.

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:batch="http://www.mulesoft.org/schema/mule/batch" 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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/batch http://www.mulesoft.org/schema/mule/batch/current/mule-batch.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">
    <db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="tharanga" password="tharanga" database="muledb" doc:name="MySQL Configuration"/>
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8089" doc:name="HTTP Listener Configuration"/>
    <batch:job name="demo-batchBatch">
        <batch:input>
            <db:select config-ref="MySQL_Configuration" doc:name="Database">
                <db:parameterized-query><![CDATA[select * from student;]]></db:parameterized-query>
            </db:select>
        </batch:input>
        <batch:process-records>
            <batch:step name="Batch_Step1">
                <file:outbound-endpoint path="/home/tharanga/workspace/training/mule-esb/tmp/batch1" responseTimeout="10000" doc:name="File"/>
                <logger message="Step1 completed" level="INFO" doc:name="Logger"/>
            </batch:step>
            <batch:step name="Batch_Step2">
                <file:outbound-endpoint path="/home/tharanga/workspace/training/mule-esb/tmp/batch2" responseTimeout="10000" doc:name="File"/>
                <logger message="Step2 completed" level="INFO" doc:name="Logger"/>
            </batch:step>
        </batch:process-records>
        <batch:on-complete>
            <logger message="All steps are completed" level="INFO" doc:name="Logger"/>
        </batch:on-complete>
    </batch:job>
    <flow name="demo-batchFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
        <batch:execute name="demo-batchBatch" doc:name="demo-batchBatch"/>
    </flow>
</mule>

Note:
In the above configuration I used 2 batch:process steps. In the first step I added the record to the batch1 directory and second steps used to add the record to the batch2 directory, using the file element.
To invoke the flow I used HTTP listener.

When invokes the flow, we can see the log as below.
....
INFO  2017-07-21 05:38:23,594 [batch-job-demo-batchBatch-work-manager.04] org.mule.transport.file.FileConnector: Writing file to: /home/tharanga/workspace/training/mule-esb/tmp/batch1/b6257190-6da8-11e7-bae9-0242dea5cda5.dat
INFO  2017-07-21 05:38:23,594 [batch-job-demo-batchBatch-work-manager.04] org.mule.api.processor.LoggerMessageProcessor: Step1 completed
INFO  2017-07-21 05:38:23,595 [batch-job-demo-batchBatch-work-manager.04] org.mule.transport.file.FileConnector: Writing file to: /home/tharanga/workspace/training/mule-esb/tmp/batch1/b625bfb0-6da8-11e7-bae9-0242dea5cda5.dat
INFO  2017-07-21 05:38:23,596 [batch-job-demo-batchBatch-work-manager.04] org.mule.api.processor.LoggerMessageProcessor: Step1 completed
INFO  2017-07-21 05:38:23,597 [batch-job-demo-batchBatch-work-manager.04] org.mule.transport.file.FileConnector: Writing file to: /home/tharanga/workspace/training/mule-esb/tmp/batch1/b6260dd0-6da8-11e7-bae9-0242dea5cda5.dat
INFO  2017-07-21 05:38:23,597 [batch-job-demo-batchBatch-work-manager.04] org.mule.api.processor.LoggerMessageProcessor: Step1 completed
INFO  2017-07-21 05:38:23,611 [batch-job-demo-batchBatch-work-manager.04] com.mulesoft.module.batch.DefaultBatchStep: Step Batch_Step1 finished processing all records for instance b621c813-6da8-11e7-bae9-0242dea5cda5 of job demo-batchBatch
....
INFO  2017-07-21 05:38:34,038 [batch-job-demo-batchBatch-work-manager.04] org.mule.transport.file.FileConnector: Writing file to: /home/tharanga/workspace/training/mule-esb/tmp/batch2/bc5f3961-6da8-11e7-bae9-0242dea5cda5.dat
INFO  2017-07-21 05:38:34,039 [batch-job-demo-batchBatch-work-manager.04] org.mule.api.processor.LoggerMessageProcessor: Step2 completed
INFO  2017-07-21 05:38:34,040 [batch-job-demo-batchBatch-work-manager.04] org.mule.transport.file.FileConnector: Writing file to: /home/tharanga/workspace/training/mule-esb/tmp/batch2/bc5f8780-6da8-11e7-bae9-0242dea5cda5.dat
INFO  2017-07-21 05:38:34,041 [batch-job-demo-batchBatch-work-manager.04] org.mule.api.processor.LoggerMessageProcessor: Step2 completed
INFO  2017-07-21 05:38:34,045 [batch-job-demo-batchBatch-work-manager.04] org.mule.transport.file.FileConnector: Writing file to: /home/tharanga/workspace/training/mule-esb/tmp/batch2/bc604ad0-6da8-11e7-bae9-0242dea5cda5.dat
INFO  2017-07-21 05:38:34,046 [batch-job-demo-batchBatch-work-manager.04] org.mule.api.processor.LoggerMessageProcessor: Step2 completed
INFO  2017-07-21 05:38:34,050 [batch-job-demo-batchBatch-work-manager.04] com.mulesoft.module.batch.engine.DefaultBatchEngine: Starting execution of onComplete phase for instance b621c813-6da8-11e7-bae9-0242dea5cda5 of job demo-batchBatch
INFO  2017-07-21 05:38:34,050 [batch-job-demo-batchBatch-work-manager.04] org.mule.api.processor.LoggerMessageProcessor: All steps are completed
....
And finally, it also shows the summary of the process the as below (in the console)

INFO  2017-07-21 05:38:34,050 [batch-job-demo-batchBatch-work-manager.04] com.mulesoft.module.batch.engine.DefaultBatchEngine: Finished execution for instance 'b621c813-6da8-11e7-bae9-0242dea5cda5' of job 'demo-batchBatch'. Total Records processed: 3. Successful records: 3. Failed Records: 0

Enjoy..!!

Wednesday, July 19, 2017

Mule ESB - payload base64-encoding and base64-decoding

In this blog, I'm going to explain how to make our payload encoding and decoding.

You can use base64-encoder-transformer to encode the payload
<base64-encoder-transformer xmlns="http://www.mulesoft.org/schema/mule/core" encoding="utf-8"></base64-encoder-transformer>

You can use base64-decoder-transformer to decode the payload
<base64-decoder-transformer xmlns="http://www.mulesoft.org/schema/mule/core" encoding="utf-8"></base64-decoder-transformer>

Find the below sample flow to check the encoding and decoding mechanism.
<mule 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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8089" doc:name="HTTP Listener Configuration"/>
    <flow name="demo-encode-decodeFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/sample" doc:name="HTTP" allowedMethods="POST"/>
        <logger message="#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
        <base64-encoder-transformer xmlns="http://www.mulesoft.org/schema/mule/core" encoding="utf-8"></base64-encoder-transformer>
        <logger message="#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
        <base64-decoder-transformer xmlns="http://www.mulesoft.org/schema/mule/core" encoding="utf-8"></base64-decoder-transformer>
        <logger message="#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

Note: I used the logger component to print the encode and decode messages.

After making the POST request with a payload, we can see the below logs in the console:
INFO  2017-07-20 05:26:33,031 [[demo-encode-decode].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Original Payload: ,{
  "id": "mule001",
  "name": "Mule course",
  "description": "Mule course Description"
}
INFO  2017-07-20 05:26:33,032 [[demo-encode-decode].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: base64-encoded message: ,ewogICJpZCI6ICJtdWxlMDAxIiwKICAibmFtZSI6ICJNdWxlIGNvdXJzZSIsCiAgImRlc2NyaXB0aW9uIjogIk11bGUgY291cnNlIERlc2NyaXB0aW9uIgp9
INFO  2017-07-20 05:26:33,032 [[demo-encode-decode].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: base64-decoded message: ,{
  "id": "mule001",
  "name": "Mule course",
  "description": "Mule course Description"
}

Enjoy..!!

Mule ESB - How to undeploy a flow?

In this blog, I'm going to explain how to undeploy a flow in a clean way.

When we are starting a 'Anypoint studio', we have to choose the workspace.

For this explanation, we will assume that

1. our workspace as below
/home/tharanga/AnypointStudio/workspace
2. create a project as 'demo-http'

Once we run this application as 'Mule Application', we can see that, it creates the below file and folder inside the '/home/tharanga/AnypointStudio/workspace/.mule/apps' location.
drwxrwxr-x  5 tharanga tharanga 4096 ජූලි   19 19:58 demo-http/
-rw-rw-r--  1 tharanga tharanga   77 ජූලි   19 19:58 demo-http-anchor.txt

 To undeploy this flow, you can delete the <project_name>-anchor.txt file. According to this example file would be demo-http-anchor.txt

Note: anchor file location path would be <Anypoint_studio_workspace>/.mule/apps

Note: Inside the anchor file you can see the below content.
Delete this file while Mule is running to remove the artifact in a clean way.

Enjoy...!!