BATCH-1099: added totalAmount variable to SkipSample
This commit is contained in:
@@ -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<Trade> {
|
||||
public class TradeWriter extends ItemStreamSupport implements ItemWriter<Trade> {
|
||||
|
||||
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<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());
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user