spring批处理在步骤未被调用之前,需要步骤执行上下文

um6iljoc  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(406)

在我的spring批处理工作流配置中,我的item writer如下所示:

@Bean
@Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
public ItemWriter<Price> skippedItemWriter(@Value("#{stepExecution.executionContext}") ExecutionContext executionContext) {
    return new SkippedItemWriter(executionContext);
}

public class SkippedItemWriter implements ItemWriter<Price>, StepExecutionListener {

    private static final Logger LOGGER = getLogger(SkippedItemWriter.class);

    private final ExecutionContext executionContext;
    private StepExecution stepExecution;

    public SkippedItemWriter(final ExecutionContext executionContext) {
        this.executionContext = executionContext;
    }

    @Override
    public void write(List<? extends Price> items) {

        if (CollectionUtils.isEmpty(items)) {
            return;
        }
        /blah 
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        LOGGER.info("in beforeStep");
        this.stepExecution = stepExecution;
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return null;
    }
}

我的前一步和后一步都没有接到电话。
bean必须是步骤范围的。
我正在尝试获取在前面的步骤中设置的发布计数:

ExecutionContext context = stepContribution.getStepExecution().getJobExecution().getExecutionContext();
context.putInt((PUBLISH_COUNT.name()), 0);

我试过回来 SkippedItemWriter() 而不是 ItemWriter<T> 但我又发现了一个代理错误。
我应该添加SkippeEditemWriter是复合编写器的一部分:

@Bean
@Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
public ItemWriter<Price> compositeWriter() {
    CompositeItemWriter<Price> itemWriter = new CompositeItemWriter<>();
itemWriter.setDelegates(Arrays.asList(
              skippedItemWriter(null)));

        return itemWriter;   }
dzhpxtsq

dzhpxtsq1#

原因是因为 SkippedItemWriter 是复合项编写器的委托,它不会自动注册为侦听器,这应该手动完成。以下是文档截取步骤执行部分的摘录:

If the listener is nested inside another component, it needs to be explicitly
registered (as described previously under "Registering ItemStream with a Step").

所以需要显式注册 SkippedItemWriter 作为你的听众。

相关问题