【发布时间】:2021-04-13 12:11:56
【问题描述】:
在我的春季批处理工作流配置中,我的项目编写器如下:
@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>,但我收到另一个代理错误。
我应该添加 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否则 Spring 不会将其检测为StepExecutionListener。 -
我试过了,但没用。我应该添加这个特定的编写器是作为返回 ItemWriter 的复合编写器的一部分的委托
-
那么您需要手动将其注册为您的步骤的侦听器,除非该委托也是可以委托给其编写者的
StepExecutionListener。 -
你能举个例子吗?我的作家实现了 StepExecutionListener。我已经添加了一个侦听器... .
listener(promoteContextListener())with keys -
您的编写者实现了组合没有实现,而组合是该步骤看到的而不是您自己的实现。因此,您需要手动将您的
skippedItemWriter注册为您的步骤的侦听器。
标签: java spring spring-batch