【发布时间】:2021-02-09 10:02:22
【问题描述】:
我最近在春季批次中偶然发现了一个非常扭曲的问题。 要求如下:
我有两个主要步骤:
- 第一个从oracle数据库中读取一些数据,从一个表中写入另一个表。
- 第二个基于第一步处理的数据执行其他一些数据库工作。
从设计的角度来看,第一步是这样的:
@Bean
public Step myFirstStep(JdbcCursorItemReader<Revision> reader) {
return stepBuilderFactory.get("my-first-step")
.<Revision, Revision>chunk(1)
.reader(readerRevisionNumber)
.writer(compositeItemWriter())
.listener(executionContextPromotionListener())
.build();
复合项目编写器:
@Bean
public CompositeItemWriter<Revision> compositeItemWriter() {
CompositeItemWriter writer = new CompositeItemWriter();
writer.setDelegates(Arrays.asList(somewriter(), someOtherwriter(), aWriterThatIsSupposedToPassDataToAnotherStep()));
return writer;
}
虽然前两位作者并不复杂,但我的兴趣集中在第三位。
aWriterThatIsSupposedToPassDataToAnotherStep()
正如您可能已经猜到的那样,这个将用于获取之前正在处理的一些数据,以便在我的第二步中推广它:
@Component
@StepScope
public class AWriterThatIsSupposedToPassDataToAnotherStep implements ItemWriter<SomeEntity> {
private StepExecution stepExecution;
public void write(List<? extends SomeEntity> items) {
ExecutionContext stepContext = this.stepExecution.getExecutionContext();
stepContext.put("revisionNumber", items.stream().findFirst().get().getSomeField());
System.out.println("writing : " + items.stream().findFirst().get().getSomeField()+ "to ExecutionContext");
}
@BeforeStep
public void saveStepExecution(StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
}
问题是:只要该作者是复合作者列表的一部分(如上所述) 我最后一位作者的 @BeforeStep 从未被执行,这最终导致我无法将我的信息传输到执行上下文。 在步骤定义中用我的单个“AWriterThatIsSupposedToPassDataToAnotherStep”替换我的 CompositeItemWriter 时,它会正确执行。
它与某种声明顺序或其他东西有什么关系吗?
非常感谢进一步的帮助。
【问题讨论】:
标签: spring-batch