春季批处理 - 在步骤中配置任务 有读取器和写入器?



我正在使用Spring Batch启动示例。在此示例中,我希望将基于 XML 的应用程序转换为基于注释的应用程序。但是,我正在努力使用步骤中的@Bean进行配置以创建确切的配置。

<batch:step id="step1">
<batch:tasklet>
<batch:chunk reader="paymentDataReader" writer="paymentDataWriter" commit-interval="100000">
<batch:listeners>
<batch:listener ref="paymentingStepExecutionListener" />
</batch:listeners>
</batch:chunk>
</batch:tasklet>
<batch:next on="COMPLETED" to="sendpaymentingBatchFiles" />
</batch:step>

作业配置.java

@Configuration
public class JobConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private DataSource dataSource;
@Bean
@StepScope
public PaymentContextTasklet paymentContextTasklet() {
return new PaymentContextTasklet();
}
// Either execute for "Payment" or "Order"
@Bean
public ContextDecider contextDecider() {
return new ContextDecider();
}
@Bean
public JdbcPagingItemReader<Payment> pagingItemReader(){
JdbcPagingItemReader<Payment> reader = new JdbcPagingItemReader<>();
reader.setDataSource(this.dataSource);
reader.setFetchSize(10);
reader.setRowMapper(new PaymentRowMapper());
MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
queryProvider.setSelectClause("select paymentId, amount, customerId, paymentDate");
queryProvider.setFromClause("from payment");
reader.setQueryProvider(queryProvider);
return reader;
}
@Bean
public ItemWriter<Payment> paymentItemWriter(){
return items -> {
for(Payment c : items) {
System.out.println(c.toString());
}
};
}
@Bean
public PaymentStepExecutionListener paymentStepExecutionListener() {
return new PaymentStepExecutionListener();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Payment, Payment>chunk(10)
.reader(pagingItemReader())
.writer(paymentItemWriter())
.tasklet(paymentStepExecutionListener())
.rea
.build();
}
@Bean
@StepScope
public PaymentDataTasklet paymentDataTasklet() {
return new PaymentDataTasklet();
}
@Bean
public Step paymentContextStep() {
return stepBuilderFactory.get("paymentContextStep")
.tasklet(paymentContextTasklet())
.build();
}
@Bean
public Step paymentDataStep() {
return stepBuilderFactory.get("paymentDataStep")
.tasklet(paymentDataTasklet())
.build();
}
@Bean
public Step endStep() {
return stepBuilderFactory.get("endStep")
.tasklet(null)
.build();
}
@Bean
public Job paymentDataBatchJob() {
return jobBuilderFactory.get("paymentDataBatchJob")
.start(paymentContextStep())
.next(contextDecider())
.on("Payment").to(paymentDataStep()).on("COMPLETED").to(endStep)
.from(contextDecider())
.on("Order").to(endStep()).end()
.build();
}
}
但是

我正在努力使用步骤中的@Bean进行配置以创建确切的配置。

Java config 中的 XML 代码段等效物如下:

@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Payment, Payment>chunk(100000)
.reader(paymentDataReader())
.writer(paymentDataWriter())
.listener(paymentingStepExecutionListener())
.build();
}
@Bean
public Job paymentDataBatchJob() {
return jobBuilderFactory.get("paymentDataBatchJob")
.start(step1())
.next(sendpaymentingBatchFiles())
.build();
}

感谢@Mahmoud Ben Hassine。我意识到我应该在listener而不是Tasklet中使用paymentStepExecutionListener.

还有一件事是StepExecutionListener只能在scope="step".中定义的 bean 中工作,所以我应该使用

只能在//scope="step" 中定义的 bean 中访问 stepExecutionContext。

@Bean
@StepScope
public PaymentStepExecutionListener paymentStepExecutionListener() {
return new PaymentStepExecutionListener();
}

现在一切进展顺利。

最新更新