Thursday, July 20, 2017

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..!!

4 comments:

  1. the blog is good and Interactive it is about Mulesoft Anypoint Studio it is useful for students and Mulesoft Developers for more updates on Mulesoft mulesoft Online training india

    ReplyDelete
  2. This idea is mind blowing. I think everyone should know such information like you have described on this post. Thank you for sharing this explanation. I have learned a lot from this .

    oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    oracle online training

    hadoop training in chennai

    hadoop training in bangalore



    ReplyDelete
  3. Casinos Near Bryson City, Bryson City NC | MapyRO
    Best 대전광역 출장안마 Casino Near 안동 출장샵 Bryson City NC · The Sky River Casino and Lodge · The Wynn Hotel · 군산 출장마사지 Pautipaug 제주 출장샵 Casino & Lodge · Encore Atrium Hotel 용인 출장안마 & Spa · The Bryson City

    ReplyDelete