【问题标题】:Spring Batch : Execution order for multiple JobExecutionListener instances for a single jobSpring Batch:单个作业的多个 JobExecutionListener 实例的执行顺序
【发布时间】:2020-02-25 10:21:21
【问题描述】:

给定一个 Spring Batch 作业,该作业具有为其配置的JobExecutionListener 实例列表,每个侦听器的执行顺序是什么。示例:

<job id="myJob" xmlns="http://www.springframework.org/schema/batch">

        <batch:listeners>
            <batch:listener ref="myJobExecutionListener1" />
            <batch:listener ref="myJobExecutionListener2" />
            <batch:listener ref="myJobExecutionListener3" />
            <batch:listener ref="myJobExecutionListener4" />                
        </batch:listeners>

       <!-- job config continues -->
</job>

在上面的例子中,是否保证监听器将按配置顺序执行,或者监听器是否按随机顺序执行。我尝试查看 Spring Batch 参考文档,但就我的研究而言,我找不到该文档。

【问题讨论】:

    标签: java spring-batch listener order-of-execution


    【解决方案1】:

    侦听器将按其声明顺序执行。在您的示例中,它们将按以下顺序调用:

    myJobExecutionListener1.beforeJob
    myJobExecutionListener2.beforeJob
    myJobExecutionListener3.beforeJob
    myJobExecutionListener4.beforeJob
    
    // .. job output
    
    myJobExecutionListener4.afterJob
    myJobExecutionListener3.afterJob
    myJobExecutionListener2.afterJob
    myJobExecutionListener1.afterJob
    

    【讨论】:

    • @MohmoudBenHassine 感谢您回答 Spring Batch 的另一个问题。这种行为是否记录在某处以确保我们不依赖某些未记录的行为?
    • 是的,请查看CompositeJobExecutionListener的javadoc。
    • 我明白了。当我们使用侦听器列表时,Spring Batch 在内部使用CompositeJobExecutionListener。尽管如此,我觉得侦听器总是按顺序执行的行为并没有在任何地方直接记录。必须知道内部使用了CompositeJobExecutionListener 来确认这一点。
    • 请在JIRA 上打开一个带有此 SO 线程链接的问题,我们将相应地更新文档。
    • 我之前从未真正为开源项目提出过 JIRA。听起来很令人兴奋。很快就会做。
    猜你喜欢
    • 2012-06-12
    • 2014-12-10
    • 2015-11-04
    • 1970-01-01
    • 2020-06-03
    • 2013-11-05
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多