Skip to content

Fix step execution retrieval in SimpleJobExplorer#getLastJobExecution #3944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2019 the original author or authors.
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -99,7 +99,14 @@ public List<JobExecution> getJobExecutions(JobInstance jobInstance) {
*/
@Nullable
public JobExecution getLastJobExecution(JobInstance jobInstance) {
return jobExecutionDao.getLastJobExecution(jobInstance);
JobExecution lastJobExecution = jobExecutionDao.getLastJobExecution(jobInstance);
if (lastJobExecution != null) {
getJobExecutionDependencies(lastJobExecution);
for (StepExecution stepExecution : lastJobExecution.getStepExecutions()) {
getStepExecutionDependencies(stepExecution);
}
}
return lastJobExecution;
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import test.jdbc.datasource.DataSourceInitializer;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobInterruptedException;
Expand All @@ -32,6 +33,7 @@
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.UnexpectedJobExecutionException;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.xml.DummyStep;
import org.springframework.batch.core.explore.JobExplorer;
Expand All @@ -41,6 +43,7 @@
import org.springframework.batch.core.job.flow.support.StateTransition;
import org.springframework.batch.core.job.flow.support.state.EndState;
import org.springframework.batch.core.job.flow.support.state.StepState;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
Expand All @@ -54,6 +57,7 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
* Integration test for the BATCH-2034 issue.
Expand All @@ -63,6 +67,7 @@
* from the spring-batch-integration project.
*
* @author Sergey Shcherbakov
* @author Mahmoud Ben Hassine
*/
@ContextConfiguration(classes={SimpleJobExplorerIntegrationTests.Config.class})
@RunWith(SpringJUnit4ClassRunner.class)
Expand Down Expand Up @@ -127,6 +132,13 @@ public DataSourceInitializer dataSourceInitializer() {
});
return dataSourceInitializer;
}

@Bean
public Job job(JobBuilderFactory jobBuilderFactory) {
return jobBuilderFactory.get("job")
.start(dummyStep())
.build();
}
}

@Autowired
Expand All @@ -137,6 +149,12 @@ public DataSourceInitializer dataSourceInitializer() {

@Autowired
private FlowStep flowStep;

@Autowired
private JobLauncher jobLauncher;

@Autowired
private Job job;

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

@Test
public void getLastJobExecutionShouldFetchStepExecutions() throws Exception {
this.jobLauncher.run(this.job, new JobParameters());
JobInstance lastJobInstance = this.jobExplorer.getLastJobInstance("job");
JobExecution lastJobExecution = this.jobExplorer.getLastJobExecution(lastJobInstance);
assertEquals(1, lastJobExecution.getStepExecutions().size());
StepExecution stepExecution = lastJobExecution.getStepExecutions().iterator().next();
assertNotNull(stepExecution.getExecutionContext());
}
}