问题
What I want to do is use one or more ItemWriter for a single step, depending on the batch configuration.
What I need is a behaviour between a classic CompositeItemWriter (which calls all writers) and a classic ClassifierCompositeItemWriter (which calls only one writer); this behaviour would let me call one or more writer, with an external condition for each of the writers specified as delegates.
One of the solution I thought about is using a ClassifierCompositeItemWriter which contains as delegates the list of all possible combination of my writers.
To create combinations of writers, I'd need to declare as many CompositeItemWriter as needed (2n - (n + 1), where n is the total number of "real" writers). Then the Classifier would be custom-made to return a code formatted as a bit-word with each bit is the statuts of a writer (activation could depend on properties configuration or jobParameters, etc.)
Here's an example with 2 writers :
<bean class="ClassifierCompositeItemWriter">
<property name="matcherMap">
<map>
<entry key="10">
<bean ref="writer1" />
</entry>
<entry key="01">
<bean ref="writer2" />
</entry>
<entry key="11">
<bean ref="writer1and2" />
</entry>
</map>
</property>
</bean>
<bean id="writer1" />
<bean id="writer2" />
<bean id="writer1and2" class="CompositeItemWriter">
<property name="delegates">
<bean ref="writer1" />
<bean red="writer2" />
</property>
</bean>
This solution is actually working but requires a great number of CompositeItemReader as the number of "real" writers grows.
Is there a more efficient solution?
来源:https://stackoverflow.com/questions/34180075/spring-batch-one-or-more-itemwriter-among-a-list