Spring Batch - A job instance already exists: JobInstanceAlreadyCompleteException

a job execution for this job is already running job instance id=0, version=0
spring boot batch command line job runner
spring batch disable metadata tables
a job instance already exists and is complete for parameters spring boot
spring batch job running twice
spring batch job parameters
spring batch prevent concurrent jobs
simplejoblauncher spring batch example

I wrote an easy scheduler included in my Spring Application. I run my local server and after few seconds, in class Importer, checker.start() is being invoked each 5 seconds as how I configured in config file.

After that, this method invokes Job with JobLauncher and here I have got an error.

org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for p arameters={}. If you want to run this job again, change the parameters.

I found a solution how to fix it using annotation but I want to keep it this way.

Thank you in advance

public class Importer {

    private Checker checker;

    public Importer() {
    }

    public void myMethod() {
        try {
            checker.start(); 
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

with .xml file:

<bean id="schedulerTask"
    class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
    <property name="targetObject" ref="fileimport" />
    <property name="targetMethod" value="myMethod" />
</bean>

<bean id="fileimport" />
    <property name="targetMethod" value" class="com...Importer">
    <property name="checker">
        <bean id="checker" class="com...Checker">

        </bean>
    </property>
</bean>

<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <property name="timerTask" ref="schedulerTask" />
    <property name="delay" value="${xyz.startdelay}" />
    <property name="period" value="${xyz.checkinterval}" />
</bean>

<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <list>
            <ref local="scheduledTask" />
        </list>
    </property>
</bean>

And property file:

xyz.checkinterval=5000

xyz.startdelay=0

In class Checker I have got the method:

static ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("/simplefile-context.xml");
Job job = (Job) applicationContext.getBean("fileBatch");
JobLauncher launcher = (JobLauncher) applicationContext.getBean("jobLauncher");
public void start() throws ClientProtocolException, IOException {
    // ...
    try {
        launcher.run(job, new JobParameters());
        } catch (Exception e) {
        e.printStackTrace();
    }
}

and my "simplefile-context.xml" file looks like this:

<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    <property name="transactionManager" ref="transactionManager"/>
</bean>

<!-- bean for lauching the job -->
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
</bean>

<task:executor id="taskExecutor" pool-size="100" />

<!--                                   -->
<!--                JOBS               -->
<!--                                   -->
<batch:job id="fileBatch" restartable="true">
    <batch:step id="readLines" >
        <batch:tasklet task-executor="taskExecutor" >
            <batch:chunk reader="fileReader" writer="fooWriter" commit-interval="100" />
        </batch:tasklet>
    </batch:step>
</batch:job>

<bean id="fileReader" class="org.springframework.batch.item.file.FlatFileItemReader">
    <property name="linesToSkip" value="1"/>
    <property name="resource" value="file:./src/main/resources/sample.csv" />
    <property name="lineMapper" ref="lineMapper" />
</bean>

<bean id="lineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
    <property name="lineTokenizer" ref="lineTokenizer"/>
    <property name="fieldSetMapper" ref="fieldsetEntityMapper"/>
</bean>

<bean id="lineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
    <property name="delimiter" value=";" />
    <property name="names" value="field1,field2,field3,field4,field5,field6,field7" />
    <property name="strict" value="false"/>
</bean>

<bean id="fieldsetEntityMapper" class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
    <property name="targetType" value="com...model.ModelObject"/>
    <property name="customEditors">
        <map>
            <entry key="java.lang.Double">
                <bean class="org.springframework.beans.propertyeditors.CustomNumberEditor">
                    <constructor-arg index="0" value="java.lang.Double"/>
                    <constructor-arg index="1" value="true"/>
                </bean>
            </entry>
        </map>
    </property>
</bean>

<bean id="fooWriter" class="com...springbatch.writer.FooWriter" />

With your current setup you will only be able to launch the job only once.

    launcher.run(job, new JobParameters());

The job is unique identified by its id together with the parameters. Currently there is no way to make a distinction based on the parameters. Instead of adding new JobParameters() use the JobParamtersBuilderBuilder and add the current date and time.

JobParametersBuilder builder = new JobParametersBuilder();
builder.addDate("date", new Date());
launcher.run(job, builder.toJobParameters());

This will allow you to run the job multiple times.

Spring Batch : A job instance already exists and is complete for , JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={}. If you want to run this job again,� - Spring Batch : A job instance already exists and is complete for parameters={}

add the line to jobBuilderFactory chain

.incrementer(new RunIdIncrementer())

next (as you run job manually with jobLauncher and with custom JobParameters)

paramsBuilder.getNextJobParameters(job);

instead of .addDate("date", new Date()); like they advicing you

Understanding Jobs and Steps - Spring Batch, this job again, change the parameters.> org.springframework.batch.core. repository.JobInstanceAlreadyCompleteException: A job instance. already exists and is� 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.

Yon can also include the time to make it more unique as I was getting the same error and passing the same JobParameters in my testing.

JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .addLong("time",System.currentTimeMillis()).toJobParameters();

A job instance already exists and is complete for , JobInstanceAlreadyCompleteException: A job instance already exists and is complete for package raj.spring.batch; import java.util.Calendar� | 2 Answers. up vote 1 down vote I am seeing you are passing only one param in your job launcher as a "file name". Once the job launcher has been started it will query from the repository BATCH_JOB_EXECUTION table and will check the status of last processed job.

In Application.property file Add new fields missing.

spring.batch.initialize-schema=always spring.batch.job.enabled = false

org.springframework.batch.core.repository , Best Java code snippets using org.springframework.batch.core.repository. origin: spring-projects/spring-batch throw new JobInstanceAlreadyCompleteException( "A job instance already exists and is complete for parameters=" +� org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={}.

[YD-418] A job instance already exists and is complete for , JobInstanceAlreadyCompleteException: A job instance already exists and is complete for invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring- aop-4.3.4. RELEASE] at org.springframework.batch.core.repository.support. The launching of a Job is considered to be a 'restart' if a JobExecution already exists for the particular JobInstance. Ideally, all jobs should be able to start up where they left off, but there are scenarios where this is not possible. It is entirely up to the developer to ensure that a new JobInstance is created in this scenario.

Programming Tutorial: Spring Batch : A job instance already exists , batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={}. If you want to run� org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={-spring.cloud.task.executionid=108}. If you want to run this job again, change the parameters.

SimpleJobRepository (Spring Batch 4.2.4.RELEASE API), Check if an instance of this job already exists with the parameters provided. void, update(JobExecution jobExecution). Update the JobExecution (but not its� Try running "SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = xxx and JOB_KEY =xxx" from database client with the credentials you are using for Spring Batch datasource configuration and see if you have any access issues. – Shailendra Oct 21 '15 at 7:25