Thanks for sharing that chudak.
I have a similar config:
Code:
<bean id="TestSimpleJob" class="org.springframework.batch.core.job.SimpleJob">
<property name="jobRepository" ref="test.springbatch.JobRepository" />
<property name="restartable" value="true"/>
<property name="steps">
<list>
<bean class="org.springframework.batch.core.step.item.SimpleStepFactoryBean">
<property name="jobRepository" ref="test.springbatch.JobRepository" />
<property name="transactionManager" ref="test.springbatch.TxManager" />
<property name="itemReader"><bean class="test.springbatch.TestItemReader"/></property>
<property name="itemWriter"><bean class="test.springbatch.TestItemWriter"/></property>
</bean>
</list>
</property>
</bean>
I changed my ItemReader and avoided the AbstractBufferedItemReaderItemStream class.
I now implement ItemStream and ItemReader.
Code:
package test.springbatch;
import (...);
public class TestItemReader implements ItemStream, ItemReader {
private static final List<Integer> NUMBERS = new ArrayList<Integer>();
private AtomicInteger mIncrementer = new AtomicInteger(0);
private int mCurrentItemCount;
static {
for (int i = 0; i < 100; i++) {
NUMBERS.add(i);
}
}
public void close(ExecutionContext pContext) throws ItemStreamException {}
public void mark() throws MarkFailedException {}
public void open(ExecutionContext pContext) throws ItemStreamException {
if (pContext.containsKey("read.count")) {
int oItemCount = Long.valueOf(pContext.getLong("read.count")).intValue();
try {
jumpToItem(oItemCount);
}
catch (Exception e) {
throw new ItemStreamException("Could not move to stored position on restart", e);
}
mCurrentItemCount = oItemCount;
} else {
System.out.println("Did not find read.count key in execution context.");
}
}
private void jumpToItem(int pItemCount) {
while (mIncrementer.getAndIncrement() < pItemCount) {}
}
public Object read() throws Exception, UnexpectedInputException, NoWorkFoundException, ParseException {
if (mIncrementer.get() >= NUMBERS.size()) return null;
mCurrentItemCount++;
return NUMBERS.get(mIncrementer.getAndIncrement());
}
public void reset() throws ResetFailedException {}
public void update(ExecutionContext pContext) throws ItemStreamException {
pContext.putLong("read.count", mCurrentItemCount);
}
}
I'm still having some weird/random behavior.
It would resume the failed job up to a certain point sometimes (all fine up to the 4th run) and then restart from "scratch" (from 0).
It's really frustrating...
chudak you must have some magic touch