Use Spring Batch to write in different Data Sources

前端 未结 1 874
轮回少年
轮回少年 2021-01-16 01:15

For a project I need to process items from one table and generate 3 different items for 3 different tables, all 3 in a second data source different from the one of the first

1条回答
  •  有刺的猬
    2021-01-16 01:25

    Your question is really two questions. Let's look at each individually:

    Can an ItemProcessor return multiple items
    An ItemProcessor can only return one item at a time for each item that is passed in. Because of this, in your specific scenario, you'll need your ItemProcessor to return a wrapper object that wraps items A, B, C, and D.

    How can I write different types in the same step
    Spring Batch relies heavily on composition in it's programming model. Since your ItemProcessor will be returning a wrapper object, you'll end up writing an ItemWriter that unwraps items A, B, C, and D and delegates the writing of each to the apropriate writer. So in the final solution, you'll end up with 5 ItemWriters: one for each item type and one that wraps all of those. Take a look at our CompositeItemWriter as an example here: https://github.com/spring-projects/spring-batch/blob/master/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/CompositeItemWriter.java

    0 讨论(0)
提交回复
热议问题