![](/img/trans.png)
[英]How to handle Spring Batch step execution context that is too large?
[英]spring batch before step not getting called, need step execution context
在我的 spring 批处理工作流配置中,我有如下项目编写器:
@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;
}
}
我的前一步和后一步没有被调用。
beans 必须是步骤范围的。
我正在尝试获取我在前面步骤中设置的发布计数:
ExecutionContext context = stepContribution.getStepExecution().getJobExecution().getExecutionContext();
context.putInt((PUBLISH_COUNT.name()), 0);
我尝试返回SkippedItemWriter()
而不是ItemWriter<T>
但我收到另一个代理错误。
我应该添加 SkippedItemWriter 是复合编写器的一部分:
@Bean
@Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
public ItemWriter<Price> compositeWriter() {
CompositeItemWriter<Price> itemWriter = new CompositeItemWriter<>();
itemWriter.setDelegates(Arrays.asList(
skippedItemWriter(null)));
return itemWriter; }
原因是因为您的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
显式注册为侦听器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.