You can apply one or more filters as attributes to any number of batch steps.
Imagine a batch job whose first batch step checks if a Salesforce contact exists for a record, and a second batch step that updates each existing Salesforce contact with new information. You can apply a filter to the second batch step to ensure it only processes records that didn’t fail during the first batch step.
By having batch steps accept only some records for processing, you streamline the batch job so the Mule runtime engine can focus only on the relevant data for a particular batch step.
A batch step uses two attributes to filter records:
-
acceptExpression
-
acceptPolicy
Each batch step can accept one acceptExpression and one acceptPolicy attributes to filter records.
Use the acceptExpression attribute to process only records that evaluate to true; if the record evaluates to false, the batch step skips the record and sends it to the next one. In other words, the records with an accept expression that resolves to false are the ones that Mule filters out.
The example below filters out all records where the age is less than 21; the batch step does not process those records.
<batch:job jobName="batchJob">
<batch:process-records >
<batch:step name="adultsOnlyStep" acceptExpression="#[payload.age > 21]">
...
</batch:step>
</batch:process-records>
</batch:job>
Use the acceptPolicy attribute from batch step to process only the records which, relative to the value of the accept policy attribute, evaluate to true. Refer to the table below for a list of the available values for the accept policy.
| Accept Policy |
When evaluates to TRUE |
|
Default
Batch step processes only those records that succeeded to process in all preceding steps.
|
|
Batch step processes only those records that failed to process in a preceding batch step.
|
|
Batch step processes all records, regardless of whether they failed to process in a preceding batch step.
|
If you don’t apply filters to a batch step, the batch processes only those records that succeeded to process in all preceding steps. In other words, the default Accept Policy applied to all batch steps is NO_FAILURES.
The example below illustrates the second batch step in a batch job that processes only those records that failed to process during the preceding step. In the first batch step, the runtime checked each record to see if it had an existing Salesforce contact; the second batch step, which creates a contact for each record, processes only the failed records (that is, records that failed to have an existing account).
<batch:job jobName="batchJob">
<batch:process-records >
<batch:step name="batchStep1">
...
</batch:step>
<batch:step name="batchStep2" accept-policy="ONLY_FAILURES">
...
</batch:step>
</batch:process-records>
</batch:job>
Each batch job has a maxFailedRecords attribute that controls how many failed records you are willing to accept for a batch job.
When a batch job instance exceeds its maxFailedRecords value, regardless of the filter set on the batch step, the step does not process any records and pushes the failed batch job instance to the On Complete phase.
See Handling Errors During Batch Job for more information.
Filter Characteristics
-
Batch filters only apply to batch steps which, in turn, are only usable within the batch process phase of a batch job. You cannot apply filters with the Input or On Complete phases.
-
If you apply no filters to a batch step, the batch processes only those records which succeeded to process in all preceding steps. In other words, the default Accept Policy applied to all batch steps is NO_FAILURES.
-
When a batch job instance exceeds its max-failed-records value, regardless of the filter set on the batch step, the step does not process any records and pushes the failed batch job instance to the On Complete phase.
-
Where you apply both types of filters, Mule evaluates them in the following order:
-
Accept Policy
-
Accept Expression