how to best approach to use spring batch annotation or xml files ?

时间秒杀一切 提交于 2019-12-04 22:29:13

Basically good start is Spring batch official documentation. Only thing here to note is that example has one job which runs when you do mvn spring-boot:run. BatchConfiguration is example how pure java configuration can look like.

On our project we created main configuration like this:

@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);
    }        
}

And we have separate configuration in separate context for each of jobs. HelloWorldJob1JobConfig would hold everything that that job needs and class would look like BatchConfiguration from spring example. This will create everything you need except triggering so we created launchers for each job (we are launching some jobs over http, some with messaging and some manually so we needed actually to start jobs defined this way with JobLauncher).

Another good resource for integrating spring-batch with spring-boot with pure java configuration is spring batch boot web starter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!