Spring Batch CompositeItemProcessor get value from other delegates

巧了我就是萌 提交于 2021-02-11 14:40:35

问题


I have a compositeItemProcessor as below

<bean id="compositeItemProcessor" class="org.springframework.batch.item.support.CompositeItemProcessor">
        <property name="delegates">
            <list>
                <bean class="com.example.itemProcessor1"/>
                <bean class="com.example.itemProcessor2"/>
                <bean class="com.example.itemProcessor3"/>
                <bean class="com.example.itemProcessor4"/>
            </list>
        </property>
    </bean>

The issue i have is that within itemProcessor4 i require values from both itemProcessor1 and itemProcessor3.

I have looked at using the Step Execution Context but this does not work as this is within one step. I have also looked at using @AfterProcess within ItemProcessor1 but this does not work as it isn't called until after ItemProcessor4.

What is the correct way to share data between delegates in a compositeItemProcessor?

Is a solution of using util:map that is updated in itemProcessor1 and read in itemProcessor4 under the circumstances that the commit-interval is set to 1?


回答1:


Using the step execution context won't work as it is persisted at chunk boundary, so it can't be shared between processors within the same chunk.

AfterProcess is called after the registered item processor, which is the composite processor in your case (so after ItemProcessor4). This won't work neither.

The only option left is to use some data holder object that you share between item processors.

Hope this helps.




回答2:


This page seems to state that there are two types of ExecutionContexts, one at step-level, one at job-level.

https://docs.spring.io/spring-batch/trunk/reference/html/patterns.html#passingDataToFutureSteps

You should be able to get the job context and set keys on that, from the step context



来源:https://stackoverflow.com/questions/54905582/spring-batch-compositeitemprocessor-get-value-from-other-delegates

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