diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CompositeCustomerUpdateLineTokenizer.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CompositeCustomerUpdateLineTokenizer.java index 182a38ffe..7f82c26c1 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CompositeCustomerUpdateLineTokenizer.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CompositeCustomerUpdateLineTokenizer.java @@ -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 diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CustomerUpdateFieldSetMapper.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CustomerUpdateFieldSetMapper.java index 24368b58c..29d4bd019 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CustomerUpdateFieldSetMapper.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CustomerUpdateFieldSetMapper.java @@ -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 { +public class CustomerUpdateFieldSetMapper implements FieldSetMapper { - 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; - } } diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CustomerUpdateWriter.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CustomerUpdateWriter.java index ed8ebc819..432cc9ce0 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CustomerUpdateWriter.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/CustomerUpdateWriter.java @@ -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 { @@ -24,6 +24,8 @@ public class CustomerUpdateWriter implements ItemWriter { customerDao.updateCustomer(customerUpdate.getCustomerName(), customerUpdate.getCredit()); } } + + //flush and/or clear resources } public void setCustomerDao(CustomerDao customerDao) { diff --git a/spring-batch-samples/src/main/resources/jobs/customerFilterJob.xml b/spring-batch-samples/src/main/resources/jobs/customerFilterJob.xml index 8ef3362a4..67946e0bc 100644 --- a/spring-batch-samples/src/main/resources/jobs/customerFilterJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/customerFilterJob.xml @@ -11,10 +11,13 @@ - - - - + + + + + + + @@ -23,17 +26,14 @@ - + - - + + - - - @@ -51,6 +51,13 @@ + + + + + + + diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/trade/CompositeCustomerUpdateLineTokenizerTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/trade/CompositeCustomerUpdateLineTokenizerTests.java index a53d1ce68..a06f362f7 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/trade/CompositeCustomerUpdateLineTokenizerTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/trade/CompositeCustomerUpdateLineTokenizerTests.java @@ -7,6 +7,8 @@ import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.StepExecution; import org.springframework.batch.item.file.mapping.DefaultFieldSet; import org.springframework.batch.item.file.mapping.FieldSet; import org.springframework.batch.item.file.transform.LineTokenizer; @@ -32,15 +34,6 @@ public class CompositeCustomerUpdateLineTokenizerTests { compositeTokenizer.setFooterTokenizer(footerTokenizer); } - @Test - public void testFooter() throws Exception{ - - String footerLine = "Ffjkdalsfjdaskl;f"; - FieldSet fs = compositeTokenizer.process(footerLine); - assertEquals(footerFieldSet, fs); - assertEquals(footerLine, footerTokenizer.getTokenizedLine()); - } - @Test public void testCustomerAdd() throws Exception{