开发者

Spring Batch base step with listeners

I would like to setup a base step has a logger listener attached for making sure that all the error logs end up in the correct file.

The setup below i simple enough and rather similar to the example presented on http://static.springsource.org/spring-batch/reference/html/configureStep.html#mergingListsOnStep but according http://www.springframework.org/schema/batch/spring-batch.xsd, it seems that listeners don't belong under step but rather under tasklet.

<step id="baseLoggedStep">
    <listeners>
        <listener>
            <bean class="org.example...StepLogListener"/>
        </listener>
    </listeners>
</step>

<step id="myJobStep" parent="baseLoggedStep">
... 
</step>

So, who is correct and how do I use the correct xsd to produce the desired result?

The following base step seems to do the trick, where StepLogListener listener implements StepExecutionListener.

<batch:step id="baseLoggedStep" abstract="true">
    <batch:tasklet>
        <batch:listeners>
            <batch:listener ref="stepLogListener">
                &开发者_开发百科lt;bean class="com.bossmedia.gem.platform.batch.StepLogListener"/>
            </batch:listener>
        </batch:listeners>
    </batch:tasklet>
</batch:step>

However it doesn't seem optimal and frankly not entirely correct. This would mean that baseLoggedStep is an abstract instance of TaskletStep right?


According to Spring Batch 2.1 XSD all you would need to do is make your first baseLoggedStep step example abstract (see 5.1.2.1. Abstract Step).

Be aware of the fact that if your child jobs add their own listeners they will have to merge them (see 5.1.2.2. Merging Lists) or the listener(s) from your abstract parent step will be ignored.


Just putting this example of a Spring Batch Step Execution Listener out there since I didn't see a good one online. Plus, the Spring reference example will not run as written.

<beans:bean id="customStepExecutionListener" class="com.foo.MyCustomStepExecutionListener" />
<beans:bean id="customJobExecutionListener" class="com.foo.MyCustomJobExecutionListener" />

<!-- Abstract step listener must be defined outside job -->     
<step id="abstractListeningStep" abstract="true">
    <!-- Must be inside tasklet, but tasklet does not affect firing order -->
    <tasklet>
        <listeners>
            <listener ref="customStepExecutionListener" />
        </listeners>
    </tasklet>  
</step>

<!-- The job -->
<job id="myJob">
    <description>My Job</description>

    <listeners>
        <!-- Any job specific listeners here -->
        <listener ref="customJobExecutionListener" />
    </listeners>

    <!-- Start here -->
    <step id="myFirstStep" parent="abstractListeningStep">
        <!-- Your work/jobs/tasklets here -->
    </step>
</job>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜