-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
678b8bb
commit f176d65
Showing
8 changed files
with
290 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/org/springframework/batch/experimental/item/support/CompositeItemReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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.experimental.item.support; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import org.springframework.batch.item.ExecutionContext; | ||
import org.springframework.batch.item.ItemStreamException; | ||
import org.springframework.batch.item.ItemStreamReader; | ||
|
||
/** | ||
* Composite reader that delegates reading to a list of {@link ItemStreamReader}s. | ||
* This implementation is not thread-safe and not restartable. | ||
* | ||
* @author Mahmoud Ben Hassine | ||
* @param <T> type of objects to read | ||
*/ | ||
public class CompositeItemReader<T> implements ItemStreamReader<T> { | ||
|
||
private final List<ItemStreamReader<T>> delegates; | ||
|
||
private final Iterator<ItemStreamReader<T>> delegatesIterator; | ||
|
||
private ItemStreamReader<T> currentDelegate; | ||
|
||
public CompositeItemReader(List<ItemStreamReader<T>> delegates) { | ||
this.delegates = delegates; | ||
this.delegatesIterator = this.delegates.iterator(); | ||
this.currentDelegate = this.delegatesIterator.hasNext() ? this.delegatesIterator.next() : null; | ||
} | ||
|
||
@Override | ||
public void open(ExecutionContext executionContext) throws ItemStreamException { | ||
for (ItemStreamReader<T> delegate : delegates) { | ||
delegate.open(executionContext); | ||
} | ||
} | ||
|
||
@Override | ||
public T read() throws Exception { | ||
if (this.currentDelegate == null) { | ||
return null; | ||
} | ||
T item = currentDelegate.read(); | ||
if (item == null) { | ||
currentDelegate = this.delegatesIterator.hasNext() ? this.delegatesIterator.next() : null; | ||
return read(); | ||
} | ||
return item; | ||
} | ||
|
||
@Override | ||
public void close() throws ItemStreamException { | ||
for (ItemStreamReader<T> delegate : delegates) { | ||
delegate.close(); | ||
} | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
.../springframework/batch/experimental/item/support/CompositeItemReaderIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* 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.experimental.item.support; | ||
|
||
import java.util.Arrays; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.batch.core.ExitStatus; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.JobExecution; | ||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.builder.StepBuilder; | ||
import org.springframework.batch.item.database.JdbcBatchItemWriter; | ||
import org.springframework.batch.item.database.JdbcCursorItemReader; | ||
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder; | ||
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder; | ||
import org.springframework.batch.item.file.FlatFileItemReader; | ||
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.jdbc.core.DataClassRowMapper; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | ||
import org.springframework.jdbc.support.JdbcTransactionManager; | ||
import org.springframework.test.jdbc.JdbcTestUtils; | ||
|
||
public class CompositeItemReaderIntegrationTests { | ||
|
||
record Person(int id, String name) { | ||
} | ||
|
||
@Test | ||
void testCompositeItemReader() throws Exception { | ||
// given | ||
ApplicationContext context = new AnnotationConfigApplicationContext(JobConfiguration.class); | ||
JobLauncher jobLauncher = context.getBean(JobLauncher.class); | ||
Job job = context.getBean(Job.class); | ||
|
||
// when | ||
JobExecution jobExecution = jobLauncher.run(job, new JobParameters()); | ||
|
||
// then | ||
Assertions.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); | ||
JdbcTemplate jdbcTemplate = new JdbcTemplate(context.getBean(DataSource.class)); | ||
int personsCount = JdbcTestUtils.countRowsInTable(jdbcTemplate, "person_target"); | ||
Assertions.assertEquals(6, personsCount); | ||
} | ||
|
||
@Configuration | ||
@EnableBatchProcessing | ||
static class JobConfiguration { | ||
|
||
@Bean | ||
public FlatFileItemReader<Person> itemReader1() { | ||
return new FlatFileItemReaderBuilder<Person>() | ||
.name("personItemReader1") | ||
.resource(new ClassPathResource("persons1.csv")) | ||
.delimited() | ||
.names("id", "name") | ||
.targetType(Person.class) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public FlatFileItemReader<Person> itemReader2() { | ||
return new FlatFileItemReaderBuilder<Person>() | ||
.name("personItemReader2") | ||
.resource(new ClassPathResource("persons2.csv")) | ||
.delimited() | ||
.names("id", "name") | ||
.targetType(Person.class) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public JdbcCursorItemReader<Person> itemReader3() { | ||
String sql = "select * from person_source"; | ||
return new JdbcCursorItemReaderBuilder<Person>() | ||
.name("personItemReader3") | ||
.dataSource(dataSource()) | ||
.sql(sql) | ||
.rowMapper(new DataClassRowMapper<>(Person.class)) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public CompositeItemReader<Person> itemReader() { | ||
return new CompositeItemReader<>(Arrays.asList(itemReader1(), itemReader2(), itemReader3())); | ||
} | ||
|
||
@Bean | ||
public JdbcBatchItemWriter<Person> itemWriter() { | ||
String sql = "insert into person_target (id, name) values (:id, :name)"; | ||
return new JdbcBatchItemWriterBuilder<Person>() | ||
.dataSource(dataSource()) | ||
.sql(sql) | ||
.beanMapped() | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Job job(JobRepository jobRepository, JdbcTransactionManager transactionManager) { | ||
return new JobBuilder("job", jobRepository) | ||
.start(new StepBuilder("step", jobRepository) | ||
.<Person, Person>chunk(5, transactionManager) | ||
.reader(itemReader()) | ||
.writer(itemWriter()) | ||
.build()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public DataSource dataSource() { | ||
return new EmbeddedDatabaseBuilder() | ||
.setType(EmbeddedDatabaseType.H2) | ||
.addScript("/org/springframework/batch/core/schema-drop-h2.sql") | ||
.addScript("/org/springframework/batch/core/schema-h2.sql") | ||
.addScript("schema.sql") | ||
.addScript("data.sql") | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public JdbcTransactionManager transactionManager(DataSource dataSource) { | ||
return new JdbcTransactionManager(dataSource); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
insert into person_source values (5, 'baz1'); | ||
insert into person_source values (6, 'baz2'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1,foo1 | ||
2,foo2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
3,bar1 | ||
4,bar2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
create table person_source (id int primary key, name varchar(20)); | ||
create table person_target (id int primary key, name varchar(20)); |