Spring Batch - Read once and write twice

偶尔善良 提交于 2019-12-20 04:24:08

问题


I am new to Spring Batch. My requirement is I have a reader which gets the records through a web service call/database call and currently I am writing those records to one table. Now I need same records (records read by reader) needs to be processed and write into another table. The point to note here is the second items those are getting stored in second write are of different type of first write.

I need like below

1st Step: - Read items of type A --> Write items of Type A 
2nd Step:-  Read items of type A --> Process to type B ---> Write 10 items of type B

For the same above job I need Transaction Management. Also, in Step-2 :- If possible, I should use the data which was already read in Step-1.


回答1:


Spring Batch defines independent steps for processing. Each step is responsible for their input, processing, and output. Because of this, I'd structure the job something like:

<job id="myJob">
    <step id="step1" next="step2">
        <tasklet>
            <chunk reader="reader" writer="typeAwriter"/>
        </tasklet>
    </step>
    <step id="step2">
        <tasklet>
            <chunk reader="reader" processor="processor" writer="typeBwriter"/>
        </tasklet>
    </step>
</job>

With the above configuration, the reader would be a step scoped reader that reads in type A. The typeAwriter writes out type A. The processor is the processor that converts type A to type B. The typeBwriter writes type B. Since the processor returns a list of type B, the typeBwriter would need to be a custom implementation that loops over the lists that the processor returns (typeBwriter would take a List<List<TypeB>>).



来源:https://stackoverflow.com/questions/25850944/spring-batch-read-once-and-write-twice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!