如何最好地使用Spring Batch批注或XML文件? [英] how to best approach to use spring batch annotation or xml files ?
本文介绍了如何最好地使用Spring Batch批注或XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先感谢您的关注,在我的春批项目中定义了很多作业,例如:
<batch:job id="helloWorldJob1" job-repository="jobRepository">
<batch:step id="step1" >
<batch:tasklet>
<batch:chunk reader="itemReader1" writer="itemWriter1"
processor="itemProcessor1">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<batch:job id="helloWorldJob2" job-repository="jobRepository">
<batch:step id="step1" >
<batch:tasklet>
<batch:chunk reader="itemReader2" writer="itemWriter2"
processor="itemProcessor2">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<batch:job id="helloWorldJob3" job-repository="jobRepository">
<batch:step id="step1" >
<batch:tasklet>
<batch:chunk reader="itemReader3" writer="itemWriter3"
processor="itemProcessor3">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
.
.
.
.
如何使用纯批注?这是正确的方法吗?
推荐答案
基本上开局良好Spring batch official documentation。这里唯一需要注意的是,当您执行
mvn spring-boot:run
时,该示例有一个运行的作业。BatchConfiguration
是纯Java配置的示例。
在我们的项目上,我们创建了如下主配置:
@Configuration
@EnableBatchProcessing(modular = true)
public class JobContextConfig {
@Autowired
private JobRepository jobRepository;
@Bean
public ApplicationContextFactory helloWorldJob1Job() {
return new GenericApplicationContextFactory(HelloWorldJob1JobConfig.class);
}
@Bean
public ApplicationContextFactory helloWorldJob2Job() {
return new GenericApplicationContextFactory(HelloWorldJob2JobConfig.class);
}
@Bean
public ApplicationContextFactory helloWorldJob3Job() {
return new GenericApplicationContextFactory(HelloWorldJob3JobConfig.class);
}
}
我们在每个作业的单独上下文中都有单独的配置。HelloWorldJob1JobConfig
将保存作业所需所有内容,类看起来像Spring示例中的BatchConfiguration
。这将创建除触发之外所需的所有内容,因此我们为每个作业创建了启动器(我们通过http启动一些作业,一些通过消息传递启动,另一些手动启动,因此我们实际上需要启动通过JobLauncher
以这种方式定义的作业)。
将spring-batch
和spring-boot
与纯Java配置集成的另一个很好的资源是spring batch boot web starter。
这篇关于如何最好地使用Spring Batch批注或XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文