Skip to content

Commit 589c823

Browse files
committed
Fix step execution id generation in ResourcelessJobRepository
(cherry picked from commit 7d269e9)
1 parent 8809b45 commit 589c823

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/ResourcelessJobRepository.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 the original author or authors.
2+
* Copyright 2024-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,6 +49,8 @@ public class ResourcelessJobRepository implements JobRepository {
4949

5050
private JobExecution jobExecution;
5151

52+
private long stepExecutionIdIncrementer = 0L;
53+
5254
@Override
5355
public boolean isJobInstanceExists(String jobName, JobParameters jobParameters) {
5456
return false;
@@ -77,12 +79,14 @@ public void update(JobExecution jobExecution) {
7779

7880
@Override
7981
public void add(StepExecution stepExecution) {
80-
this.addAll(Collections.singletonList(stepExecution));
82+
stepExecution.setId(this.stepExecutionIdIncrementer++);
8183
}
8284

8385
@Override
8486
public void addAll(Collection<StepExecution> stepExecutions) {
85-
this.jobExecution.addStepExecutions(new ArrayList<>(stepExecutions));
87+
for (StepExecution stepExecution : stepExecutions) {
88+
this.add(stepExecution);
89+
}
8690
}
8791

8892
@Override
@@ -105,6 +109,9 @@ public void updateExecutionContext(JobExecution jobExecution) {
105109

106110
@Override
107111
public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) {
112+
if (this.jobExecution == null || !this.jobExecution.getJobInstance().getId().equals(jobInstance.getId())) {
113+
return null;
114+
}
108115
return this.jobExecution.getStepExecutions()
109116
.stream()
110117
.filter(stepExecution -> stepExecution.getStepName().equals(stepName))

spring-batch-samples/src/test/java/org/springframework/batch/samples/helloworld/HelloWorldJobFunctionalTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public void testLaunchJob() throws Exception {
4242

4343
// then
4444
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
45+
assertEquals(1, jobExecution.getStepExecutions().size());
46+
assertEquals(BatchStatus.COMPLETED, jobExecution.getStepExecutions().iterator().next().getStatus());
4547
}
4648

4749
}

0 commit comments

Comments
 (0)