RESOLVED - issue BATCH-76: ItemWriter for SQL batch updates
http://jira.springframework.org/browse/BATCH-76
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package org.springframework.batch.sample.item.writer;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.springframework.batch.io.support.BatchSqlUpdateItemWriter;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.exception.ClearFailedException;
|
||||
import org.springframework.batch.item.exception.FlushFailedException;
|
||||
import org.springframework.batch.sample.domain.CustomerCredit;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Increases customer's credit by fixed amount, delegating to a
|
||||
* {@link BatchSqlUpdateItemWriter} to push the result out to persistent
|
||||
* storage.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class BatchSqlCustomerCreditIncreaseWriter implements ItemWriter, InitializingBean {
|
||||
|
||||
private ItemWriter delegate;
|
||||
|
||||
public static final BigDecimal FIXED_AMOUNT = new BigDecimal(1000);
|
||||
|
||||
/**
|
||||
* Public setter for the {@link ItemWriter}, which must be an instance of
|
||||
* {@link BatchSqlUpdateItemWriter}.
|
||||
* @param delegate the delegate to set
|
||||
*/
|
||||
public void setDelegate(ItemWriter delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.state(delegate instanceof BatchSqlUpdateItemWriter, "Delegate must be set and must be an instance of BatchSqlUpdateItemWriter");
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.batch.item.processor.DelegatingItemWriter#doProcess(java.lang.Object)
|
||||
*/
|
||||
public void write(Object data) throws Exception {
|
||||
CustomerCredit customerCredit = (CustomerCredit) data;
|
||||
customerCredit.increaseCreditBy(FIXED_AMOUNT);
|
||||
delegate.write(customerCredit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ClearFailedException
|
||||
* @see org.springframework.batch.io.support.BatchSqlUpdateItemWriter#clear()
|
||||
*/
|
||||
public void clear() throws ClearFailedException {
|
||||
delegate.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws FlushFailedException
|
||||
* @see org.springframework.batch.io.support.BatchSqlUpdateItemWriter#flush()
|
||||
*/
|
||||
public void flush() throws FlushFailedException {
|
||||
delegate.flush();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2006-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.sample.item.writer;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.batch.io.support.ItemPreparedStatementSetter;
|
||||
import org.springframework.batch.sample.domain.CustomerCredit;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class CustomerCreditUpdatePreparedStatementSetter implements ItemPreparedStatementSetter {
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.io.support.ItemPreparedStatementSetter#setValues(java.lang.Object, java.sql.PreparedStatement)
|
||||
*/
|
||||
public void setValues(Object item, PreparedStatement ps) throws SQLException {
|
||||
CustomerCredit customerCredit = (CustomerCredit) item;
|
||||
ps.setBigDecimal(1, customerCredit.getCredit());
|
||||
ps.setLong(2, customerCredit.getId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,12 +8,14 @@ import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
public class CustomerCreditRowMapper implements RowMapper {
|
||||
|
||||
public static final String ID_COLUMN = "id";
|
||||
public static final String NAME_COLUMN = "name";
|
||||
public static final String CREDIT_COLUMN = "credit";
|
||||
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
CustomerCredit customerCredit = new CustomerCredit();
|
||||
|
||||
customerCredit.setId(rs.getInt(ID_COLUMN));
|
||||
customerCredit.setName(rs.getString(NAME_COLUMN));
|
||||
customerCredit.setCredit(rs.getBigDecimal(CREDIT_COLUMN));
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?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"
|
||||
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
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
|
||||
|
||||
<description>Example for SQL Batch Update integration.</description>
|
||||
|
||||
<bean id="batchUpdateJob" parent="simpleJob">
|
||||
<!-- 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="simpleStep">
|
||||
<property name="skipLimit" value="5" />
|
||||
<property name="itemReader" ref="itemReader" />
|
||||
<property name="itemWriter" ref="itemWriter" />
|
||||
<property name="commitInterval" value="3" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="itemWriter" class="org.springframework.batch.sample.item.writer.BatchSqlCustomerCreditIncreaseWriter">
|
||||
<property name="delegate">
|
||||
<bean class="org.springframework.batch.io.support.BatchSqlUpdateItemWriter">
|
||||
<property name="jdbcTemplate" ref="jdbcTemplate" />
|
||||
<property name="sql"><value><![CDATA[UPDATE CUSTOMER SET CREDIT=? WHERE ID=?]]></value></property>
|
||||
<property name="itemPreparedStatementSetter">
|
||||
<bean class="org.springframework.batch.sample.item.writer.CustomerCreditUpdatePreparedStatementSetter"/>
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="itemReader" class="org.springframework.batch.io.cursor.JdbcCursorItemReader">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="sql" value="SELECT id, name, credit FROM customer " />
|
||||
<property name="mapper">
|
||||
<bean class="org.springframework.batch.sample.mapping.CustomerCreditRowMapper" />
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -86,6 +86,4 @@
|
||||
<bean id="fieldSetMapper"
|
||||
class="org.springframework.batch.sample.mapping.TradeFieldSetMapper" />
|
||||
|
||||
<bean parent="customEditorConfigurer" />
|
||||
|
||||
</beans>
|
||||
@@ -122,8 +122,6 @@
|
||||
<bean id="fieldSetMapper"
|
||||
class="org.springframework.batch.sample.mapping.TradeFieldSetMapper" />
|
||||
|
||||
<bean parent="customEditorConfigurer" />
|
||||
|
||||
<aop:config>
|
||||
<aop:aspect id="moduleLogging" ref="logAdvice">
|
||||
<aop:after
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
/**
|
||||
* Test for BatchSqlUpdateJob - checks that customer credit has been updated to expected value.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class BatchSqlUpdateJobFunctionalTests extends AbstractCustomerCreditIncreaseTests {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2006-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.sample.item.writer;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.sample.domain.CustomerCredit;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class BatchSqlCustomerCreditIncreaseWriterTests extends TestCase {
|
||||
|
||||
private BatchSqlCustomerCreditIncreaseWriter writer = new BatchSqlCustomerCreditIncreaseWriter();
|
||||
|
||||
private ItemWriter delegate;
|
||||
|
||||
private MockControl control = MockControl.createControl(ItemWriter.class);
|
||||
|
||||
private CustomerCredit customerCredit;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see junit.framework.TestCase#setUp()
|
||||
*/
|
||||
protected void setUp() throws Exception {
|
||||
delegate = (ItemWriter) control.getMock();
|
||||
writer.setDelegate(delegate);
|
||||
customerCredit = new CustomerCredit();
|
||||
customerCredit.setId(13);
|
||||
customerCredit.setCredit(new BigDecimal(1000));
|
||||
customerCredit.setName("foo");
|
||||
}
|
||||
|
||||
public void testAfterPropertiesSet() throws Exception {
|
||||
try {
|
||||
writer.afterPropertiesSet();
|
||||
fail("Expected IllegalStateException");
|
||||
} catch (IllegalStateException e) {
|
||||
// expected: wrong class
|
||||
String message = e.getMessage();
|
||||
assertTrue("Message does not contain 'instance'"+message, message.indexOf("instance")>=0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.sample.item.writer.BatchSqlCustomerCreditIncreaseWriter#write(java.lang.Object)}.
|
||||
* @throws Exception
|
||||
*/
|
||||
public void testWrite() throws Exception {
|
||||
delegate.write(customerCredit);
|
||||
control.setVoidCallable();
|
||||
control.replay();
|
||||
writer.write(customerCredit);
|
||||
control.verify();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.sample.item.writer.BatchSqlCustomerCreditIncreaseWriter#clear()}.
|
||||
*/
|
||||
public void testClear() {
|
||||
delegate.clear();
|
||||
control.setVoidCallable();
|
||||
control.replay();
|
||||
writer.clear();
|
||||
control.verify();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.sample.item.writer.BatchSqlCustomerCreditIncreaseWriter#flush()}.
|
||||
*/
|
||||
public void testFlush() {
|
||||
delegate.flush();
|
||||
control.setVoidCallable();
|
||||
control.replay();
|
||||
writer.flush();
|
||||
control.verify();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2006-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.sample.item.writer;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.sample.domain.CustomerCredit;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class CustomerCreditUpdatePreparedStatementSetterTests extends TestCase {
|
||||
|
||||
private CustomerCreditUpdatePreparedStatementSetter setter = new CustomerCreditUpdatePreparedStatementSetter();
|
||||
|
||||
private CustomerCredit credit;
|
||||
|
||||
private PreparedStatement ps;
|
||||
|
||||
private MockControl control = MockControl.createControl(PreparedStatement.class);
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see junit.framework.TestCase#setUp()
|
||||
*/
|
||||
protected void setUp() throws Exception {
|
||||
ps = (PreparedStatement) control.getMock();
|
||||
credit = new CustomerCredit();
|
||||
credit.setId(13);
|
||||
credit.setCredit(new BigDecimal(12000));
|
||||
credit.setName("foo");
|
||||
}
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.sample.item.writer.CustomerCreditUpdatePreparedStatementSetter#setValues(java.lang.Object, java.sql.PreparedStatement)}.
|
||||
* @throws SQLException
|
||||
*/
|
||||
public void testSetValues() throws SQLException {
|
||||
ps.setBigDecimal(1, credit.getCredit());
|
||||
control.setVoidCallable();
|
||||
ps.setLong(2, credit.getId());
|
||||
control.setVoidCallable();
|
||||
control.replay();
|
||||
setter.setValues(credit, ps);
|
||||
control.verify();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,11 +11,16 @@ import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
public class CustomerCreditRowMapperTests extends AbstractRowMapperTests {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final int ID = 12;
|
||||
private static final String CUSTOMER = "Jozef Mak";
|
||||
private static final BigDecimal CREDIT = new BigDecimal(0.1);
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
CustomerCredit credit = new CustomerCredit();
|
||||
credit.setId(ID);
|
||||
credit.setCredit(CREDIT);
|
||||
credit.setName(CUSTOMER);
|
||||
return credit;
|
||||
@@ -26,6 +31,8 @@ public class CustomerCreditRowMapperTests extends AbstractRowMapperTests {
|
||||
}
|
||||
|
||||
protected void setUpResultSetMock(ResultSet rs, MockControl rsControl) throws SQLException {
|
||||
rs.getInt(CustomerCreditRowMapper.ID_COLUMN);
|
||||
rsControl.setReturnValue(ID);
|
||||
rs.getString(CustomerCreditRowMapper.NAME_COLUMN);
|
||||
rsControl.setReturnValue(CUSTOMER);
|
||||
rs.getBigDecimal(CustomerCreditRowMapper.CREDIT_COLUMN);
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
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.0.xsd">
|
||||
|
||||
<import resource="classpath:/simple-job-launcher-context.xml" />
|
||||
<import resource="classpath:/jobs/batchUpdateJob.xml" />
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user