diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditIncreaseProcessor.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditIncreaseProcessor.java new file mode 100644 index 000000000..7c686cfa1 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditIncreaseProcessor.java @@ -0,0 +1,20 @@ +package org.springframework.batch.sample.domain.trade.internal; + +import java.math.BigDecimal; + +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.sample.domain.trade.CustomerCredit; + +/** + * Increases customer's credit by a fixed amount. + * + * @author Robert Kasanicky + */ +public class CustomerCreditIncreaseProcessor implements ItemProcessor { + + public static final BigDecimal FIXED_AMOUNT = new BigDecimal("1000"); + + public CustomerCredit process(CustomerCredit item) throws Exception { + return item.increaseCreditBy(FIXED_AMOUNT); + } +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditIncreaseWriter.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditItemWriter.java similarity index 61% rename from spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditIncreaseWriter.java rename to spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditItemWriter.java index a0de507f5..7005eede0 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditIncreaseWriter.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditItemWriter.java @@ -1,6 +1,5 @@ package org.springframework.batch.sample.domain.trade.internal; -import java.math.BigDecimal; import java.util.List; import org.springframework.batch.item.ItemWriter; @@ -8,13 +7,11 @@ import org.springframework.batch.sample.domain.trade.CustomerCredit; import org.springframework.batch.sample.domain.trade.CustomerCreditDao; /** - * Increases customer's credit by fixed amount. + * Delegates actual writing to a custom DAO. * * @author Robert Kasanicky */ -public class CustomerCreditIncreaseWriter implements ItemWriter { - - public static final BigDecimal FIXED_AMOUNT = new BigDecimal("1000"); +public class CustomerCreditItemWriter implements ItemWriter { private CustomerCreditDao customerCreditDao; @@ -26,17 +23,9 @@ public class CustomerCreditIncreaseWriter implements ItemWriter this.customerCreditDao = customerCreditDao; } - /* - * (non-Javadoc) - * - * @see - * org.springframework.batch.item.processor.DelegatingItemWriter#doProcess - * (java.lang.Object) - */ public void write(List customerCredits) throws Exception { for (CustomerCredit customerCredit : customerCredits) { - CustomerCredit result = customerCredit.increaseCreditBy(FIXED_AMOUNT); - customerCreditDao.writeCredit(result); + customerCreditDao.writeCredit(customerCredit); } } diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/HibernateAwareCustomerCreditItemWriter.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/HibernateAwareCustomerCreditItemWriter.java new file mode 100644 index 000000000..1b17405fc --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/domain/trade/internal/HibernateAwareCustomerCreditItemWriter.java @@ -0,0 +1,54 @@ +package org.springframework.batch.sample.domain.trade.internal; + +import java.util.List; + +import org.hibernate.SessionFactory; +import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.sample.domain.trade.CustomerCredit; +import org.springframework.batch.sample.domain.trade.CustomerCreditDao; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.orm.hibernate3.HibernateOperations; +import org.springframework.orm.hibernate3.HibernateTemplate; +import org.springframework.util.Assert; + +/** + * Delegates writing to a custom DAO and flushes + clears hibernate session to + * fulfill the {@link ItemWriter} contract. + * + * @author Robert Kasanicky + */ +public class HibernateAwareCustomerCreditItemWriter implements ItemWriter, InitializingBean { + + private CustomerCreditDao dao; + + private HibernateOperations hibernateTemplate; + + public void write(List items) throws Exception { + for (CustomerCredit credit : items) { + dao.writeCredit(credit); + } + try { + hibernateTemplate.flush(); + } + finally { + // this should happen automatically on commit, but to be on the safe + // side... + hibernateTemplate.clear(); + } + + } + + public void setDao(CustomerCreditDao dao) { + this.dao = dao; + } + + public void setSessionFactory(SessionFactory sessionFactory) { + this.hibernateTemplate = new HibernateTemplate(sessionFactory); + } + + public void afterPropertiesSet() throws Exception { + Assert.notNull(hibernateTemplate, "Hibernate session factory must be set"); + Assert.notNull(dao, "Delegate DAO must be set"); + } + +} diff --git a/spring-batch-samples/src/main/resources/jobs/hibernateJob.xml b/spring-batch-samples/src/main/resources/jobs/hibernateJob.xml index a8b021daf..6cb9894ae 100644 --- a/spring-batch-samples/src/main/resources/jobs/hibernateJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/hibernateJob.xml @@ -1,9 +1,7 @@ - + - - + + + - - - - - - - + + - + class="org.springframework.batch.sample.domain.trade.internal.HibernateAwareCustomerCreditItemWriter"> + + - + + + + + class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditItemWriter"> diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/AbstractCustomerCreditIncreaseTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/AbstractCustomerCreditIncreaseTests.java index 9fd9eb5b9..0024f57bb 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/AbstractCustomerCreditIncreaseTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/AbstractCustomerCreditIncreaseTests.java @@ -10,11 +10,11 @@ import java.util.List; import javax.sql.DataSource; -import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseWriter; +import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.RowCallbackHandler; -import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.jdbc.core.simple.ParameterizedRowMapper; +import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallback; @@ -33,7 +33,7 @@ public abstract class AbstractCustomerCreditIncreaseTests extends AbstractValida protected PlatformTransactionManager transactionManager; - private static final BigDecimal CREDIT_INCREASE = CustomerCreditIncreaseWriter.FIXED_AMOUNT; + private static final BigDecimal CREDIT_INCREASE = CustomerCreditIncreaseProcessor.FIXED_AMOUNT; private static String[] customers = { "INSERT INTO customer (id, version, name, credit) VALUES (1, 0, 'customer1', 100000)", "INSERT INTO customer (id, version, name, credit) VALUES (2, 0, 'customer2', 100000)", diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditIncreaseProcessorTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditIncreaseProcessorTests.java index 74a00ae4a..af9832f80 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditIncreaseProcessorTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/domain/trade/internal/CustomerCreditIncreaseProcessorTests.java @@ -1,22 +1,20 @@ package org.springframework.batch.sample.domain.trade.internal; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; import java.math.BigDecimal; -import java.util.Collections; import org.junit.Test; import org.springframework.batch.sample.domain.trade.CustomerCredit; -import org.springframework.batch.sample.domain.trade.CustomerCreditDao; /** - * Tests for {@link CustomerCreditIncreaseWriter}. + * Tests for {@link CustomerCreditItemWriter}. * * @author Robert Kasanicky */ public class CustomerCreditIncreaseProcessorTests { - private CustomerCreditIncreaseWriter writer = new CustomerCreditIncreaseWriter(); + private CustomerCreditIncreaseProcessor tested = new CustomerCreditIncreaseProcessor(); /* * Increases customer's credit by fixed value @@ -25,23 +23,9 @@ public class CustomerCreditIncreaseProcessorTests { public void testProcess() throws Exception { final BigDecimal oldCredit = new BigDecimal(10.54); - class CustomerDaoStub implements CustomerCreditDao { - - public void writeCredit(CustomerCredit customerCredit) throws Exception { - BigDecimal expectedCredit = oldCredit.add(CustomerCreditIncreaseWriter.FIXED_AMOUNT); - assertTrue(customerCredit.getCredit().compareTo(expectedCredit) == 0); - - } - - } CustomerCredit customerCredit = new CustomerCredit(); - customerCredit.setId(1); - customerCredit.setName("testCustomer"); - writer.setCustomerCreditDao(new CustomerDaoStub()); - customerCredit.setCredit(oldCredit); - - writer.write(Collections.singletonList(customerCredit)); - + + assertEquals(oldCredit.add(CustomerCreditIncreaseProcessor.FIXED_AMOUNT),tested.process(customerCredit).getCredit()); } }