Reputation: 65
I'm writing a Spring Batch Job and I can't find a way to handle multiple datasource and merge then together.
I have a web api that return a list of objects and their id.
I also have a database with some other objects
For each object read from the web api call, I need to send a sql request to the database to get the object with the id of my current object i'm reading.
Then I need to merge those objects together and save them to an another web api.
@Bean
public Job todoJob() {
final String jobName = "todos-migration-job";
Step step1 = stepBuilderFactory.get("get-todo-from-api").<TodoDto, TodoDto>chunk(30)
.reader(todoItemReader)
.writer(todoItemWriter)
.build();
Step step2 =
stepBuilderFactory.get("get-todo-from-database").<TodoDto, TodoBackendDto>chunk(30)
.reader(todoItemReader)
.processor(todoItemDatabaseProcessor)
.writer(todoItemBackendWriter)
.build();
// what should I do here? should I do 1 or 2 steps?
Job todoJob = jobBuilderFactory.get(jobName)
.incrementer(new RunIdIncrementer())
.start(step1)
.next(step2)
.build();
this.jobService.registerJob(jobName);
return todoJob;
}
@Bean
public TodoItemRestReader todoItemReader() {
return new TodoItemRestReader();
}
@Bean
public JdbcCursorItemReader<TodoDto> todoItemDatabaseReader() {
return new JdbcCursorItemReaderBuilder<TodoDto>()
.dataSource(this.datasourceConfig.todoDataSource())
.name("todoItemDatabaseReader")
.sql("select id, title from todo_data where id = :id") // how do I get the id ?
.rowMapper(new TodoItemRowMapper())
.build();
}
So, how can I do to share all my read data from the first reader to the second ?
Thank's
Upvotes: 3
Views: 8607
Reputation: 3899
Spring Batch does not allow to use more than one reader in one step.
There is more than one way to solve your problem:
Upvotes: 4