Thanks for the hints, they made me implement something like this:
Code:
/**
* Custom reader that needs to be configured BOTH as reader AND as
* chunk-completion-policy to be able to update its internal state.
*/
public class RecordReader extends SingleItemPeekableItemReader<Scheduling> implements CompletionPolicy {
private Record current;
@Override
public boolean isComplete(RepeatContext context) {
return ((ReaderRepeatContext) context).isComplete();
}
@Override
public boolean isComplete(RepeatContext context, RepeatStatus result) {
return ((ReaderRepeatContext) context).isComplete();
}
@Override
public RepeatContext start(RepeatContext parent) {
/*
* Set first item of the chunk for later comparison.
*/
this.current = invokePeek();
return new ReaderRepeatContext(parent);
}
@Override
public void update(RepeatContext context) {
/*
* Check if the step should finish.
* In this case when there are no more records to process.
*/
if (current == null) {
context.setCompleteOnly();
}
}
private Scheduling invokePeek() {
Record peeked = null;
try {
peeked = peek();
} catch (Exception e) {
e.printStackTrace();
}
return peeked;
}
//custom RepeatContext
protected class ReaderRepeatContext extends RepeatContextSupport {
public ReaderRepeatContext(RepeatContext parent) {
super(parent);
}
public boolean isComplete() {
Record next = null;
next = invokePeek();
if (next == null || next.getDate().after(current.getDate())) {
current = next;
return true;
}
return false;
}
}
}
And the step is configured something like this:
Code:
<step id="chunkStep">
<tasklet transaction-manager="jobRepository-transactionManager">
<chunk reader="peekableReader"
processor="scheduleCopyProcessor"
writer="scheduleCopyWriter"
chunk-completion-policy="peekableReader"/>
</tasklet>
</step>
<bean id="peekableReader" scope="singleton" class="RecordReader" >
<property name="delegate" ref="schedulingReader" />
</bean>
This seems to work for my use-case, in which records in a file have a date field and I want to commit records with the same date in one chunk, so a commit interval per new encountered date.
Thanks for the hint, and always open for suggestions.