多次运行spring批处理作业 [英] Running a spring batch job multiple times

查看:524
本文介绍了多次运行spring批处理作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下教程创建了一个带有spring boot的spring批处理作业:

I have created a spring batch job with spring boot using below tutorial:

https://spring.io/guides/gs/batch-processing/

作业正在读取文件并按预期写入数据库。

The job is reading a file and writing to a database as expected.

但是,现在我有一个用例来多次运行这个作业。

However, now I have a use case to run this job multiple times.

我有一个 ArrayList 的参数。

我应该对工作做些什么改动,以便我可以按照 ArrayList

What changes should I do to the job so that I can run the job the number of times the size of my ArrayList ?

推荐答案

您可以像这样手动启动批处理作业

You can kickstart your batch job manually like this

@Component
Class Someclass{
  ...............
     @Autowired
     private JobLauncher jobLauncher;
     @Autowired
     private Job job;  

     public void someFunction(){
       jobLauncher.run(job, new JobParameters());  
   }
}

如果它不能重新启动批处理作业已经完成,它会抛出一个错误,说明状态为COMPLETED。为此,您必须将 allowStartIfComplete 属性设置为true。这必须在您的批处理步骤配置中完成,类​​似这样

Only thing is you cannot restart a batch job if it is already completed, It throws an error saying the status is COMPLETED. For this to work you have to set allowStartIfComplete property to true. This has to be done in your batch step configuration, something like this

stepBuilderFactory.get("step1")
                .<Person, Person> chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .allowStartIfComplete(true)
                .build();

这篇关于多次运行spring批处理作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆