Skip to content

Commit 7d269e9

Browse files
committed
Fix step execution id generation in ResourcelessJobRepository
1 parent 76a6550 commit 7d269e9

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.springframework.batch.core.repository.support;
1717

1818
import java.time.LocalDateTime;
19-
import java.util.ArrayList;
2019
import java.util.Collection;
2120
import java.util.Collections;
2221
import java.util.List;
@@ -51,6 +50,8 @@ public class ResourcelessJobRepository implements JobRepository {
5150

5251
private JobExecution jobExecution;
5352

53+
private long stepExecutionIdIncrementer = 0L;
54+
5455
/*
5556
* ===================================================================================
5657
* Job operations
@@ -189,6 +190,9 @@ public StepExecution getStepExecution(Long jobExecutionId, Long stepExecutionId)
189190
@Override
190191
@Nullable
191192
public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) {
193+
if (this.jobExecution == null || !this.jobExecution.getJobInstance().getId().equals(jobInstance.getId())) {
194+
return null;
195+
}
192196
return this.jobExecution.getStepExecutions()
193197
.stream()
194198
.filter(stepExecution -> stepExecution.getStepName().equals(stepName))
@@ -206,12 +210,14 @@ public long getStepExecutionCount(JobInstance jobInstance, String stepName) {
206210

207211
@Override
208212
public void add(StepExecution stepExecution) {
209-
this.addAll(Collections.singletonList(stepExecution));
213+
stepExecution.setId(this.stepExecutionIdIncrementer++);
210214
}
211215

212216
@Override
213217
public void addAll(Collection<StepExecution> stepExecutions) {
214-
this.jobExecution.addStepExecutions(new ArrayList<>(stepExecutions));
218+
for (StepExecution stepExecution : stepExecutions) {
219+
this.add(stepExecution);
220+
}
215221
}
216222

217223
@Override

spring-batch-samples/src/main/java/org/springframework/batch/samples/helloworld/HelloWorldJobConfiguration.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,17 @@
1616
package org.springframework.batch.samples.helloworld;
1717

1818
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
19-
import org.springframework.batch.core.configuration.annotation.EnableJdbcJobRepository;
2019
import org.springframework.batch.core.job.Job;
21-
import org.springframework.batch.core.step.Step;
2220
import org.springframework.batch.core.job.builder.JobBuilder;
2321
import org.springframework.batch.core.repository.JobRepository;
22+
import org.springframework.batch.core.step.Step;
2423
import org.springframework.batch.core.step.builder.StepBuilder;
2524
import org.springframework.batch.repeat.RepeatStatus;
26-
import org.springframework.batch.samples.common.DataSourceConfiguration;
2725
import org.springframework.context.annotation.Bean;
2826
import org.springframework.context.annotation.Configuration;
29-
import org.springframework.context.annotation.Import;
3027

3128
@Configuration
3229
@EnableBatchProcessing
33-
@EnableJdbcJobRepository
34-
@Import(DataSourceConfiguration.class)
3530
public class HelloWorldJobConfiguration {
3631

3732
@Bean

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)