Skip to content

JobLauncherApplicationRunner returns a success exit code even when no jobs have been run. #35940

@umbum

Description

@umbum

When using JobLauncherApplicationRunner, if we pass an arbitrary JobName, the spring application terminates with a success exit code without failure.

If the job name is entered incorrectly by mistake, it could be mistaken for success even though no job was actually executed.

It would be good to add validation whether the job name is defined in application.

sol. 1

public class JobLauncherApplicationRunner implements ApplicationRunner, Ordered, ApplicationEventPublisherAware {
    ...
	@Value("${spring.batch.job.fail-on-unknown-name:false}")
	private boolean failOnUnknownName;

	@PostConstruct
	public void validate() {
		if (this.jobs.size() > 1 && !StringUtils.hasText(this.jobName)) {
			throw new IllegalArgumentException("Job name must be specified in case of multiple jobs");
		}

		if (failOnUnknownName == true && this.jobs.stream().noneMatch(job -> StringUtils.equals(this.jobName, job.getName()))) {
			throw IllegalArgumentException("No such job name : [$jobName]")
		}
	}

sol. 2

public class JobExecutionExitCodeGenerator implements ApplicationListener<JobExecutionEvent>, ExitCodeGenerator {
	@Override
	public int getExitCode() {
		if (this.executions.isEmpty()) {
			return 127;
		}

		for (JobExecution execution : this.executions) {
			if (execution.getStatus().ordinal() > 0) {
				return execution.getStatus().ordinal();
			}
		}
		return 0;
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: supersededAn issue that has been superseded by anothertype: bugA general bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions