RESOLVED - BATCH-34: Support for multiple I/O files in a single jobRun for a particular scheduleDate.

multiResourceJob used as sample also for output
This commit is contained in:
robokaso
2008-11-13 10:11:50 +00:00
parent d19145d0af
commit 675ffb70b7
6 changed files with 67 additions and 28 deletions

View File

@@ -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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
@@ -14,14 +12,22 @@
<bean id="step1" parent="simpleStep" p:commitInterval="3">
<property name="itemReader" ref="fileItemReader" />
<property name="itemProcessor">
<bean class="org.springframework.batch.item.validator.ValidatingItemProcessor">
<bean
class="org.springframework.batch.item.validator.ValidatingItemProcessor">
<constructor-arg ref="fixedValidator" />
</bean>
</property>
<property name="itemWriter">
<bean
class="org.springframework.batch.sample.domain.trade.internal.TradeWriter">
<property name="dao" ref="tradeDao" />
<bean class="org.springframework.batch.item.file.MultiResourceItemWriter">
<property name="resource" ref="outputFile" />
<property name="itemCountLimitPerResource" value="2" />
<property name="delegate">
<bean class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator" />
</property>
</bean>
</property>
</bean>
</property>
</bean>
@@ -29,20 +35,25 @@
</bean>
<!-- INFRASTRUCTURE SETUP -->
<!-- This input source is injected into the test case to verify the output - not used by the job at all -->
<bean id="outputFile" class="org.springframework.core.io.FileSystemResource">
<constructor-arg type="java.lang.String"
value="target/test-outputs/multiResourceOutput.txt" />
</bean>
<!--
This input source is injected into the test case to verify the output
- not used by the job at all
-->
<bean id="testItemReader"
class="org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources"
value="classpath:data/multiResourceJob/input/file-*.txt" />
<property name="delegate" ref="flatFileItemReader" />
</bean>
<bean id="fileItemReader" parent="testItemReader"
autowire-candidate="false" />
<bean id="flatFileItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader">
<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource"
value="classpath:data/fixedLengthImportJob/input/20070122.teststream.ImportTradeDataStep.txt" />
<property name="lineMapper">
@@ -62,8 +73,7 @@
<bean id="fixedValidator"
class="org.springframework.batch.item.validator.SpringValidator">
<property name="validator">
<bean id="tradeValidator"
class="org.springmodules.validation.valang.ValangValidator">
<bean id="tradeValidator" class="org.springmodules.validation.valang.ValangValidator">
<property name="valang">
<value>
<![CDATA[

View File

@@ -53,7 +53,7 @@ public class FixedLengthImportJobFunctionalTests extends AbstractValidatingBatch
//auto-injected attributes
private SimpleJdbcTemplate simpleJdbcTemplate;
private Resource fileLocator;
private FlatFileItemReader<Trade> inputSource;
protected FlatFileItemReader<Trade> itemReader;
private LineTokenizer lineTokenizer;
@Autowired
@@ -71,16 +71,17 @@ public class FixedLengthImportJobFunctionalTests extends AbstractValidatingBatch
public void onSetUp() throws Exception {
simpleJdbcTemplate.update("delete from TRADE");
fileLocator = new ClassPathResource("data/fixedLengthImportJob/input/20070122.teststream.ImportTradeDataStep.txt");
inputSource = new FlatFileItemReader<Trade>();
itemReader = new FlatFileItemReader<Trade>();
FieldSetMapper<Trade> mapper = new TradeFieldSetMapper();
DefaultLineMapper<Trade> lineMapper = new DefaultLineMapper<Trade>();
lineMapper.setLineTokenizer(lineTokenizer);
lineMapper.setFieldSetMapper(mapper);
inputSource.setLineMapper(lineMapper);
itemReader.setLineMapper(lineMapper);
inputSource.setResource(fileLocator);
itemReader.setResource(fileLocator);
itemReader.open(new ExecutionContext());
}
/**
@@ -89,7 +90,6 @@ public class FixedLengthImportJobFunctionalTests extends AbstractValidatingBatch
*/
protected void validatePostConditions() throws Exception {
inputSource.open(new ExecutionContext());
simpleJdbcTemplate.getJdbcOperations().query(
"SELECT ID, ISIN, QUANTITY, PRICE, CUSTOMER FROM trade ORDER BY id",
@@ -97,7 +97,7 @@ public class FixedLengthImportJobFunctionalTests extends AbstractValidatingBatch
public void processRow(ResultSet rs) throws SQLException {
Trade trade;
try {
trade = inputSource.read();
trade = itemReader.read();
}
catch (Exception e) {
throw new IllegalStateException(e.getMessage());
@@ -110,7 +110,7 @@ public class FixedLengthImportJobFunctionalTests extends AbstractValidatingBatch
});
assertNull(inputSource.read());
assertNull(itemReader.read());
}
/*

View File

@@ -1,5 +1,11 @@
package org.springframework.batch.sample;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.beans.factory.annotation.Autowired;
@@ -11,6 +17,30 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration()
public class MultiResourceJobFunctionalTests extends FixedLengthImportJobFunctionalTests {
/**
* Context: 5 items overall, min. 2 items per output file, commitInterval=3,
* => two files created, with 3 items in the first and two in second.
*/
@Override
protected void validatePostConditions() throws Exception {
File file1 = new File("target/test-outputs/multiResourceOutput.txt.1");
File file2 = new File("target/test-outputs/multiResourceOutput.txt.2");
assertTrue(file1.exists());
assertTrue(file2.exists());
BufferedReader reader1 = new BufferedReader(new FileReader(file1));
for (int i = 1; i <= 3; i++) {
assertEquals(itemReader.read().toString(), reader1.readLine());
}
assertNull(reader1.readLine());
BufferedReader reader2 = new BufferedReader(new FileReader(file2));
for (int i = 1; i <= 2; i++) {
assertEquals(itemReader.read().toString(), reader2.readLine());
}
assertNull(reader2.readLine());
}
@Autowired
public void setJob(@Qualifier("multiResourceJob") Job job) {