batch spring 重复执行_Spring Batch重复步骤以永无止境的循环结束

I have a spring batch job that I'd like to do the following...

Step 1 -

Tasklet - Create a list of dates, store the list of dates in the job execution context.

Step 2 -

JDBC Item Reader - Get list of dates from job execution context.

Get element(0) in dates list. Use is as input for jdbc query.

Store element(0) date is job execution context

Remove element(0) date from list of dates

Store element(0) date in job execution context

Flat File Item Writer - Get element(0) date from job execution context and use for file name.

Then using a job listener repeat step 2 until no remaining dates in the list of dates.

I've created the job and it works okay for the first execution of step 2. But step 2 is not repeating as I want it to. I know this because when I debug through my code it only breaks for the initial run of step 2.

It does however continue to give me messages like below as if it is running step 2 even when I know it is not.

2016-08-10 22:20:57.842 INFO 11784 --- [ main] o.s.batch.core.job.SimpleStepHandler : Duplicate step [readStgDbAndExportMasterListStep] detected in execution of job=[exportMasterListCsv]. If either step fails, both will be executed again on restart.

2016-08-10 22:20:57.846 INFO 11784 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [readStgDbAndExportMasterListStep]

This ends up in a never ending loop.

Could someone help me figure out or give a suggestion as to why my stpe 2 is only running once?

thanks in advance

I've added two links to PasteBin for my code so as not to pollute this post.

解决方案

Based on our discussion on Spring batch execute dynamically generated steps in a tasklet I'm trying to answer the questions on how to access jobParameter before the job is actually being executed.

I assume that there is restcall which will execute the batch. In general, this will require the following steps to be taken.

1. a piece of code that receives the rest call with its parameters

2. creation of a new springcontext (there are ways to reuse an existing context and launch the job again but there are some issues when it comes to reuse of steps, readers and writers)

3. launch the job

The simplest solution would be to store the jobparameter received from the service as an system-property and then access this property when you build up the job in step 3. But this could lead to a problem if more than one user starts the job at the same moment.

There are other ways to pass parameters into the springcontext, when it is loaded. But that depends on the way you setup your context.

For instance, if you are using SpringBoot directly for step 2, you could write a method like:

private int startJob(Properties jobParamsAsProps) {

SpringApplication springApp = new SpringApplication(.. my config classes ..);

springApp.setDefaultProperties(jobParamsAsProps);

ConfigurableApplicationContext context = springApp.run();

ExitCodeGenerator exitCodeGen = context.getBean(ExitCodeGenerator.class);

int code = exitCodeGen.getExitCode();

context.close();

return cod;

}

This way, you could access the properties as normal with standard Value- or ConfigurationProperties Annotations.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值