BATCH-1100: moved state data update to @AfterWrite method

This commit is contained in:
dhgarrette
2009-02-24 19:01:13 +00:00
parent 0a49194ad4
commit e0c30c26c1
3 changed files with 12 additions and 11 deletions

View File

@@ -22,12 +22,14 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.annotation.AfterWrite;
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;
import org.springframework.util.Assert;
/**
* Delegates the actual writing to custom DAO delegate. Allows configurable
@@ -47,23 +49,26 @@ public class TradeWriter extends ItemStreamSupport implements ItemWriter<Trade>
public void write(List<? extends Trade> trades) {
BigDecimal amount = BigDecimal.ZERO;
for (Trade trade : trades) {
log.debug(trade);
dao.writeTrade(trade);
amount = amount.add(trade.getPrice());
Assert.notNull(trade.getPrice()); // There must be a price to total
if (this.failingCustomers.contains(trade.getCustomer())) {
throw new RuntimeException("Something unexpected happened!");
}
}
this.totalPrice = this.totalPrice.add(amount);
}
@AfterWrite
public void updateTotalPrice(List<Trade> trades) {
for (Trade trade : trades) {
this.totalPrice = this.totalPrice.add(trade.getPrice());
}
}
@Override

View File

@@ -38,7 +38,8 @@
<listeners>
<listener ref="skipCheckingListener"/>
<listener ref="promotionListener"/>
<listener ref="promotionListener"/>
<listener ref="tradeWriter"/>
</listeners>
</step>

View File

@@ -24,7 +24,6 @@ import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
import org.springframework.transaction.annotation.Transactional;
/**
* Error is encountered during writing - transaction is rolled back and the
@@ -126,7 +125,6 @@ public class SkipSampleFunctionalTests {
* <li>This step does not occur. No error records are logged.
* </ul>
*/
@Transactional
@Test
public void testJobIncrementing() {
//
@@ -136,10 +134,7 @@ public class SkipSampleFunctionalTests {
Map<String, Object> execution1 = this.getJobExecution(id1);
assertEquals("COMPLETED", execution1.get("STATUS"));
//
// TODO: Uncomment this!
//
// this.validateLaunchWithSkips();
this.validateLaunchWithSkips();
//
// Clear the data