I had solved such problem by processing the file multiple times.
On every pass I'll try to read\process every record in the file with such alg:
- if record has parent - check if parent already stored. If no - I skip it in processor
- if record unchanged (or already stored if updates are not possible) - skip it in processor
- else - store in db
And then declare loop and decider:
<batch:step id="processParentAndChilds" next="loop">
<batch:tasklet>
<batch:chunk reader="processParentAndChildsReader"
commit-interval="1000">
<batch:processor>
<bean class="processParentAndChildsProcessor"/>
</batch:processor>
<batch:writer>
<bean class="processParentAndChildsWriter"/>
</batch:writer>
</batch:chunk>
</batch:tasklet>
</batch:step>
<batch:decision decider="processParentAndChildsRetryDecider" id="loop">
<batch:next on="NEXT_LEVEL" to="processprocessParentAndChilds"/>
<batch:next on="COMPLETED" to="goToNextSteps"/>
</batch:decision>
public class ProcessParentAndChildsRetryDecider implements JobExecutionDecider{
@Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
// if no on record written - no sense to try again
if (stepExecution.getWriteCount() > 0) {
return new FlowExecutionStatus("NEXT_LEVEL");
} else {
return FlowExecutionStatus.COMPLETED;
}
}
}