OPEN - BATCH-803: Add non-buffering ChunkOrientedTasklet (or option in existing one) plus flag for factory bean
added SimpleChunkOrientedTasklet *not* designed for inheritance, so that we can deal with fault-tolerance concerns separately and in one place
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package org.springframework.batch.core.step.item;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.batch.repeat.RepeatCallback;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.RepeatOperations;
|
||||
import org.springframework.core.AttributeAccessor;
|
||||
|
||||
/**
|
||||
* Simplest possible implementation of {@link Tasklet} with no skipping or
|
||||
* recovering. Just delegates all calls to the provided {@link ItemReader} and
|
||||
* {@link ItemWriter}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class SimpleChunkOrientedTasklet<I, O> extends AbstractItemProcessingTasklet<I, O> {
|
||||
|
||||
private RepeatOperations repeatOperations;
|
||||
|
||||
public SimpleChunkOrientedTasklet(ItemReader<? extends I> itemReader,
|
||||
ItemProcessor<? super I, ? extends O> itemProcessor, ItemWriter<? super O> itemWriter,
|
||||
RepeatOperations repeatOperations) {
|
||||
super(itemReader, itemProcessor, itemWriter);
|
||||
this.repeatOperations = repeatOperations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read-process-write a list of items.
|
||||
*/
|
||||
public ExitStatus execute(final StepContribution contribution, AttributeAccessor attributes) throws Exception {
|
||||
ExitStatus result = ExitStatus.CONTINUABLE;
|
||||
final List<I> inputs = new ArrayList<I>();
|
||||
|
||||
result = repeatOperations.iterate(new RepeatCallback() {
|
||||
|
||||
public ExitStatus doInIteration(final RepeatContext context) throws Exception {
|
||||
I item = doRead();
|
||||
|
||||
if (item == null) {
|
||||
return ExitStatus.FINISHED;
|
||||
}
|
||||
inputs.add(item);
|
||||
contribution.incrementReadCount();
|
||||
return ExitStatus.CONTINUABLE;
|
||||
}
|
||||
});
|
||||
|
||||
// If there is no input we don't have to do anything more
|
||||
if (inputs.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
|
||||
List<O> outputs = new ArrayList<O>();
|
||||
for (I item : inputs) {
|
||||
O output = doProcess(item);
|
||||
if (output != null) {
|
||||
outputs.add(output);
|
||||
}
|
||||
}
|
||||
contribution.incrementFilterCount(inputs.size() - outputs.size());
|
||||
|
||||
// TODO: use ItemWriter interface properly
|
||||
// TODO: make sure exceptions get handled by the appropriate handler
|
||||
doWrite(outputs);
|
||||
contribution.incrementWriteCount(outputs.size());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -457,7 +457,7 @@ public class SimpleStepFactoryBean<T,S> implements FactoryBean, BeanNameAware {
|
||||
|
||||
step.setStepOperations(stepOperations);
|
||||
|
||||
ChunkOrientedTasklet<T,S> tasklet = new ChunkOrientedTasklet<T,S>(itemReader, itemProcessor, itemWriter, chunkOperations);
|
||||
SimpleChunkOrientedTasklet<T,S> tasklet = new SimpleChunkOrientedTasklet<T,S>(itemReader, itemProcessor, itemWriter, chunkOperations);
|
||||
tasklet.setListeners(getListeners());
|
||||
step.setTasklet(tasklet);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user