1
votes

I am using Spring batch. I have 2 Item Procesors which are used for doing the processing logic.

I have configured the listener for both ItemReader and ItemWriter.

I have used Spring Batch CompositeItemProcessor (org.springframework.batch.item.support.CompositeItemProcessor).

My Job configuration is as follows :-

<job id="SoDJob" xmlns="http://www.springframework.org/schema/batch">
        <step id="step1">
            <tasklet>
                <chunk reader="itemReader"  processor="SoDConflictProcessor" writer="SoDConflictExcelWriter"
                    commit-interval="1" />                  
            <listeners>
                    <listener ref="sodJobListener" />
                    <listener ref="SoDItemReaderListener" />
                    <listener ref="SoDItemWriterListener" />
            </listeners>                    
            </tasklet>
        </step>
    </job>

My Processors are configured as :-

<bean id="SoDConflictProcessor"
class="org.springframework.batch.item.support.CompositeItemProcessor">
<property name="delegates">
<list>
<ref bean="SoDDataProcessor" />
<ref bean="SoDLogicProcessor" />
</list>
</property>
</bean>

How Can i write individual ItemProcessListener for both ItemProcessor (SoDDataProcessor and SoDLogicProcessor).

FYI :

1) SoDDataProcessor - implements ItemProcessor<User, HashSet<String>>.

2) SoDLogicProcessor - implements ItemProcessor<HashSet<String>, HashSet<Object>>

Hope this clears my question.

1

1 Answers

3
votes

There is a CompositeItemProcessListener but this doesn't fit your requirement because - I think - your request is to have SoDDataProcessorListener called when SoDDataProcessor process an item and SoDLogicProcessorListener called when the item transformed from previous step pass into SoDLogicProcessor.
This is not possible for two reasons:

  1. ItemProcessorListener is called around your CompositeItemProcessor and not called around its delegated ItemProcessors
  2. CompositeItemProcessListener will run into a ClassCastException because is signature must be CompositeItemProcessListener<User,HashSet<Object>> and when receiving HashSet<String> will thrown an exception

You have to resolve in another way, this depends by your needs; I'll give you just an idea for your question, but you can also reconsider your design to achieve the same result

My idea:
Store intermediate transformed data in StepExecutionContext, write your own ItemProcessorListener that delegate each intermediate result to SoDDataProcessorListener, SoDLogicProcessorListener and others if you (will) have more and dispatch data to correct listener; in this way you can also write specific classes to mantain listeners separated.

Hope can help.