【问题标题】:Spring Batch, Access StepExecution in the writer()Spring Batch,在 writer() 中访问 StepExecution
【发布时间】:2023-04-04 19:25:01
【问题描述】:

我最近提出了这个问题,但我认为我的目标不够明确。

我有一个由读取器、处理器和写入器组成的步骤。

writer() 是一个CompositeItemWriter,由两个JdbcBatchItemWriter 组成。在其中一个中,我正在写入一个包含row_count 列的控制表。该值需要来自StepExecution.getReadCount()

我是 Spring Batch 的新手,所以我不确定如何添加一个可以将 StepExecution 包装到实际编写器本身的侦听器。

感谢您的阅读。

【问题讨论】:

    标签: spring spring-boot spring-batch


    【解决方案1】:

    您可以使用@BeforeStep annotated 方法。

    此方法在第一项写入之前调用,spring 应该将 StepExecution 传递给它。

    @BeforeStep
    public void beforeStep(StepExecution stepExecution) { ... }
    

    【讨论】:

    • 感谢您的回复,虽然它真的没有意义? BeforeStep 将在步骤执行之前运行?如果该步骤尚未执行,那么 readCount 是否为 0?
    【解决方案2】:

    您可以通过 afterStep 实现此功能
    并且将提供读取、写入、跳过和批处理状态。
    类将是:

    @StepScope  
    public class GcblRptAccItemListener {  
            @AfterStep  
            public ExitStatus afterWrite(StepExecution stepExecution) {  
                stepExecution.getReadCount();  
                stepExecution.getWriteCount();  
                return ExitStatus.COMPLETED;  
            }  
    } 
    

    你必须在配置其他读写器的地方配置这个类

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 2017-08-20
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      相关资源
      最近更新 更多