Spring Batch:作业实例已经存在,并且对于参数= {}已完成

在使用Spring Batch 2.2.0.RELEASE时,遇到作业实例已存在并完成的问题,导致作业无法再次启动。错误信息提示需要更改参数以重新运行作业。解决方法是通过传递当前时间作为参数来创建新的作业实例。
摘要由CSDN通过智能技术生成

使用Spring Batch 2.2.0.RELEASE,并使用Spring Scheduler启动作业。

CustomJobLauncher.java
package com.mkyong.batch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CustomJobLauncher {

	@Autowired
	JobLauncher jobLauncher;

	@Autowired
	Job job;

	public void run() {

	  try {

		JobExecution execution = jobLauncher.run(job, new JobParameters());
		System.out.println("Exit Status : " + execution.getStatus());
			
	  } catch (Exception e) {
		e.printStackTrace();
	  }

	}

}
job-config.xml
<bean id="customJobLauncher" class="com.mkyong.batch.CustomJobLauncher" />

  <task:scheduled-tasks>
	<task:scheduled ref="customJobLauncher" method="run" fixed-delay="10000" />
  </task:scheduled-tasks>

问题

批处理作业仅在第一次成功运行,第二次启动(10秒后)时,将提示以下错误消息。

org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: 
	A job instance already exists and is complete for parameters={}.  
        If you want to run this job again, change the parameters.
	at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:126)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

请参考上面的错误消息“ 如果要再次运行此作业,请更改参数。 公式是JobInstance = JobParameters + Job 。 如果您没有JobParameters任何参数,只需将当前时间作为参数来创建一个新的JobInstance 。 例如,

CustomJobLauncher.java
//...

@Component
public class CustomJobLauncher {

	@Autowired
	JobLauncher jobLauncher;

	@Autowired
	Job job;

	public void run() {

	  try {
		JobParameters jobParameters = 
		  new JobParametersBuilder()
		  .addLong("time",System.currentTimeMillis()).toJobParameters();
			
		JobExecution execution = jobLauncher.run(job, jobParameters);
		System.out.println("Exit Status : " + execution.getStatus());
			
	  } catch (Exception e) {
		e.printStackTrace();
	  }

	}

}

参考文献

  1. Spring Batch:配置和运行作业
  2. 如何创建新的作业实例

翻译自: https://mkyong.com/spring-batch/spring-batch-a-job-instance-already-exists-and-is-complete-for-parameters/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值