BATCH-1103: Added partitioning sample
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.common;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.annotation.BeforeStep;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class OutputFileListener {
|
||||
|
||||
private String outputKeyName = "outputFile";
|
||||
|
||||
private String inputKeyName = "fileName";
|
||||
|
||||
public void setOutputKeyName(String outputKeyName) {
|
||||
this.outputKeyName = outputKeyName;
|
||||
}
|
||||
|
||||
public void setInputKeyName(String inputKeyName) {
|
||||
this.inputKeyName = inputKeyName;
|
||||
}
|
||||
|
||||
@BeforeStep
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.springframework.batch.sample.common;
|
||||
|
||||
public class OutputFileNameListener {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:batch="http://www.springframework.org/schema/batch" 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/batch http://www.springframework.org/schema/batch/spring-batch-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">
|
||||
|
||||
<batch:job id="partitionJob">
|
||||
<batch:step id="step" ref="step1:master" />
|
||||
</batch: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.core.partition.support.MultiResourcePartitioner">
|
||||
<property name="resources" value="classpath:data/iosample/input/delimited*.csv" />
|
||||
</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" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<batch:step id="step1" job-repository="jobRepository" transaction-manager="transactionManager">
|
||||
<batch:tasklet writer="itemWriter" reader="itemReader" processor="itemProcessor" commit-interval="5" />
|
||||
<batch:listeners>
|
||||
<batch:listener ref="fileNameListener" />
|
||||
</batch:listeners>
|
||||
</batch:step>
|
||||
|
||||
<bean id="fileNameListener" class="org.springframework.batch.sample.common.OutputFileListener" />
|
||||
|
||||
<bean id="itemReader" scope="step" autowire-candidate="false" parent="itemReaderParent">
|
||||
<property name="resource" value="#{stepExecutionContext[fileName]}" />
|
||||
</bean>
|
||||
|
||||
<bean id="inputTestReader" class="org.springframework.batch.item.file.MultiResourceItemReader">
|
||||
<property name="resources" value="classpath:data/iosample/input/delimited*.csv" />
|
||||
<property name="delegate" ref="testItemReader" />
|
||||
</bean>
|
||||
|
||||
<bean id="outputTestReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="prototype">
|
||||
<property name="resources" value="file:target/output/delimited*.csv" />
|
||||
<property name="delegate" ref="testItemReader" />
|
||||
</bean>
|
||||
|
||||
<bean id="testItemReader" parent="itemReaderParent"/>
|
||||
|
||||
<bean id="itemReaderParent" class="org.springframework.batch.item.file.FlatFileItemReader" abstract="true">
|
||||
<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="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="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="name,credit" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
|
||||
import org.springframework.batch.test.AbstractJobTests;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration()
|
||||
public class PartitionJobFunctionalTests extends AbstractJobTests {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("inputTestReader")
|
||||
private ItemReader<CustomerCredit> inputReader;
|
||||
|
||||
/**
|
||||
* Check the resulting credits correspond to inputs increased by fixed
|
||||
* amount.
|
||||
*/
|
||||
@Test
|
||||
public void testUpdateCredit() throws Exception {
|
||||
|
||||
assertTrue("Define a prototype bean called 'outputTestReader' to check the output", getApplicationContext()
|
||||
.containsBeanDefinition("outputTestReader"));
|
||||
|
||||
open(inputReader);
|
||||
List<CustomerCredit> inputs = getCredits(inputReader);
|
||||
close(inputReader);
|
||||
|
||||
JobExecution jobExecution = this.launchJob();
|
||||
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ItemReader<CustomerCredit> outputReader = (ItemReader<CustomerCredit>) getApplicationContext().getBean(
|
||||
"outputTestReader");
|
||||
open(outputReader);
|
||||
List<CustomerCredit> outputs = getCredits(outputReader);
|
||||
close(outputReader);
|
||||
|
||||
assertEquals(inputs.size(), outputs.size());
|
||||
int itemCount = inputs.size();
|
||||
assertTrue(itemCount > 0);
|
||||
|
||||
for (int i = 0; i < itemCount; i++) {
|
||||
assertEquals(inputs.get(i).getCredit().add(CustomerCreditIncreaseProcessor.FIXED_AMOUNT).intValue(),
|
||||
outputs.get(i).getCredit().intValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Read all credits using the provided reader.
|
||||
*/
|
||||
private List<CustomerCredit> getCredits(ItemReader<CustomerCredit> reader) throws Exception {
|
||||
CustomerCredit credit;
|
||||
List<CustomerCredit> result = new ArrayList<CustomerCredit>();
|
||||
while ((credit = reader.read()) != null) {
|
||||
result.add(credit);
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the reader if applicable.
|
||||
*/
|
||||
private void open(ItemReader<?> reader) {
|
||||
if (reader instanceof ItemStream) {
|
||||
((ItemStream) reader).open(new ExecutionContext());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the reader if applicable.
|
||||
*/
|
||||
private void close(ItemReader<?> reader) {
|
||||
if (reader instanceof ItemStream) {
|
||||
((ItemStream) reader).close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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/partitionJob.xml" />
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user