Spring Batch : A job instance already exists and is complete for parameters={}

摘要: Working with Spring Batch 2.2.0.RELEASE, and launches the job with Spring Scheduler.

Working with Spring Batch 2.2.0.RELEASE, and launches the job with 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>

Problem

The batch job is running successful in the first time only, when it launches the second time (after 10 seconds) it prompts following error messages.

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)

Solution

Refer error message above “If you want to run this job again, change the parameters.” The formula is JobInstance = JobParameters + Job. If you do not have any parameters for JobParameters, just pass a current time as parameter to create a new JobInstance. For example,

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();

References

  1. Spring Batch : Configuring and Running a Job
  2. How to create new job instance

上一篇: Spring Batch metadata tables are not created automatically?
下一篇: Java Whois example
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号