Spring Batch重复步骤以永无止境的循环结束 [英] Spring batch repeat step ending up in never ending loop

查看:921
本文介绍了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.

我已经创建了作业,并且可以在步骤2的第一次执行中正常工作.但是步骤2并没有按照我想要的重复.我知道这是因为,当我调试代码时,它只会在步骤2的初始运行中中断.

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.

但是,即使我知道不是,它仍会继续向我发送如下消息,好像它正在运行第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.

有人可以帮我找出问题或提出建议,为什么我的stpe 2只运行一次吗?

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

预先感谢

我为代码添加了两个指向PasteBin的链接,以免污染这篇文章.

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

http://pastebin.com/QhExNikm (作业配置)

http://pastebin.com/sscKKWRk (通用作业配置)

http://pastebin.com/Nn74zTpS (步骤执行监听器)

推荐答案

基于我们对

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.

我假设有将执行该批处理的restcall.通常,这将需要采取以下步骤. 1.一段代码,接收带有其参数的剩余调用 2.创建一个新的springcontext(有一些方法可以重用现有的上下文并再次启动工作,但是在重用步骤,读取器和编写器时会遇到一些问题) 3.启动工作

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

最简单的解决方案是将从服务接收的jobparameter存储为系统属性,然后在步骤3中建立作业时访问此属性.但是,如果有多个用户启动该作业,可能会导致问题.同时工作.

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.

还有其他方法可以在加载springcontext时将参数传递给它.但这取决于您设置上下文的方式. 例如,如果您直接将SpringBoot用于步骤2,则可以编写如下方法:

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

这样,您可以使用标准的Value-或ConfigurationProperties批注正常访问属性.

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

这篇关于Spring Batch重复步骤以永无止境的循环结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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