触发Spring Batch任务时RunIdIncrementer未生成run.id参数
Spring Batch RunIdIncrementer未自动传入run.id参数问题
我在定义Spring Batch任务Bean时添加了RunIdIncrementer,期望触发任务时自动传入run.id作为任务参数,但日志显示该参数并未被传入。
任务Bean代码
public Job firstPartyStatsIngestionJob() { return jobBuilderFactory.get(TradingCenterJobName.firstPartyStatsIngestionJob.name()) .incrementer(new RunIdIncrementer()) .start(getFirstPartyIngestionStep()) .build(); }
日志输出
[INFO ] 2022-12-16 09:04:07 [main ] [/] [] JobLauncherApplicationRunner:154 - Running default command line with: [] [INFO ] 2022-12-16 09:04:07 [main ] [/] [] SimpleJobLauncher:146 - Job: [SimpleJob: [name=firstPartyStatsIngestionJob]] launched with the following parameters: [{}]
关键依赖版本
- spring-boot: 2.5.12
- spring-batch-core: 4.3.5
解决方案
1. 明确RunIdIncrementer的工作逻辑
RunIdIncrementer的作用是基于任务的历史执行记录生成递增的run.id参数。如果是任务首次执行且无任何历史记录,它不会自动生成参数,必须有初始参数触发增量逻辑。
2. 自定义任务启动逻辑
Spring Boot默认的JobLauncherApplicationRunner不会主动调用Job定义中的Incrementer生成参数,需自定义ApplicationRunner显式触发:
@Component public class CustomJobLauncherRunner implements ApplicationRunner { private final JobLauncher jobLauncher; private final Job firstPartyStatsIngestionJob; private final JobParametersIncrementer incrementer; public CustomJobLauncherRunner(JobLauncher jobLauncher, Job firstPartyStatsIngestionJob) { this.jobLauncher = jobLauncher; this.firstPartyStatsIngestionJob = firstPartyStatsIngestionJob; this.incrementer = new RunIdIncrementer(); } @Override public void run(ApplicationArguments args) throws Exception { JobParameters jobParameters = incrementer.getNext(firstPartyStatsIngestionJob.getJobParameters()); jobLauncher.run(firstPartyStatsIngestionJob, jobParameters); } }
3. 手动传入初始参数
若不想自定义启动逻辑,可在首次启动时通过命令行传入初始参数,后续执行时RunIdIncrementer会自动递增run.id:
java -jar your-app.jar --spring.batch.job.parameters=run.id=1
内容的提问来源于stack exchange,提问作者divyang
相关产品推荐
相关产品推荐

