Spring batch 忽略spring批处理递增器

Spring batch 忽略spring批处理递增器,spring-batch,spring-batch-admin,Spring Batch,Spring Batch Admin,我有一个spring批处理(2.2.2)应用程序,由于某些原因无法使作业参数增量计工作。该步骤声明如下: <job id="job1" xmlns="http://www.springframework.org/schema/batch" incrementer="incrementer"> <step id="step1" parent="step" /> </job> <bean id="incrementer" class="org.sp

我有一个spring批处理(2.2.2)应用程序,由于某些原因无法使作业参数增量计工作。该步骤声明如下:

<job id="job1" xmlns="http://www.springframework.org/schema/batch" incrementer="incrementer">
    <step id="step1" parent="step" />
</job>

<bean id="incrementer" class="org.springframework.batch.core.launch.support.RunIdIncrementer" />
我检查了这里的官方样品


它也有同样的问题

如果您使用
CommandLineJobRunner运行
使用
-next
选项,否则最好的解决方案是使用timestamp作业参数使每个作业实例与其他作业实例不同。

我看到没有人给您正确的答案,所以这里是它(即使它晚了一年,可能也会帮助其他作业实例):

您可以直接调用 CommandLineJobRunner类。 例如:

String[] args = new String[]{"spring/batch/jobs/helloWorldJob.xml", "helloWorldJob", "name(string)=Spring", "-next"};
CommandLineJobRunner.main(args);

基本上,当您从命令行(或者实际上是任何其他java应用程序)运行Spring Batch时,您就是这样做的。

旧问题,但仍然适用于当前版本(3.0.5):

如果您正在通过启动作业执行

JobExecution jobExecution = launcher.run(job, jobParameters);
例如,使用
simplejoblancher
类,则从不调用递增器。如果检查方法
Incrementer.getNext(JobParameters)
的“调用者”,则调用者的数量是有限的:

  • org.springframework.batch.core.launch.support.CommandLineJobRunner
CommandLineJobRunner在调用启动器之前有条件地在“-next”上调用
getNext()

    if (opts.contains("-next")) {
        JobParameters nextParameters = getNextJobParameters(job);
        Map<String, JobParameter> map = new HashMap<String, JobParameter>(nextParameters.getParameters());
        map.putAll(jobParameters.getParameters());
        jobParameters = new JobParameters(map);
    }

    JobExecution jobExecution = launcher.run(job, jobParameters);
if(opts.contains(“-next”)){
JobParameters nextParameters=getNextJobParameters(作业);
Map Map=newhashmap(nextParameters.getParameters());
map.putAll(jobParameters.getParameters());
jobParameters=新的jobParameters(映射);
}
JobExecution JobExecution=launcher.run(作业,作业参数);
  • org.springframework.batch.core.launch.support.SimpleJoboOperator
这是Spring Admin web应用程序使用的,它与CommandLineJobRunner中的实现基本相同:

if (lastInstances.isEmpty()) {
    parameters = incrementer.getNext(new JobParameters());
    if (parameters == null) {
        throw new JobParametersNotFoundException("No bootstrap parameters found for job=" + jobName);
    }
}
else {
    List<JobExecution> lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0));
    parameters = incrementer.getNext(lastExecutions.get(0).getJobParameters());
}

logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
try {
    return jobLauncher.run(job, parameters).getId();
}
catch (JobExecutionAlreadyRunningException e) {
    throw new UnexpectedJobExecutionException(String.format(ILLEGAL_STATE_MSG, "job already running", jobName,
            parameters), e);
}
if(lastInstances.isEmpty()){
parameters=incrementer.getNext(newJobParameters());
if(参数==null){
抛出新的JobParametersNotFoundException(“找不到job=“+jobName”)的引导参数;
}
}
否则{
List lastExecutions=jobExplorer.getJobExecutions(lastInstances.get(0));
parameters=incrementer.getNext(lastExecutions.get(0.getJobParameters());
}
logger.info(String.format(“尝试启动名称=%s且参数=%s”、作业名称、参数的作业”);
试一试{
返回jobLauncher.run(作业,参数).getId();
}
捕获(JobExecutionalReadyRunning异常e){
抛出新的意外JobExecutionException(String.format(非法状态)消息,“作业已在运行”,jobName,
参数(e);
}

因此,如果您使用JobLauncher类来启动作业,则在调用JobLauncher以使用所需值增强作业参数之前,必须注意调用递增器。

您是如何启动作业的?谢谢,我看到了这一点,但是解决方案,但不确定这在我的场景中如何应用,因为我正在web应用程序的war文件中运行批处理作业。我想生成一个通用的war文件,它可以部署在任何web容器中,因此任何这样的标志都应该位于war内部,而不是来自外部参数。在我文章的链接中查看整个web应用程序。
if (lastInstances.isEmpty()) {
    parameters = incrementer.getNext(new JobParameters());
    if (parameters == null) {
        throw new JobParametersNotFoundException("No bootstrap parameters found for job=" + jobName);
    }
}
else {
    List<JobExecution> lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0));
    parameters = incrementer.getNext(lastExecutions.get(0).getJobParameters());
}

logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
try {
    return jobLauncher.run(job, parameters).getId();
}
catch (JobExecutionAlreadyRunningException e) {
    throw new UnexpectedJobExecutionException(String.format(ILLEGAL_STATE_MSG, "job already running", jobName,
            parameters), e);
}