RESOLVED - issue BATCH-268: retry configuration for ItemOrientedTasklet

http://jira.springframework.org/browse/BATCH-268

The retry only works if you have an ItemStream as an ItemReader (so that you get a proper rollback).  Applied the patch and fixed it up to fit the new m5 way of looking at things.
This commit is contained in:
dsyer
2008-02-11 17:23:57 +00:00
parent 0824f1aeb3
commit 2ce3013ac1
21 changed files with 338 additions and 14 deletions

View File

@@ -0,0 +1,109 @@
package org.springframework.batch.sample.item.reader;
import java.math.BigDecimal;
import org.springframework.batch.item.ExecutionAttributes;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.exception.MarkFailedException;
import org.springframework.batch.item.exception.ResetFailedException;
import org.springframework.batch.item.exception.StreamException;
import org.springframework.batch.item.reader.AbstractItemReaderRecoverer;
import org.springframework.batch.sample.domain.Trade;
/**
* Generates configurable number of {@link Trade} items.
*
* @author Robert Kasanicky
*/
public class GeneratingItemReader extends AbstractItemReaderRecoverer implements ItemStream {
private int limit = 1;
private int counter = 0;
private int marked;
public Object read() throws Exception {
if (counter < limit) {
counter++;
return new Trade(
"isin" + counter,
counter,
new BigDecimal(counter),
"customer" + counter);
}
return null;
}
/**
* @param limit number of items that will be generated
* (null returned on consecutive calls).
*/
public void setLimit(int limit) {
this.limit = limit;
}
public int getCounter() {
return counter;
}
public int getLimit() {
return limit;
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ItemRecoverer#recover(java.lang.Object, java.lang.Throwable)
*/
public boolean recover(Object data, Throwable cause) {
return false;
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#close()
*/
public void close() throws StreamException {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#isMarkSupported()
*/
public boolean isMarkSupported() {
return true;
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#mark()
*/
public void mark() throws MarkFailedException {
this.marked = this.counter;
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws StreamException {
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#reset()
*/
public void reset() throws ResetFailedException {
this.counter = this.marked;
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.ExecutionAttributes)
*/
public void restoreFrom(ExecutionAttributes context) {
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ExecutionAttributesProvider#getExecutionAttributes()
*/
public ExecutionAttributes getExecutionAttributes() {
return new ExecutionAttributes();
}
}

View File

@@ -0,0 +1,29 @@
package org.springframework.batch.sample.item.writer;
import org.springframework.batch.item.ItemWriter;
/**
* Simulates temporary output trouble - requires to
* retry 3 times to pass successfully.
*
* @author Robert Kasanicky
*/
public class RetrySampleItemWriter implements ItemWriter {
private int counter = 0;
public void write(Object data) throws Exception {
counter++;
if (counter == 2 || counter == 3) {
throw new RuntimeException("Temporary error");
}
}
/**
* @return number of times {@link #process(Object)} method was called.
*/
public int getCounter() {
return counter;
}
}