Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleJobRepository should ignore non-identifying JobParameters #1221

Closed
spring-projects-issues opened this issue May 14, 2015 · 1 comment
Closed
Labels
for: backport-to-4.2.x Issues that will be back-ported to the 4.2.x line in: core type: bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Trevor Baker opened BATCH-2383 and commented

Using the CommandLineJobRunner I pass in some non-identifying parameters but when I rerun with the same parameter, SimpleJobRepository throws an exception:

2015-05-14 12:03:58,414 [ERROR] org.springframework.batch.core.launch.support.CommandLineJobRunner#start:368 | Job Terminated in error: A job instance already exists and is complete for parameters={report.date=1431586800000}.  If you want to run this job again, change the parameters.
org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={report.date=1431586800000}.  If you want to run this job again, change the parameters.
        at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:126)

This is because SimpleJobRepository only checks the number of JobParameters but doesn't check if they are identifying or not.

BatchStatus status = execution.getStatus();
if (execution.getJobParameters().getParameters().size() > 0 && (status == BatchStatus.COMPLETED || status == BatchStatus.ABANDONED)) {
	throw new JobInstanceAlreadyCompleteException(
			"A job instance already exists and is complete for parameters=" + jobParameters
			+ ".  If you want to run this job again, change the parameters.");
}

Related Jira: BATCH-1412


Affects: 3.0.3

Attachments:

fmbenhassine added a commit that referenced this issue May 5, 2021
Before this commit, the logic that validates if a job
instance is complete or not was based only on the size
of previous parameters, without checking if they are
identifying or not.

This commit introduces a change that uses only
identifying job parameters to identify a job instance
and validate its previous executions.

Issue #1221
@fmbenhassine
Copy link
Contributor

This is a valid issue, here is a quick way to reproduce it:

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class JobConfig {

    @Bean
    public Job job(JobBuilderFactory jobs, StepBuilderFactory steps) {
        return jobs.get("job")
                .start(steps.get("step")
                        .tasklet((contribution, chunkContext) -> {
                            Object name = chunkContext.getStepContext().getJobParameters().get("name");
                            System.out.println("hello " + name);
                            return RepeatStatus.FINISHED;
                        })
                        .build())
                .build();
    }

    public static void main(String[] args) throws Exception {
        ApplicationContext context = new AnnotationConfigApplicationContext(JobConfig.class);
        JobLauncher jobLauncher = context.getBean(JobLauncher.class);
        Job job = context.getBean(Job.class);
        JobParameters jobParameters = new JobParametersBuilder()
                .addString("name", "foo", false)
                .toJobParameters();
        jobLauncher.run(job, jobParameters);
        jobLauncher.run(job, jobParameters); // fails with: A job instance already exists and is complete for parameters={name=foo}
    }

}

Fixed in 7f0824b by applying a slightly modified version of the attached patch.

@fmbenhassine fmbenhassine added for: backport-to-4.2.x Issues that will be back-ported to the 4.2.x line and removed status: waiting-for-triage Issues that we did not analyse yet labels May 5, 2021
@fmbenhassine fmbenhassine added this to the 4.3.3 milestone May 5, 2021
fmbenhassine added a commit that referenced this issue May 6, 2021
Before this commit, the logic that validates if a job
instance is complete or not was based only on the size
of previous parameters, without checking if they are
identifying or not.

This commit introduces a change that uses only
identifying job parameters to identify a job instance
and validate its previous executions.

Issue #1221
@fmbenhassine fmbenhassine changed the title SimpleJobRepository should ignore non-identifying JobParameters [BATCH-2383] SimpleJobRepository should ignore non-identifying JobParameters May 11, 2021
fmbenhassine added a commit that referenced this issue May 11, 2021
Before this commit, the logic that validates if a job
instance is complete or not was based only on the size
of previous parameters, without checking if they are
identifying or not.

This commit introduces a change that uses only
identifying job parameters to identify a job instance
and validate its previous executions.

Issue #1221
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: backport-to-4.2.x Issues that will be back-ported to the 4.2.x line in: core type: bug
Projects
None yet
Development

No branches or pull requests

2 participants