Skip to content

Commit 7bb5ca7

Browse files
committed
Add java configuration for the multi-resource job sample
Issue #3663
1 parent bb8d267 commit 7bb5ca7

File tree

8 files changed

+252
-59
lines changed

8 files changed

+252
-59
lines changed

spring-batch-samples/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ streaming and Spring OXM marshallers and unmarshallers.
183183

184184
[XML Input Output](./src/main/java/org/springframework/batch/sample/file/xml/README.md)
185185

186+
### MultiResource Input Output Job
187+
188+
This sample shows how to use the `MultiResourceItemReader` and `MultiResourceItemWriter`
189+
to read and write multiple files in the same step.
190+
191+
[MultiResource Input Output Job Sample](./src/main/java/org/springframework/batch/sample/file/multiresource/README.md)
192+
186193
### Football Job
187194

188195
This is a (American) Football statistics loading job. It loads two files containing players and games
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.sample.file.multiresource;
17+
18+
import javax.sql.DataSource;
19+
20+
import org.springframework.batch.core.Job;
21+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
22+
import org.springframework.batch.core.configuration.annotation.StepScope;
23+
import org.springframework.batch.core.job.builder.JobBuilder;
24+
import org.springframework.batch.core.repository.JobRepository;
25+
import org.springframework.batch.core.step.builder.StepBuilder;
26+
import org.springframework.batch.item.ItemReader;
27+
import org.springframework.batch.item.ItemWriter;
28+
import org.springframework.batch.item.file.FlatFileItemReader;
29+
import org.springframework.batch.item.file.FlatFileItemWriter;
30+
import org.springframework.batch.item.file.MultiResourceItemReader;
31+
import org.springframework.batch.item.file.MultiResourceItemWriter;
32+
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
33+
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
34+
import org.springframework.batch.item.file.builder.MultiResourceItemReaderBuilder;
35+
import org.springframework.batch.item.file.builder.MultiResourceItemWriterBuilder;
36+
import org.springframework.batch.sample.domain.trade.CustomerCredit;
37+
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
38+
import org.springframework.beans.factory.annotation.Value;
39+
import org.springframework.context.annotation.Bean;
40+
import org.springframework.context.annotation.Configuration;
41+
import org.springframework.core.io.Resource;
42+
import org.springframework.core.io.WritableResource;
43+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
44+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
45+
import org.springframework.jdbc.support.JdbcTransactionManager;
46+
47+
/**
48+
* @author Mahmoud Ben Hassine
49+
*/
50+
@Configuration
51+
@EnableBatchProcessing
52+
public class MultiResourceJobConfiguration {
53+
54+
@Bean
55+
@StepScope
56+
public MultiResourceItemReader<CustomerCredit> itemReader(
57+
@Value("#{jobParameters[inputFiles]}") Resource[] resources) {
58+
return new MultiResourceItemReaderBuilder<CustomerCredit>().name("itemReader")
59+
.resources(resources)
60+
.delegate(delegateReader())
61+
.build();
62+
}
63+
64+
@Bean
65+
public FlatFileItemReader<CustomerCredit> delegateReader() {
66+
return new FlatFileItemReaderBuilder<CustomerCredit>().name("delegateItemReader")
67+
.delimited()
68+
.names("name", "credit")
69+
.targetType(CustomerCredit.class)
70+
.build();
71+
}
72+
73+
@Bean
74+
@StepScope
75+
public MultiResourceItemWriter<CustomerCredit> itemWriter(
76+
@Value("#{jobParameters[outputFiles]}") WritableResource resource) {
77+
return new MultiResourceItemWriterBuilder<CustomerCredit>().name("itemWriter")
78+
.delegate(delegateWriter())
79+
.resource(resource)
80+
.itemCountLimitPerResource(6)
81+
.build();
82+
}
83+
84+
@Bean
85+
public FlatFileItemWriter<CustomerCredit> delegateWriter() {
86+
return new FlatFileItemWriterBuilder<CustomerCredit>().name("delegateItemWriter")
87+
.delimited()
88+
.names("name", "credit")
89+
.build();
90+
}
91+
92+
@Bean
93+
public Job job(JobRepository jobRepository, JdbcTransactionManager transactionManager,
94+
ItemReader<CustomerCredit> itemReader, ItemWriter<CustomerCredit> itemWriter) {
95+
return new JobBuilder("ioSampleJob", jobRepository)
96+
.start(new StepBuilder("step1", jobRepository).<CustomerCredit, CustomerCredit>chunk(2, transactionManager)
97+
.reader(itemReader)
98+
.processor(new CustomerCreditIncreaseProcessor())
99+
.writer(itemWriter)
100+
.build())
101+
.build();
102+
}
103+
104+
// Infrastructure beans
105+
106+
@Bean
107+
public DataSource dataSource() {
108+
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
109+
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
110+
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
111+
.generateUniqueName(true)
112+
.build();
113+
}
114+
115+
@Bean
116+
public JdbcTransactionManager transactionManager(DataSource dataSource) {
117+
return new JdbcTransactionManager(dataSource);
118+
}
119+
120+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
### MultiResource Input Output Job
2+
3+
## About
4+
5+
This sample shows how to use the `MultiResourceItemReader` and `MultiResourceItemWriter`
6+
to read and write multiple files in the same step.
7+
8+
## Run the sample
9+
10+
You can run the sample from the command line as following:
11+
12+
```
13+
$>cd spring-batch-samples
14+
# Launch the sample using the XML configuration
15+
$>../mvnw -Dtest=MultiResourceFunctionalTests#testLaunchJobWithXmlConfig test
16+
# Launch the sample using the Java configuration
17+
$>../mvnw -Dtest=MultiResourceFunctionalTests#testLaunchJobWithJavaConfig test
18+
```
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
customer1,10
2+
customer2,20
3+
customer3,30
4+
customer4,40
5+
customer5,50
6+
customer6,60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
customer7,70
2+
customer8,80

spring-batch-samples/src/main/resources/jobs/iosample/multiResource.xml renamed to spring-batch-samples/src/main/resources/org/springframework/batch/sample/file/multiresource/job/multiResource.xml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:batch="http://www.springframework.org/schema/batch"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/batch https://www.springframework.org/schema/batch/spring-batch.xsd
57
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
68

9+
<batch:job id="ioSampleJob" xmlns="http://www.springframework.org/schema/batch">
10+
<batch:step id="step1">
11+
<batch:tasklet>
12+
<batch:chunk reader="itemReader" processor="itemProcessor" writer="itemWriter"
13+
commit-interval="2"/>
14+
</batch:tasklet>
15+
</batch:step>
16+
</batch:job>
17+
18+
<bean id="itemProcessor" class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor" />
19+
720
<bean id="itemReader"
821
class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
922
<property name="delegate">
@@ -29,13 +42,13 @@
2942
</property>
3043
</bean>
3144
</property>
32-
<property name="resources" value="#{jobParameters['input.file.path']}" />
45+
<property name="resources" value="#{jobParameters['inputFiles']}" />
3346
</bean>
3447

3548
<bean id="itemWriter"
3649
class="org.springframework.batch.item.file.MultiResourceItemWriter" scope="step">
3750
<property name="resource"
38-
value="#{jobParameters['output.file.path']}" />
51+
value="#{jobParameters['outputFiles']}" />
3952
<property name="itemCountLimitPerResource" value="6" />
4053
<property name="delegate" ref="delegateWriter" />
4154
</bean>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2006-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.batch.sample.file.multiresource;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.batch.core.BatchStatus;
22+
import org.springframework.batch.core.Job;
23+
import org.springframework.batch.core.JobExecution;
24+
import org.springframework.batch.core.JobParameters;
25+
import org.springframework.batch.core.JobParametersBuilder;
26+
import org.springframework.batch.core.launch.JobLauncher;
27+
import org.springframework.batch.test.JobLauncherTestUtils;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.context.ApplicationContext;
30+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
31+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
32+
33+
import static org.junit.jupiter.api.Assertions.assertEquals;
34+
35+
/**
36+
* @author Dan Garrette
37+
* @author Glenn Renfro
38+
* @author Mahmoud Ben Hassine
39+
* @since 2.0
40+
*/
41+
@SpringJUnitConfig(locations = { "/org/springframework/batch/sample/file/multiresource/job/multiResource.xml",
42+
"/simple-job-launcher-context.xml", "/job-runner-context.xml" })
43+
class MultiResourceFunctionalTests {
44+
45+
@Autowired
46+
private JobLauncherTestUtils jobLauncherTestUtils;
47+
48+
@Test
49+
void testLaunchJobWithXmlConfig() throws Exception {
50+
// given
51+
JobParameters jobParameters = new JobParametersBuilder()
52+
.addString("inputFiles", "org/springframework/batch/sample/file/multiresource/data/delimited*.csv")
53+
.addString("outputFiles", "file:./target/test-outputs/multiResourceOutput.csv")
54+
.toJobParameters();
55+
56+
// when
57+
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);
58+
59+
// then
60+
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
61+
}
62+
63+
@Test
64+
public void testLaunchJobWithJavaConfig() throws Exception {
65+
// given
66+
ApplicationContext context = new AnnotationConfigApplicationContext(MultiResourceJobConfiguration.class);
67+
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
68+
Job job = context.getBean(Job.class);
69+
JobParameters jobParameters = new JobParametersBuilder()
70+
.addString("inputFiles", "org/springframework/batch/sample/file/multiresource/data/delimited*.csv")
71+
.addString("outputFiles", "file:./target/test-outputs/multiResourceOutput.csv")
72+
.toJobParameters();
73+
74+
// when
75+
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
76+
77+
// then
78+
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
79+
}
80+
81+
}

spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiResourceFunctionalTests.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)