Incomplete - task 87: Refactor KeyedItemReader

Split off from ItemReader interface.
This commit is contained in:
dsyer
2008-03-01 15:24:06 +00:00
parent 886e8b4f75
commit b89a68dfeb
18 changed files with 220 additions and 116 deletions

View File

@@ -35,11 +35,11 @@ import org.springframework.batch.execution.step.support.ThreadStepInterruptionPo
import org.springframework.batch.io.Skippable;
import org.springframework.batch.io.exception.InfrastructureException;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemKeyGenerator;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemRecoverer;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.KeyedItemReader;
import org.springframework.batch.item.exception.CommitFailedException;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatCallback;
@@ -99,6 +99,26 @@ public class ItemOrientedStep extends AbstractStep implements InitializingBean {
private ListenerMulticaster listener = new ListenerMulticaster();
private ItemKeyGenerator itemKeyGenerator;
private ItemKeyGenerator defaultKeyGenerator = new ItemKeyGenerator() {
public Object getKey(Object item) {
return item;
}
};
/**
* Public setter for the {@link ItemKeyGenerator}. If it is not injected
* but the reader or writer implement {@link ItemKeyGenerator}, one of
* those will be used instead (preferring the reader to the writer if both
* would be appropriate).
*
* @param itemKeyGenerator the {@link ItemKeyGenerator} to set
*/
public void setItemKeyGenerator(ItemKeyGenerator itemKeyGenerator) {
this.itemKeyGenerator = itemKeyGenerator;
}
/**
* Register each of the objects as listeners. The {@link ItemOrientedStep}
* accepts listeners of type {@link ItemStream} and {@link BatchListener}.
@@ -196,10 +216,10 @@ public class ItemOrientedStep extends AbstractStep implements InitializingBean {
ItemReaderRetryPolicy itemProviderRetryPolicy = new ItemReaderRetryPolicy(retryPolicy);
template.setRetryPolicy(itemProviderRetryPolicy);
itemKeyGenerator = getKeyGenerator();
if (retryPolicy != null) {
Assert.state(itemReader instanceof KeyedItemReader,
"ItemReader must be instance of KeyedItemReader to use the retry policy");
retryCallback = new ItemReaderRetryCallback((KeyedItemReader) itemReader, itemWriter);
retryCallback = new ItemReaderRetryCallback(itemReader, itemKeyGenerator, itemWriter);
}
if (this.chunkOperations instanceof RepeatTemplate && commitInterval > 0) {
@@ -212,6 +232,23 @@ public class ItemOrientedStep extends AbstractStep implements InitializingBean {
}
/**
* @return
*/
private ItemKeyGenerator getKeyGenerator() {
if (itemKeyGenerator != null) {
return itemKeyGenerator;
}
if (itemReader instanceof ItemKeyGenerator) {
return (ItemKeyGenerator) itemReader;
}
if (itemWriter instanceof ItemKeyGenerator) {
return (ItemKeyGenerator) itemWriter;
}
return defaultKeyGenerator;
}
/**
* Process the step and update its context so that progress can be monitored
* by the caller. The step is broken down into chunks, each one executing in