1

我有一个 Sprint 批次,其工作为

<batch:job id="data-send" parent="baseJob">
    <batch:listeners merge="true">
        <batch:listener ref="someJobExecutionListener"/>
    </batch:listeners>
    <batch:step id="data-send.step01">
        <batch:tasklet transaction-manager="jobTransactionManager" ref="dataSendTasklet"/>
    </batch:step>
</batch:job>

tasklet 伪代码是

@Component
public class DataSendTasklet implements Tasklet {
 
@Override
public RepeatStatus execute((final StepContribution contribution, final ChunkContext chunkContext) throws Exception {

   List<SomeObject> objectList = dbGetOperation();
   objectList.stream().forEach(o -> someDataSendOperation(o));
   log("ReadCount: "+objectList.size());
   return RepeatStatus.FINISHED;
}

目前,我通过计算 objectList 的大小来记录读取计数,如果我必须记录已处理的记录和错误记录,我将不得不跟踪 someDataSendOperation 方法中的变量。在这种情况下,是否有任何 Spring 现成的功能来处理读取、写入和错误计数?

4

2 回答 2

1

我有一个类似的用例,我创建了一个 StepListener 并打印所有有用的信息。这是示例:

public class StepExecuteListener extends StepExecutionListenerSupport {
private static final Logger LOG = LoggerFactory.getLogger(StepExecuteListener.class);

@Override
public void beforeStep(StepExecution stepExecution) {
    // No action needed
}

@Override
public ExitStatus afterStep(StepExecution stepExecution) {
    
    LOG.debug("StepExecutionListener - afterStep:getCommitCount= {}", stepExecution.getCommitCount());
    LOG.debug("StepExecutionListener - afterStep:getFilterCount= {}", stepExecution.getFilterCount());
    LOG.debug("StepExecutionListener - afterStep:getProcessSkipCount= {}", stepExecution.getProcessSkipCount());
    LOG.info("StepExecutionListener - afterStep:getReadCount= {}", stepExecution.getReadCount());
    LOG.debug("StepExecutionListener - afterStep:getReadSkipCount= {}", stepExecution.getReadSkipCount());
    LOG.debug("StepExecutionListener - afterStep:getRollbackCount= {}", stepExecution.getRollbackCount());
    LOG.info("StepExecutionListener - afterStep:getWriteCount= {}", stepExecution.getWriteCount());
    LOG.debug("StepExecutionListener - afterStep:getWriteSkipCount= {}", stepExecution.getWriteSkipCount());
    LOG.debug("StepExecutionListener - afterStep:getStepName= {}", stepExecution.getStepName());
    LOG.debug("StepExecutionListener - afterStep:getSummary= {}", stepExecution.getSummary());
    LOG.debug("StepExecutionListener - afterStep:getStartTime= {}", stepExecution.getStartTime());
    LOG.debug("StepExecutionListener - afterStep:getStartTime= {}", stepExecution.getEndTime());
    LOG.debug("StepExecutionListener - afterStep:getLastUpdated= {}", stepExecution.getLastUpdated());
    LOG.debug("StepExecutionListener - afterStep:getExitStatus= {}", stepExecution.getExitStatus());
    LOG.debug("StepExecutionListener - afterStep:getFailureExceptions= {}", stepExecution.getFailureExceptions());
   
    
    return null;
}
   }
于 2020-08-03T20:00:44.470 回答
0

我不明白为什么您使用简单的 tasklet 而不是使用面向块的步骤。您可以使用 anItemReader<SomeObject>和 anItemWriter<SomeObject>来执行someDataSendOperation.

这样,您无需自己遍历列表,您将获得所需的计数。

于 2020-08-14T09:31:01.880 回答