RESOLVED - BATCH-893: Remove the HibernateAwareItemWriter?
-removed HibernateAwareItemWriter and fixed sample -moved business logic from CustomerCreditIncreaseItemWriter into processor
This commit is contained in:
@@ -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<CustomerCredit, CustomerCredit> {
|
||||
|
||||
public static final BigDecimal FIXED_AMOUNT = new BigDecimal("1000");
|
||||
|
||||
public CustomerCredit process(CustomerCredit item) throws Exception {
|
||||
return item.increaseCreditBy(FIXED_AMOUNT);
|
||||
}
|
||||
}
|
||||
@@ -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<CustomerCredit> {
|
||||
|
||||
public static final BigDecimal FIXED_AMOUNT = new BigDecimal("1000");
|
||||
public class CustomerCreditItemWriter implements ItemWriter<CustomerCredit> {
|
||||
|
||||
private CustomerCreditDao customerCreditDao;
|
||||
|
||||
@@ -26,17 +23,9 @@ public class CustomerCreditIncreaseWriter implements ItemWriter<CustomerCredit>
|
||||
this.customerCreditDao = customerCreditDao;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.batch.item.processor.DelegatingItemWriter#doProcess
|
||||
* (java.lang.Object)
|
||||
*/
|
||||
public void write(List<? extends CustomerCredit> customerCredits) throws Exception {
|
||||
for (CustomerCredit customerCredit : customerCredits) {
|
||||
CustomerCredit result = customerCredit.increaseCreditBy(FIXED_AMOUNT);
|
||||
customerCreditDao.writeCredit(result);
|
||||
customerCreditDao.writeCredit(customerCredit);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<CustomerCredit>, InitializingBean {
|
||||
|
||||
private CustomerCreditDao dao;
|
||||
|
||||
private HibernateOperations hibernateTemplate;
|
||||
|
||||
public void write(List<? extends CustomerCredit> 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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
|
||||
@@ -12,29 +10,28 @@
|
||||
<description>Example for Hibernate integration.</description>
|
||||
|
||||
<bean id="hibernateJob" parent="simpleJob">
|
||||
<!-- set restartable=false so that this job can be used by more than one test -->
|
||||
<!--
|
||||
set restartable=false so that this job can be used by more than one
|
||||
test
|
||||
-->
|
||||
<property name="restartable" value="false" />
|
||||
<property name="steps">
|
||||
<bean id="step1" parent="skipLimitStep">
|
||||
<property name="skipLimit" value="5" />
|
||||
<property name="itemReader" ref="hibernateItemReader" />
|
||||
<property name="itemWriter" ref="hibernateItemWriter" />
|
||||
<property name="itemReader" ref="hibernateItemReader" />
|
||||
<property name="itemProcessor" ref="creditIncreaseProcessor" />
|
||||
<property name="itemWriter" ref="hibernateCreditWriter" />
|
||||
<property name="commitInterval" value="3" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
|
||||
<!-- This is a framework class that needs a delegate and also needs to be registered as a RepeatInterceptor in the chunk -->
|
||||
<bean id="hibernateItemWriter"
|
||||
class="org.springframework.batch.item.database.HibernateAwareItemWriter">
|
||||
<property name="sessionFactory" ref="sessionFactory" />
|
||||
<property name="delegate" ref="hibernateCreditWriter" />
|
||||
</bean>
|
||||
|
||||
<bean id="creditIncreaseProcessor" class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor" />
|
||||
|
||||
<bean id="hibernateCreditWriter"
|
||||
class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseWriter">
|
||||
<property name="customerCreditDao" ref="customerCreditDao" />
|
||||
class="org.springframework.batch.sample.domain.trade.internal.HibernateAwareCustomerCreditItemWriter">
|
||||
<property name="sessionFactory" ref="sessionFactory" />
|
||||
<property name="dao" ref="customerCreditDao" />
|
||||
</bean>
|
||||
|
||||
<bean id="customerCreditDao"
|
||||
|
||||
@@ -14,10 +14,13 @@
|
||||
<bean id="ibatisJob" parent="simpleJob">
|
||||
<property name="steps">
|
||||
<bean id="step1" parent="simpleStep">
|
||||
<property name="itemReader" ref="ibatisItemReader" />
|
||||
<property name="itemReader" ref="ibatisItemReader" />
|
||||
<property name="itemProcessor">
|
||||
<bean class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor" />
|
||||
</property>
|
||||
<property name="itemWriter">
|
||||
<bean
|
||||
class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseWriter">
|
||||
class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditItemWriter">
|
||||
<property name="customerCreditDao">
|
||||
<bean
|
||||
class="org.springframework.batch.sample.domain.trade.internal.IbatisCustomerCreditDao">
|
||||
|
||||
@@ -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)",
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user