Add sample for jdbc paging reader

This commit is contained in:
dsyer
2009-08-22 11:20:38 +00:00
parent 679a3dbcac
commit 150fff9f41
6 changed files with 270 additions and 6 deletions

View File

@@ -33,7 +33,7 @@
<property name="resource" value="#{jobParameters[fileName]}" />
</bean>
<bean id="itemReaderForTest" parent="itemReaderParent" >
<bean id="itemReaderForTest" parent="itemReaderParent" scope="prototype" >
<property name="resource" value="data/iosample/input/delimited.csv" />
</bean>

View File

@@ -0,0 +1,61 @@
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="itemReaderForTest" class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="dataSource"/>
<property name="sql" value="select ID, NAME, CREDIT from CUSTOMER"/>
<property name="rowMapper">
<bean class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper"/>
</property>
</bean>
<bean id="itemReader"
class="org.springframework.batch.item.database.JdbcPagingItemReader" autowire-candidate="false"
scope="step">
<property name="dataSource" ref="dataSource" />
<property name="rowMapper">
<bean
class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper" />
</property>
<property name="queryProvider">
<bean
class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="sortKey" value="ID" />
<property name="selectClause" value="select ID, NAME, CREDIT"/>
<property name="fromClause" value="FROM CUSTOMER" />
<property name="whereClause"
value="WHERE CREDIT > :credit" />
</bean>
</property>
<property name="pageSize" value="10" />
<property name="parameterValues">
<map>
<entry key="statusCode" value="PE" />
<entry key="credit" value="#{jobParameters[credit]}" />
<entry key="type" value="COLLECTION" />
</map>
</property>
</bean>
<bean id="itemWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="assertUpdates" value="true" />
<property name="itemSqlParameterSourceProvider">
<bean
class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
</property>
<property name="sql"
value="UPDATE customer set credit = :credit where id = :id" />
<property name="dataSource" ref="dataSource" />
</bean>
</beans>

View File

@@ -1,3 +1,6 @@
log4j.rootLogger=info, stdout
# log4j.rootLogger=info, stdout, chainsaw
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
@@ -14,9 +17,6 @@ log4j.appender.chainsaw.layout=org.apache.log4j.xml.XMLLayout
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
# log4j.rootLogger=info, stdout, chainsaw
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
@@ -30,5 +30,3 @@ log4j.rootLogger=info, stdout
### debug your specific package or classes with the following example
log4j.logger.org.springframework.batch=debug
log4j.logger.org.springframework.batch.sample=debug
log4j.logger.org.springframework.batch.sample.module.OrderDataProvider=debug
log4j.logger.org.springframework.batch.container.common.module.process.support.DefaultXmlDataProvider=debug

View File

@@ -0,0 +1,46 @@
/*
* 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.iosample;
import org.junit.runner.RunWith;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dan Garrette
* @since 2.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
"/jobs/iosample/jdbcPaging.xml" })
public class JdbcPagingFunctionalTests extends AbstractIoSampleTests {
@Override
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
// no-op
}
@Override
protected JobParameters getUniqueJobParameters() {
return new JobParametersBuilder(super.getUniqueJobParameters()).addDouble("credit", 10000.).toJobParameters();
}
}

View File

@@ -0,0 +1,94 @@
/*
* 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.iosample;
import static org.junit.Assert.assertEquals;
import java.util.Date;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dave Syer
* @since 2.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
"/jobs/iosample/delimited.xml" })
public class TwoJobInstancesDelimitedFunctionalTests {
@Autowired
private JobLauncher launcher;
@Autowired
private AbstractJob job;
@Autowired
private FlatFileItemReader<CustomerCredit> reader;
@Autowired
private Resource outputResource;
@Test
public void testLaunchJobTwice() throws Exception {
JobExecution jobExecution = launcher.run(this.job, getJobParameters("data/iosample/input/delimited.csv"));
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
verifyOutput(6);
jobExecution = launcher.run(this.job, getJobParameters("data/iosample/input/delimited2.csv"));
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
verifyOutput(2);
}
private void verifyOutput(int expected) throws Exception {
reader.setResource(outputResource);
reader.open(new ExecutionContext());
int count = 0;
try {
while (reader.read() != null) {
count++;
}
}
finally {
reader.close();
}
assertEquals(expected, count);
}
protected JobParameters getJobParameters(String fileName) {
return new JobParametersBuilder().addLong("timestamp", new Date().getTime()).addString("fileName", fileName)
.toJobParameters();
}
}

View File

@@ -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.iosample;
import static org.junit.Assert.assertEquals;
import java.util.Date;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dave Syer
* @since 2.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
"/jobs/iosample/jdbcPaging.xml" })
public class TwoJobInstancesPagingFunctionalTests {
@Autowired
private JobLauncher launcher;
@Autowired
private AbstractJob job;
@Test
public void testLaunchJobTwice() throws Exception {
JobExecution jobExecution = launcher.run(this.job, getJobParameters(1000.));
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
assertEquals(4, jobExecution.getStepExecutions().iterator().next().getWriteCount());
jobExecution = launcher.run(this.job, getJobParameters(1000000.));
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
assertEquals(0, jobExecution.getStepExecutions().iterator().next().getWriteCount());
}
protected JobParameters getJobParameters(double amount) {
return new JobParametersBuilder().addLong("timestamp", new Date().getTime()).addDouble("credit", amount)
.toJobParameters();
}
}