java - 未调用步骤之前的弹簧批处理,需要步骤执行上下文
问题描述
在我的春季批处理工作流配置中,我有如下项目编写器:
@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
是复合项目编写器的代表,它不会自动注册为侦听器,这应该手动完成。这是文档的拦截步骤执行部分的摘录:
If the listener is nested inside another component, it needs to be explicitly
registered (as described previously under "Registering ItemStream with a Step").
因此,您需要SkippedItemWriter
在您的步骤中明确地将 注册为侦听器。
推荐阅读
- java - 我在本地主机上的数据库和使用 Java 在线开发的网站的连接 URL 应该是什么?
- pandas - 我可以使用 .apply(lambda 函数根据条件返回值,同时跳过不满足条件的值吗?
- java - 嗨,我的 For 循环有问题,我对其进行了调试,但看不到给出结果的原因
- javascript - 将函数分配给变量而不调用
- javascript - 如何使用 JSON 从动态生成的表单中获取值
- azure-functions - Device Provisioning 自定义分配 - 在 azure 函数中设置初始 Device Twin
- python - 将值附加到三个列表的小函数
- swift - 购买成功后相关功能失效
- firebase - react-redux-firebase 没有在 firebase reducer 中填充配置文件
- python - 使用字典插入 WTForm 字段。jinja2.exceptions.UndefinedError: 'wtforms.fields.core.UnboundField object' 没有属性 'label'