BATCH-531: Moved item count increment from Step to ItemHandler so that the last call to ItemReader#read() doesn't inadvertently increment the count. Also added a test case.
This commit is contained in:
@@ -320,7 +320,6 @@ public class ItemOrientedStep extends AbstractStep {
|
||||
// check for interruption before each item as well
|
||||
interruptionPolicy.checkInterrupted(execution);
|
||||
ExitStatus exitStatus = itemHandler.handle(contribution);
|
||||
contribution.incrementItemCount();
|
||||
// check for interruption after each item as well
|
||||
interruptionPolicy.checkInterrupted(execution);
|
||||
return exitStatus;
|
||||
|
||||
@@ -89,6 +89,7 @@ public class SimpleItemHandler implements ItemHandler {
|
||||
* @param contribution current context
|
||||
*/
|
||||
protected void write(Object item, StepContribution contribution) throws Exception {
|
||||
contribution.incrementItemCount();
|
||||
doWrite(item);
|
||||
}
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@ import org.springframework.batch.item.MarkFailedException;
|
||||
import org.springframework.batch.item.ResetFailedException;
|
||||
import org.springframework.batch.item.support.ListItemReader;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.batch.repeat.policy.DefaultResultCompletionPolicy;
|
||||
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
|
||||
import org.springframework.batch.repeat.support.RepeatTemplate;
|
||||
import org.springframework.batch.support.PropertiesConverter;
|
||||
@@ -750,6 +751,22 @@ public class ItemOrientedStepTests extends TestCase {
|
||||
assertTrue(stepExecution.getExecutionContext().getString("spam").equals("bucket"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testStepToCompletion() throws Exception {
|
||||
|
||||
RepeatTemplate template = new RepeatTemplate();
|
||||
|
||||
// process all items:
|
||||
template.setCompletionPolicy(new DefaultResultCompletionPolicy());
|
||||
itemOrientedStep.setStepOperations(template);
|
||||
|
||||
JobExecution jobExecutionContext = new JobExecution(jobInstance);
|
||||
StepExecution stepExecution = new StepExecution(itemOrientedStep, jobExecutionContext);
|
||||
|
||||
itemOrientedStep.execute(stepExecution);
|
||||
assertEquals(3, processed.size());
|
||||
assertEquals(3, stepExecution.getItemCount().intValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception in {@link StepExecutionListener#afterStep(StepExecution)}
|
||||
|
||||
Reference in New Issue
Block a user