RESOLVED - issue BATCH-1420: Late Binding only happens first time when using inner bean definition with collection property

This commit is contained in:
dsyer
2009-10-16 19:15:17 +00:00
parent db1351f7ab
commit 232d739dbd
16 changed files with 585 additions and 30 deletions

View File

@@ -0,0 +1,93 @@
package org.springframework.batch.sample.common;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.batch.core.partition.support.Partitioner;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
/**
* Simple minded partitioner for a range of values of a column in a database
* table. Works best if the values are uniformly distributed (e.g.
* auto-generated primary key values).
*
* @author Dave Syer
*
*/
public class ColumnRangePartitioner implements Partitioner {
private SimpleJdbcTemplate jdbcTemplate;
private String table;
private String column;
/**
* The name of the SQL table the data are in.
*
* @param table the name of the table
*/
public void setTable(String table) {
this.table = table;
}
/**
* The name of the column to partition.
*
* @param column the column name.
*/
public void setColumn(String column) {
this.column = column;
}
/**
* The data source for connecting to the database.
*
* @param dataSource a {@link DataSource}
*/
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
/**
* Partition a database table assuming that the data in the column specified
* are uniformly distributed. The execution context values will have keys
* <code>minValue</code> and <code>maxValue</code> specifying the range of
* values to consider in each partition.
*
* @see Partitioner#partition(int)
*/
public Map<String, ExecutionContext> partition(int gridSize) {
int min = jdbcTemplate.queryForInt("SELECT MIN(" + column + ") from " + table);
int max = jdbcTemplate.queryForInt("SELECT MAX(" + column + ") from " + table);
int targetSize = (max - min) / gridSize + 1;
Map<String, ExecutionContext> result = new HashMap<String, ExecutionContext>();
int number = 0;
int start = min;
int end = start + targetSize - 1;
while (start <= max) {
ExecutionContext value = new ExecutionContext();
result.put("partition" + number, value);
if (end >= max) {
end = max;
}
value.putInt("minValue", start);
value.putInt("maxValue", end);
start += targetSize;
end += targetSize;
number++;
}
return result;
}
}

View File

@@ -30,6 +30,12 @@ public class OutputFileListener {
private String inputKeyName = "fileName";
private String path = "file:./target/output/";
public void setPath(String path) {
this.path = path;
}
public void setOutputKeyName(String outputKeyName) {
this.outputKeyName = outputKeyName;
}
@@ -39,11 +45,15 @@ public class OutputFileListener {
}
@BeforeStep
public void CreateOutputNameFromInput(StepExecution stepExecution) {
public void createOutputNameFromInput(StepExecution stepExecution) {
ExecutionContext executionContext = stepExecution.getExecutionContext();
if (executionContext.containsKey(inputKeyName) && !executionContext.containsKey(outputKeyName)) {
String inputName = executionContext.getString(inputKeyName);
executionContext.putString(outputKeyName, "file:./target/output/" + FilenameUtils.getBaseName(inputName) + ".csv");
String inputName = stepExecution.getStepName().replace(":", "-");
if (executionContext.containsKey(inputKeyName)) {
inputName = executionContext.getString(inputKeyName);
}
if (!executionContext.containsKey(outputKeyName)) {
executionContext.putString(outputKeyName, path + FilenameUtils.getBaseName(inputName)
+ ".csv");
}
}

View File

@@ -29,6 +29,7 @@
<bean class="org.springframework.core.task.SyncTaskExecutor" />
</property>
<property name="step" ref="step1" />
<property name="gridSize" value="2"/>
</bean>
</property>
</bean>
@@ -42,7 +43,10 @@
</tasklet>
</step>
<bean id="fileNameListener" class="org.springframework.batch.sample.common.OutputFileListener" scope="step"/>
<bean id="fileNameListener" class="org.springframework.batch.sample.common.OutputFileListener" scope="step">
<property name="path" value="file:./target/output/file/"/>
</bean>
<bean id="itemReader" scope="step" autowire-candidate="false" parent="itemReaderParent">
<property name="resource" value="#{stepExecutionContext[fileName]}" />
@@ -54,7 +58,7 @@
</bean>
<bean id="outputTestReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="prototype">
<property name="resources" value="file:target/output/delimited*.csv" />
<property name="resources" value="file:target/output/file/delimited*.csv" />
<property name="delegate" ref="testItemReader" />
</bean>

View File

@@ -0,0 +1,146 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<job id="partitionJdbcJob" xmlns="http://www.springframework.org/schema/batch">
<step id="step" parent="step1:master" />
</job>
<bean name="step1:master"
class="org.springframework.batch.core.partition.support.PartitionStep">
<property name="jobRepository" ref="jobRepository" />
<property name="stepExecutionSplitter">
<bean
class="org.springframework.batch.core.partition.support.SimpleStepExecutionSplitter">
<constructor-arg ref="jobRepository" />
<constructor-arg ref="step1" />
<constructor-arg>
<bean
class="org.springframework.batch.sample.common.ColumnRangePartitioner">
<property name="dataSource" ref="dataSource" />
<property name="table" value="CUSTOMER" />
<property name="column" value="ID" />
</bean>
</constructor-arg>
</bean>
</property>
<property name="partitionHandler">
<bean
class="org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler">
<property name="taskExecutor">
<bean class="org.springframework.core.task.SyncTaskExecutor" />
</property>
<property name="step" ref="step1" />
<property name="gridSize" value="2"/>
</bean>
</property>
</bean>
<step id="step1" xmlns="http://www.springframework.org/schema/batch">
<tasklet job-repository="jobRepository" transaction-manager="transactionManager">
<chunk writer="itemWriter" reader="itemReader" processor="itemProcessor"
commit-interval="5" />
<listeners>
<listener ref="fileNameListener" />
</listeners>
</tasklet>
</step>
<bean id="fileNameListener"
class="org.springframework.batch.sample.common.OutputFileListener"
scope="step">
<property name="path" value="file:./target/output/jdbc/"/>
</bean>
<bean id="itemReader" scope="step" autowire-candidate="false"
parent="itemReaderParent">
<property name="sql">
<value>
<![CDATA[
select ID,NAME,CREDIT from CUSTOMER where ID >= ? and ID <= ?
]]>
</value>
</property>
<property name="preparedStatementSetter">
<bean
class="org.springframework.batch.core.resource.ListPreparedStatementSetter">
<property name="parameters">
<list>
<value>#{stepExecutionContext[minValue]}</value>
<value>#{stepExecutionContext[maxValue]}</value>
</list>
</property>
</bean>
</property>
</bean>
<bean id="outputTestReader"
class="org.springframework.batch.item.file.MultiResourceItemReader"
scope="prototype">
<property name="resources" value="file:target/output/jdbc/*.csv" />
<property name="delegate" ref="testItemReader" />
</bean>
<bean id="testItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="delimiter" value="," />
<property name="names" value="id,name,credit" />
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="targetType" value="org.springframework.batch.sample.domain.trade.CustomerCredit" />
</bean>
</property>
</bean>
</property>
</bean>
<bean id="inputTestReader" parent="itemReaderParent">
<property name="sql">
<value>
<![CDATA[
select ID,NAME,CREDIT from CUSTOMER
]]>
</value>
</property>
</bean>
<bean id="itemReaderParent"
class="org.springframework.batch.item.database.JdbcCursorItemReader"
abstract="true">
<property name="dataSource" ref="dataSource" />
<property name="rowMapper">
<bean
class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper" />
</property>
</bean>
<bean id="itemProcessor"
class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor" />
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter"
scope="step">
<property name="resource" value="#{stepExecutionContext[outputFile]}" />
<property name="lineAggregator">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value="," />
<property name="fieldExtractor">
<bean
class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="id,name,credit" />
</bean>
</property>
</bean>
</property>
</bean>
</beans>