Skip to content

Commit 8ce37d1

Browse files
committed
Fix step execution retrieval in SimpleJobExplorer#getLastJobExecution
Before this commit, SimpleJobExplorer#getLastJobExecution returned the last job execution without fetching its step executions and their execution contexts. This commit fixes the implementation to load the entire object graph as done in SimpleJobExplorer#getJobExecution. Resolves #3943 #3944
1 parent 2754018 commit 8ce37d1

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 the original author or authors.
2+
* Copyright 2006-2021 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.
@@ -99,7 +99,14 @@ public List<JobExecution> getJobExecutions(JobInstance jobInstance) {
9999
*/
100100
@Nullable
101101
public JobExecution getLastJobExecution(JobInstance jobInstance) {
102-
return jobExecutionDao.getLastJobExecution(jobInstance);
102+
JobExecution lastJobExecution = jobExecutionDao.getLastJobExecution(jobInstance);
103+
if (lastJobExecution != null) {
104+
getJobExecutionDependencies(lastJobExecution);
105+
for (StepExecution stepExecution : lastJobExecution.getStepExecutions()) {
106+
getStepExecutionDependencies(stepExecution);
107+
}
108+
}
109+
return lastJobExecution;
103110
}
104111

105112
/*

spring-batch-core/src/test/java/org/springframework/batch/core/explore/support/SimpleJobExplorerIntegrationTests.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2017 the original author or authors.
2+
* Copyright 2013-2021 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.
@@ -24,6 +24,7 @@
2424
import test.jdbc.datasource.DataSourceInitializer;
2525

2626
import org.springframework.batch.core.BatchStatus;
27+
import org.springframework.batch.core.Job;
2728
import org.springframework.batch.core.JobExecution;
2829
import org.springframework.batch.core.JobInstance;
2930
import org.springframework.batch.core.JobInterruptedException;
@@ -32,6 +33,7 @@
3233
import org.springframework.batch.core.StepExecution;
3334
import org.springframework.batch.core.UnexpectedJobExecutionException;
3435
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
36+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
3537
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
3638
import org.springframework.batch.core.configuration.xml.DummyStep;
3739
import org.springframework.batch.core.explore.JobExplorer;
@@ -41,6 +43,7 @@
4143
import org.springframework.batch.core.job.flow.support.StateTransition;
4244
import org.springframework.batch.core.job.flow.support.state.EndState;
4345
import org.springframework.batch.core.job.flow.support.state.StepState;
46+
import org.springframework.batch.core.launch.JobLauncher;
4447
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
4548
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
4649
import org.springframework.batch.core.repository.JobRepository;
@@ -54,6 +57,7 @@
5457
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
5558

5659
import static org.junit.Assert.assertEquals;
60+
import static org.junit.Assert.assertNotNull;
5761

5862
/**
5963
* Integration test for the BATCH-2034 issue.
@@ -63,6 +67,7 @@
6367
* from the spring-batch-integration project.
6468
*
6569
* @author Sergey Shcherbakov
70+
* @author Mahmoud Ben Hassine
6671
*/
6772
@ContextConfiguration(classes={SimpleJobExplorerIntegrationTests.Config.class})
6873
@RunWith(SpringJUnit4ClassRunner.class)
@@ -127,6 +132,13 @@ public DataSourceInitializer dataSourceInitializer() {
127132
});
128133
return dataSourceInitializer;
129134
}
135+
136+
@Bean
137+
public Job job(JobBuilderFactory jobBuilderFactory) {
138+
return jobBuilderFactory.get("job")
139+
.start(dummyStep())
140+
.build();
141+
}
130142
}
131143

132144
@Autowired
@@ -137,6 +149,12 @@ public DataSourceInitializer dataSourceInitializer() {
137149

138150
@Autowired
139151
private FlowStep flowStep;
152+
153+
@Autowired
154+
private JobLauncher jobLauncher;
155+
156+
@Autowired
157+
private Job job;
140158

141159
@Test
142160
public void testGetStepExecution() throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobInterruptedException, UnexpectedJobExecutionException {
@@ -153,4 +171,13 @@ public void testGetStepExecution() throws JobExecutionAlreadyRunningException, J
153171
assertEquals(BatchStatus.COMPLETED, jobExplorerStepExecution.getStatus());
154172
}
155173

174+
@Test
175+
public void getLastJobExecutionShouldFetchStepExecutions() throws Exception {
176+
this.jobLauncher.run(this.job, new JobParameters());
177+
JobInstance lastJobInstance = this.jobExplorer.getLastJobInstance("job");
178+
JobExecution lastJobExecution = this.jobExplorer.getLastJobExecution(lastJobInstance);
179+
assertEquals(1, lastJobExecution.getStepExecutions().size());
180+
StepExecution stepExecution = lastJobExecution.getStepExecutions().iterator().next();
181+
assertNotNull(stepExecution.getExecutionContext());
182+
}
156183
}

0 commit comments

Comments
 (0)