RESOLVED - issue BATCH-581: Add filter capability to item oriented paradigm
http://jira.springframework.org/browse/BATCH-581 Modified the customerFilterJob to use the ResourceLineReader and processors.
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
*/
|
||||
package org.springframework.batch.sample.domain.trade;
|
||||
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
|
||||
import org.springframework.batch.item.file.mapping.FieldSet;
|
||||
import org.springframework.batch.item.file.transform.LineTokenizer;
|
||||
|
||||
@@ -15,10 +17,11 @@ import org.springframework.batch.item.file.transform.LineTokenizer;
|
||||
* @author Lucas Ward
|
||||
* @since 2.0
|
||||
*/
|
||||
public class CompositeCustomerUpdateLineTokenizer implements LineTokenizer {
|
||||
public class CompositeCustomerUpdateLineTokenizer extends StepExecutionListenerSupport implements LineTokenizer {
|
||||
|
||||
private LineTokenizer customerTokenizer;
|
||||
private LineTokenizer footerTokenizer;
|
||||
private StepExecution stepExecution;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.item.file.transform.LineTokenizer#tokenize(java.lang.String)
|
||||
@@ -27,17 +30,37 @@ public class CompositeCustomerUpdateLineTokenizer implements LineTokenizer {
|
||||
|
||||
if(line.charAt(0) == 'F'){
|
||||
//line starts with F, so the footer tokenizer should tokenize it.
|
||||
return footerTokenizer.process(line);
|
||||
FieldSet fs = footerTokenizer.process(line);
|
||||
long customerUpdateTotal = stepExecution.getReadCount();
|
||||
long fileUpdateTotal = fs.readLong(1);
|
||||
if(customerUpdateTotal != fileUpdateTotal){
|
||||
throw new IllegalStateException("The total number of customer updates in the file footer does not match the " +
|
||||
"number entered File footer total: [" + fileUpdateTotal + "] Total encountered during processing: [" +
|
||||
customerUpdateTotal + "]");
|
||||
}
|
||||
else{
|
||||
//return null, because the footer indicates an end of processing.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if(line.charAt(0) == 'A' || line.charAt(0) == 'U' || line.charAt(0) == 'D'){
|
||||
//line starts with A,U, or D, so it must be a customer operation.
|
||||
return customerTokenizer.process(line);
|
||||
}
|
||||
else if(line.charAt(0) == 'S'){
|
||||
//header record, ignore
|
||||
return null;
|
||||
}
|
||||
else{
|
||||
//If the line doesn't start with any of the characters above, it must obviously be invalid.
|
||||
throw new IllegalArgumentException("Invalid line encountered for tokenizing: " + line);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeStep(StepExecution stepExecution) {
|
||||
this.stepExecution = stepExecution;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link LineTokenizer} that will be used to tokenize any lines that begin with
|
||||
|
||||
@@ -5,8 +5,6 @@ package org.springframework.batch.sample.domain.trade;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
|
||||
import org.springframework.batch.item.file.mapping.FieldSet;
|
||||
import org.springframework.batch.item.file.mapping.FieldSetMapper;
|
||||
|
||||
@@ -16,37 +14,15 @@ import org.springframework.batch.item.file.mapping.FieldSetMapper;
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class CustomerUpdateFieldSetMapper extends StepExecutionListenerSupport implements FieldSetMapper<CustomerUpdate> {
|
||||
public class CustomerUpdateFieldSetMapper implements FieldSetMapper<CustomerUpdate> {
|
||||
|
||||
StepExecution stepExecution;
|
||||
|
||||
public CustomerUpdate process(FieldSet fs) {
|
||||
|
||||
char code = fs.readChar(0);
|
||||
if(code == 'F'){
|
||||
long customerUpdateTotal = stepExecution.getReadCount();
|
||||
long fileUpdateTotal = fs.readLong(1);
|
||||
if(customerUpdateTotal != fileUpdateTotal){
|
||||
throw new IllegalStateException("The total number of customer updates in the file footer does not match the " +
|
||||
"number entered File footer total: [" + fileUpdateTotal + "] Total encountered during processing: [" +
|
||||
customerUpdateTotal + "]");
|
||||
}
|
||||
else{
|
||||
//return null, because the footer indicates an end of processing.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
CustomerOperation operation = CustomerOperation.fromCode(code);
|
||||
CustomerOperation operation = CustomerOperation.fromCode(fs.readChar(0));
|
||||
String name = fs.readString(1);
|
||||
BigDecimal credit = fs.readBigDecimal(2);
|
||||
|
||||
return new CustomerUpdate(operation, name, credit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeStep(StepExecution stepExecution) {
|
||||
|
||||
this.stepExecution = stepExecution;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.util.List;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
|
||||
/**
|
||||
* @author Lucas Wward
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class CustomerUpdateWriter implements ItemWriter<CustomerUpdate> {
|
||||
@@ -24,6 +24,8 @@ public class CustomerUpdateWriter implements ItemWriter<CustomerUpdate> {
|
||||
customerDao.updateCustomer(customerUpdate.getCustomerName(), customerUpdate.getCredit());
|
||||
}
|
||||
}
|
||||
|
||||
//flush and/or clear resources
|
||||
}
|
||||
|
||||
public void setCustomerDao(CustomerDao customerDao) {
|
||||
|
||||
Reference in New Issue
Block a user