Batch Processing in Mule 4
- April 07, 2020
Let’s discuss batch processing.
MuleSoft allows you to process messages as a batch that can be achieved by batch scope. Batch scope in a Mule application can:
- Divide the input payload into individual records
- Perform actions on these individual records
- Send the processed data to target systems
A batch job divides the payload into 100 records per batch, and it will process 16 threads at a time.
Batch has three phases in Mule 4.
Load and dispatch: This phase creates job instances, converts the payload into a collection of records and then splits the collection into individual records for processing.
Process: This phase processes all individual records asynchronously. Batch step in this phase allows you to filter records. You can also use a batch aggregator processor to aggregate records into groups. For example, if you want to process 10 batches as one group, you can set the aggregate processor size as 10.
On complete: This last — and optional — phase provides a summary of the payload. It shows how many records are processed, and how many failed.
The following batch processing example inserts the records into Salesforce using batch processing, I’ve created an Employee custom object in Salesforce and then storing the records through Mule using batch processing.
Now, I’m going to construct one flow in AnyPoint Studio by taking an HTTP Listener and transforming the message component for field mapping with respect to Salesforce.
Drag and drop the batch job from the Mule Pallet, and the default batch job size is 100. Create single component from a Salesforce connector to store the single record in Salesforce.
Configure the created single component with the Salesforce configuration and type in the name of the object created in Salesforce — Employee.
Finally, deploy the project locally and test from postman. The batch job will send the payload asynchronously, one record at a time, to each batch step and then apply the necessary actions on records and store the data in the Salesforce. Here’s what the processed data loos like:
Drag and drop the set payload component from Mule Pallet to the OnComplete phase of the batch job. After that, you can see a summary of the records — total records processed, successful records and failed records — as shown in the screenshot below.
This is the way in which batch processing works in Mule 4.
— By Swetha Bokka