Skip to content

JobLauncherApplicationRunner does not follow expected behavior with a JobParametersIncrementer #22602

@touchrock

Description

@touchrock

Expected Behavior:

A JobParametersIncrementer is expected to modify the given JobParameters to provide a modified set of JobParameters. If the new set of JobParameters is unique for the Job then a new JobInstance will be created.

Actual Behavior:

Before the JobParametersIncrementer is executed, a JobInstance is found in the "if" statement on line 206 in method getNextJobParameters(Job job, JobParameters jobParameters) of JobLauncherApplicationRunner.

	private JobParameters getNextJobParameters(Job job, JobParameters jobParameters) {
		if (this.jobRepository != null && this.jobRepository.isJobInstanceExists(job.getName(), jobParameters)) {
			return getNextJobParametersForExisting(job, jobParameters);
		}
		if (job.getJobParametersIncrementer() == null) {
			return jobParameters;
		}
		JobParameters nextParameters = new JobParametersBuilder(jobParameters, this.jobExplorer)
				.getNextJobParameters(job).toJobParameters();
		return merge(nextParameters, jobParameters);
	}

The method getNextJobParametersForExisting(Job job, JobParameters jobParameters) is then executed which in the case of a 'COMPLETED' JobExecution will return the original JobParameters object.

	private JobParameters getNextJobParametersForExisting(Job job, JobParameters jobParameters) {
		JobExecution lastExecution = this.jobRepository.getLastJobExecution(job.getName(), jobParameters);
		if (isStoppedOrFailed(lastExecution) && job.isRestartable()) {
			JobParameters previousIdentifyingParameters = getGetIdentifying(lastExecution.getJobParameters());
			return merge(previousIdentifyingParameters, jobParameters);
		}
		return jobParameters;
	}

Use Case

Restarting a 'COMPLETED' job using a JobParameterIncrementer. Below is the JobParameterIncrementer expected to provide a unique set of JobParameters for each execution.

package util;

import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersIncrementer;

public class ForceRestart implements JobParametersIncrementer {
    
    private static final long JOB_PARAMETER_MAXIMUM = 1000000;

    @Override
    public JobParameters getNext(JobParameters params) {
        
        Long random = (long) (Math.random() * JOB_PARAMETER_MAXIMUM);

        return new JobParametersBuilder(params).addLong("random", random).toJobParameters();
    }
}

Version: 2.2.6.RELEASE

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions