首页 文章

有没有办法在 Spring 季批量调用编写器之前拦截并注入另一个对象?

提问于
浏览
1

我正在使用Spring Batch .

来自文件的流读取项如下:

图片来自official Spring batch documentation
Flow of item reader

我正在使用批量块 .

<batch:step id="myImportStep">
    <batch:tasklet>
        <batch:chunk reader="myFileItemReader" processor="myFileProcessor" writer="myFileWriter" chunk-completion-policy="myCompletionPolicy" skip-limit="1000000" retry-limit="3">
            ... Some excetions and listeners
        </batch:chunk>
    </batch:tasklet>
</batch:step>

我有一个文件,我在我的项目阅读器中处理,我逐行阅读,然后在我的处理器中,我创建自己的自定义订单对象 . 在每个第10个订单项创建后,根据完成策略,订单对象列表将传递给编写器以执行实际订单 .

我的业务案例说,当执行编写器进程时(也就是当我有一个创建了10个订单对象的列表时)去运行一些逻辑来查看是否需要创建一个额外的订单对象 .

public class myWriter implements ItemWriter<OrderObject> {

    public void write(final List<? extends OrderObject> items) throws Exception {

        //apply logic here to see if an additional order should be created
        //problem is that the list of items cannot be modified to add more items to it
        //which make sense... if the itemWriter gets called in chunks of 10 then you simply 
        //can't add a new order in between. What I am looking to do... is there way to access this 
        //list of orders to add more to it... or another suggestion on how to add the original list of
        //orders and to be able to add more

但我不知道如何获得对处理器创建的原始订单列表的访问权限 . 即使我可以在列表的后面添加订单,这些订单将在稍后阶段再次发送给Writer . 关于如何做这样的事情的任何其他建议?

根据Kayaman提出的问题:我同意我可以创建一个新的ArrayList <>(items)并添加或修改列表 . 但这不会改变从一个步骤传递到另一个步骤的主要项目列表 .

ItemWriter的接口接受List参数:

package org.springframework.batch.item;

import java.util.List;

public interface ItemWriter<T> {
    void write(List<? extends T> var1) throws Exception;
}

1 回答

相关问题