diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/TradeWriter.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/TradeWriter.java index 83bde85ab..b676b6639 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/TradeWriter.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/TradeWriter.java @@ -16,10 +16,14 @@ package org.springframework.batch.sample.domain.trade.internal; +import java.math.BigDecimal; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.ItemStreamException; +import org.springframework.batch.item.ItemStreamSupport; import org.springframework.batch.item.ItemWriter; import org.springframework.batch.sample.domain.trade.Trade; import org.springframework.batch.sample.domain.trade.TradeDao; @@ -28,15 +32,20 @@ import org.springframework.batch.sample.domain.trade.TradeDao; * Delegates the actual writing to custom DAO delegate. Allows configurable * exception raising for testing skip and restart. */ -public class TradeWriter implements ItemWriter { +public class TradeWriter extends ItemStreamSupport implements ItemWriter { + private static Log log = LogFactory.getLog(TradeWriter.class); + private static final String TOTAL_AMOUNT_KEY = "TOTAL_AMOUNT"; + private TradeDao dao; private int failure = -1; private int index = 0; + private BigDecimal totalPrice = BigDecimal.ZERO; + /** * Public setter for the the index on which failure should occur. * @@ -48,18 +57,43 @@ public class TradeWriter implements ItemWriter { public void write(List trades) { + BigDecimal amount = BigDecimal.ZERO; + for (Trade trade : trades) { log.debug(trade); - + dao.writeTrade(trade); - + + amount = amount.add(trade.getPrice()); + if (index++ == failure) { throw new RuntimeException("Something unexpected happened!"); } - } + + this.totalPrice = this.totalPrice.add(amount); + + } + @Override + public void open(ExecutionContext executionContext) throws ItemStreamException { + if (executionContext.containsKey(TOTAL_AMOUNT_KEY)) { + this.totalPrice = (BigDecimal) executionContext.get(TOTAL_AMOUNT_KEY); + } + else + { + this.totalPrice = BigDecimal.ZERO; + } + } + + @Override + public void update(ExecutionContext executionContext) { + executionContext.put(TOTAL_AMOUNT_KEY, this.totalPrice); + } + + public BigDecimal getTotalPrice() { + return totalPrice; } public void setDao(TradeDao dao) { diff --git a/spring-batch-samples/src/main/resources/jobs/skipSampleJob.xml b/spring-batch-samples/src/main/resources/jobs/skipSampleJob.xml index 048d8852c..231b4a097 100644 --- a/spring-batch-samples/src/main/resources/jobs/skipSampleJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/skipSampleJob.xml @@ -57,9 +57,6 @@ - - - @@ -89,15 +86,6 @@ - - - - - - - - - diff --git a/spring-batch-samples/src/main/resources/skipSample-job-launcher-context.xml b/spring-batch-samples/src/main/resources/skipSample-job-launcher-context.xml index a6e266da0..8ab01bb8e 100644 --- a/spring-batch-samples/src/main/resources/skipSample-job-launcher-context.xml +++ b/spring-batch-samples/src/main/resources/skipSample-job-launcher-context.xml @@ -40,6 +40,19 @@ + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java index 4ff55ec23..2ed8235f9 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java @@ -3,6 +3,7 @@ package org.springframework.batch.sample; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.math.BigDecimal; import java.util.Map; import javax.sql.DataSource; @@ -17,6 +18,7 @@ import org.springframework.batch.core.repository.JobExecutionAlreadyRunningExcep import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; import org.springframework.batch.core.repository.JobRestartException; import org.springframework.batch.sample.domain.trade.internal.ItemTrackingTradeItemWriter; +import org.springframework.batch.sample.domain.trade.internal.TradeWriter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.test.context.ContextConfiguration; @@ -40,6 +42,9 @@ public class SkipSampleFunctionalTests { @Autowired private JobOperator jobOperator; + @Autowired + private TradeWriter tradeWriter; + @Autowired private ItemTrackingTradeItemWriter itemTrackingWriter; @@ -169,6 +174,8 @@ public class SkipSampleFunctionalTests { assertEquals(1, simpleJdbcTemplate.queryForInt( "SELECT Count(*) from ERROR_LOG where JOB_NAME = ? and STEP_NAME = ?", "skipJob", "step" + i)); } + + assertEquals(new BigDecimal("252.63"), tradeWriter.getTotalPrice()); } private void validateLaunchWithoutSkips() { @@ -180,6 +187,8 @@ public class SkipSampleFunctionalTests { // Neither step contained skips assertEquals(0, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "ERROR_LOG")); + + assertEquals(new BigDecimal("270.75"), tradeWriter.getTotalPrice()); } private Map getJobExecution(long jobExecutionId) {