@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright 2023 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
|
||||
*
|
||||
* https://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.file.multiresource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
|
||||
import org.springframework.batch.core.configuration.annotation.StepScope;
|
||||
import org.springframework.batch.core.job.builder.JobBuilder;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||
import org.springframework.batch.item.file.FlatFileItemWriter;
|
||||
import org.springframework.batch.item.file.MultiResourceItemReader;
|
||||
import org.springframework.batch.item.file.MultiResourceItemWriter;
|
||||
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
|
||||
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
|
||||
import org.springframework.batch.item.file.builder.MultiResourceItemReaderBuilder;
|
||||
import org.springframework.batch.item.file.builder.MultiResourceItemWriterBuilder;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.WritableResource;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
import org.springframework.jdbc.support.JdbcTransactionManager;
|
||||
|
||||
/**
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
@Configuration
|
||||
@EnableBatchProcessing
|
||||
public class MultiResourceJobConfiguration {
|
||||
|
||||
@Bean
|
||||
@StepScope
|
||||
public MultiResourceItemReader<CustomerCredit> itemReader(
|
||||
@Value("#{jobParameters[inputFiles]}") Resource[] resources) {
|
||||
return new MultiResourceItemReaderBuilder<CustomerCredit>().name("itemReader")
|
||||
.resources(resources)
|
||||
.delegate(delegateReader())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FlatFileItemReader<CustomerCredit> delegateReader() {
|
||||
return new FlatFileItemReaderBuilder<CustomerCredit>().name("delegateItemReader")
|
||||
.delimited()
|
||||
.names("name", "credit")
|
||||
.targetType(CustomerCredit.class)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@StepScope
|
||||
public MultiResourceItemWriter<CustomerCredit> itemWriter(
|
||||
@Value("#{jobParameters[outputFiles]}") WritableResource resource) {
|
||||
return new MultiResourceItemWriterBuilder<CustomerCredit>().name("itemWriter")
|
||||
.delegate(delegateWriter())
|
||||
.resource(resource)
|
||||
.itemCountLimitPerResource(6)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FlatFileItemWriter<CustomerCredit> delegateWriter() {
|
||||
return new FlatFileItemWriterBuilder<CustomerCredit>().name("delegateItemWriter")
|
||||
.delimited()
|
||||
.names("name", "credit")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job job(JobRepository jobRepository, JdbcTransactionManager transactionManager,
|
||||
ItemReader<CustomerCredit> itemReader, ItemWriter<CustomerCredit> itemWriter) {
|
||||
return new JobBuilder("ioSampleJob", jobRepository)
|
||||
.start(new StepBuilder("step1", jobRepository).<CustomerCredit, CustomerCredit>chunk(2, transactionManager)
|
||||
.reader(itemReader)
|
||||
.processor(new CustomerCreditIncreaseProcessor())
|
||||
.writer(itemWriter)
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
|
||||
// Infrastructure beans
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
|
||||
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
|
||||
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
|
||||
.generateUniqueName(true)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JdbcTransactionManager transactionManager(DataSource dataSource) {
|
||||
return new JdbcTransactionManager(dataSource);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
### MultiResource Input Output Job
|
||||
|
||||
## About
|
||||
|
||||
This sample shows how to use the `MultiResourceItemReader` and `MultiResourceItemWriter`
|
||||
to read and write multiple files in the same step.
|
||||
|
||||
## Run the sample
|
||||
|
||||
You can run the sample from the command line as following:
|
||||
|
||||
```
|
||||
$>cd spring-batch-samples
|
||||
# Launch the sample using the XML configuration
|
||||
$>../mvnw -Dtest=MultiResourceFunctionalTests#testLaunchJobWithXmlConfig test
|
||||
# Launch the sample using the Java configuration
|
||||
$>../mvnw -Dtest=MultiResourceFunctionalTests#testLaunchJobWithJavaConfig test
|
||||
```
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
customer1,10
|
||||
customer2,20
|
||||
customer3,30
|
||||
customer4,40
|
||||
customer5,50
|
||||
customer6,60
|
||||
|
@@ -0,0 +1,2 @@
|
||||
customer7,70
|
||||
customer8,80
|
||||
|
@@ -1,9 +1,22 @@
|
||||
<?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="
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:batch="http://www.springframework.org/schema/batch"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/batch https://www.springframework.org/schema/batch/spring-batch.xsd
|
||||
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<batch:job id="ioSampleJob" xmlns="http://www.springframework.org/schema/batch">
|
||||
<batch:step id="step1">
|
||||
<batch:tasklet>
|
||||
<batch:chunk reader="itemReader" processor="itemProcessor" writer="itemWriter"
|
||||
commit-interval="2"/>
|
||||
</batch:tasklet>
|
||||
</batch:step>
|
||||
</batch:job>
|
||||
|
||||
<bean id="itemProcessor" class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor" />
|
||||
|
||||
<bean id="itemReader"
|
||||
class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
|
||||
<property name="delegate">
|
||||
@@ -29,13 +42,13 @@
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
<property name="resources" value="#{jobParameters['input.file.path']}" />
|
||||
<property name="resources" value="#{jobParameters['inputFiles']}" />
|
||||
</bean>
|
||||
|
||||
<bean id="itemWriter"
|
||||
class="org.springframework.batch.item.file.MultiResourceItemWriter" scope="step">
|
||||
<property name="resource"
|
||||
value="#{jobParameters['output.file.path']}" />
|
||||
value="#{jobParameters['outputFiles']}" />
|
||||
<property name="itemCountLimitPerResource" value="6" />
|
||||
<property name="delegate" ref="delegateWriter" />
|
||||
</bean>
|
||||
Reference in New Issue
Block a user