Java Reference
In-Depth Information
processing. It allows you to commit the work you've successfully completed and pick up where you left
off when you restart.
By default, Spring Batch considers a step and job failed when any exception is thrown. You can see
this in action by tweaking TransactionReader as shown in Listing 6-35. In this case, you throw a
org.springframework.batch.item.ParseException after reading 510 records, stopping the job in a Failed
status.
Listing 6-35. Transaction Reader Set Up to Throw an Exception
package com.apress.springbatch.chapter6;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.AfterStep;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.file.transform.FieldSet;
public class TransactionReader implements ItemReader<Object> {
private ItemReader<FieldSet> fieldSetReader;
private int recordCount = 0;
public Object read() throws Exception, UnexpectedInputException,
ParseException, NonTransientResourceException {
if(recordCount == 510) {
throw new ParseException("This isn't what I hoped to happen");
}
Transaction record = process(fieldSetReader.read());
return record;
}
private Transaction process(FieldSet fieldSet) {
Transaction result = null;
result = new Transaction();
result.setAccountNumber(fieldSet.readString(0));
result.setTimestamp(fieldSet.readDate(1, "yyyy-MM-DD HH:mm:ss"));
result.setAmount(fieldSet.readDouble(2));
recordCount++;
return result;
}
public void setFieldSetReader(ItemReader<FieldSet> fieldSetReader) {
this.fieldSetReader = fieldSetReader;
 
Search WWH ::




Custom Search