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) {
|
||||
|
||||
@@ -57,9 +57,6 @@
|
||||
</step>
|
||||
|
||||
<beans:bean id="tradeProcessor" class="org.springframework.batch.sample.domain.trade.internal.TradeProcessor"/>
|
||||
<beans:bean id="tradeWriter" class="org.springframework.batch.sample.domain.trade.internal.TradeWriter">
|
||||
<beans:property name="dao" ref="tradeDao"/>
|
||||
</beans:bean>
|
||||
|
||||
<beans:bean id="skipCheckingListener" class="org.springframework.batch.sample.common.SkipCheckingListener"/>
|
||||
|
||||
@@ -89,15 +86,6 @@
|
||||
|
||||
<beans:bean class="org.springframework.batch.core.scope.StepScope" />
|
||||
|
||||
<beans:bean id="tradeDao" class="org.springframework.batch.sample.domain.trade.internal.JdbcTradeDao">
|
||||
<beans:property name="dataSource" ref="dataSource"/>
|
||||
<beans:property name="incrementer">
|
||||
<beans:bean parent="incrementerParent">
|
||||
<beans:property name="incrementerName" value="TRADE_SEQ" />
|
||||
</beans:bean>
|
||||
</beans:property>
|
||||
</beans:bean>
|
||||
|
||||
<beans:bean id="tradeSqlItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
|
||||
<beans:property name="dataSource" ref="dataSource" />
|
||||
<beans:property name="sql" value="SELECT isin, quantity, price, customer from TRADE" />
|
||||
|
||||
@@ -40,6 +40,19 @@
|
||||
|
||||
<bean id="logAdvice" class="org.springframework.batch.sample.common.LogAdvice" />
|
||||
|
||||
<bean id="tradeWriter" class="org.springframework.batch.sample.domain.trade.internal.TradeWriter">
|
||||
<property name="dao">
|
||||
<bean class="org.springframework.batch.sample.domain.trade.internal.JdbcTradeDao">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="incrementer">
|
||||
<bean parent="incrementerParent">
|
||||
<property name="incrementerName" value="TRADE_SEQ" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="itemTrackingWriter" class="org.springframework.batch.sample.domain.trade.internal.ItemTrackingTradeItemWriter" />
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -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<String, Object> getJobExecution(long jobExecutionId) {
|
||||
|
||||
Reference in New Issue
Block a user