Spring 批处理 - 如何从 ItemListenerSupport 访问 ExecutionContext

Spring Batch - how to access ExecutionContext from ItemListenerSupport

我正在扩展 ItemListenerSupport 以捕获在 read/process/write 步骤中遇到的错误,例如此代码段。

   @Override
    public void onWriteError(Exception ex, List<? extends BaseDomainDataObject> items) {
        logger.error("Encountered error on write", ex);

        String msgBody = ExceptionUtils.getStackTrace(ex);
        numProcessedMap.computeIfAbsent("numErrors", val -> items.size());
        errorMap.put(numErrors.addAndGet(1), msgBody);
    }

如何将我在地图中累积的所有错误输入步骤或作业(最好)的 ExecutionContext

唯一的方法是创建 StepExecutionListener 并将 StepExecutionContext 注入 StepExecution#beforeStep() 中的自定义 ItemListenerSupport。 使用 JobExecutionContext#beforeJob().

JobExecutionContext 也是如此

或者。更简单,让您的自定义 ItemListenerSupport 实现 StepExecutionListenerJobExectutionListener