Add getJobInstance method in JobExplorer/JobRepository

Resolves #3930
This commit is contained in:
Parikshit Dutta
2021-06-07 20:41:40 +05:30
committed by Mahmoud Ben Hassine
parent fd7dd5f1a7
commit 5a35b03300
11 changed files with 108 additions and 5 deletions

View File

@@ -20,6 +20,7 @@ import java.util.Set;
import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance; import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.launch.NoSuchJobException; import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ExecutionContext;
@@ -34,6 +35,7 @@ import org.springframework.lang.Nullable;
* @author Michael Minella * @author Michael Minella
* @author Will Schipp * @author Will Schipp
* @author Mahmoud Ben Hassine * @author Mahmoud Ben Hassine
* @author Parikshit Dutta
* @since 2.0 * @since 2.0
*/ */
public interface JobExplorer { public interface JobExplorer {
@@ -92,6 +94,16 @@ public interface JobExplorer {
@Nullable @Nullable
JobInstance getJobInstance(@Nullable Long instanceId); JobInstance getJobInstance(@Nullable Long instanceId);
/**
* @param jobName {@link String} name for the jobInstance.
* @param jobParameters {@link JobParameters} parameters for the jobInstance.
* @return the {@link JobInstance} with this name and parameters, or null
*
* @since 5.0
*/
@Nullable
JobInstance getJobInstance(String jobName, JobParameters jobParameters);
/** /**
* Retrieve job executions by their job instance. The corresponding step executions * Retrieve job executions by their job instance. The corresponding step executions
* may not be fully hydrated (for example, their execution context may be missing), * may not be fully hydrated (for example, their execution context may be missing),

View File

@@ -18,6 +18,7 @@ package org.springframework.batch.core.explore.support;
import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance; import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.explore.JobExplorer; import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.NoSuchJobException; import org.springframework.batch.core.launch.NoSuchJobException;
@@ -38,6 +39,8 @@ import java.util.Set;
* @author Michael Minella * @author Michael Minella
* @author Will Schipp * @author Will Schipp
* @author Mahmoud Ben Hassine * @author Mahmoud Ben Hassine
* @author Parikshit Dutta
*
* @see JobExplorer * @see JobExplorer
* @see JobInstanceDao * @see JobInstanceDao
* @see JobExecutionDao * @see JobExecutionDao
@@ -185,6 +188,19 @@ public class SimpleJobExplorer implements JobExplorer {
return jobInstanceDao.getJobInstance(instanceId); return jobInstanceDao.getJobInstance(instanceId);
} }
/*
* (non-Javadoc)
*
* @see
* org.springframework.batch.core.explore.JobExplorer#getJobInstance(java
* .lang.String, org.springframework.batch.core.JobParameters)
*/
@Nullable
@Override
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
return jobInstanceDao.getJobInstance(jobName, jobParameters);
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* *

View File

@@ -46,6 +46,7 @@ import java.util.List;
* @author David Turanski * @author David Turanski
* @author Michael Minella * @author Michael Minella
* @author Mahmoud Ben Hassine * @author Mahmoud Ben Hassine
* @author Parikshit Dutta
*/ */
public interface JobRepository { public interface JobRepository {
@@ -186,6 +187,16 @@ public interface JobRepository {
*/ */
void updateExecutionContext(JobExecution jobExecution); void updateExecutionContext(JobExecution jobExecution);
/**
* @param jobName {@link String} the name of the jobInstance
* @param jobParameters {@link JobParameters} parameters identifying the {@link JobInstance}
* @return the {@link JobInstance} with name and parameters, or null
*
* @since 5.0
*/
@Nullable
JobInstance getJobInstance(String jobName, JobParameters jobParameters);
/** /**
* @param jobInstance {@link JobInstance} instance containing the step executions. * @param jobInstance {@link JobInstance} instance containing the step executions.
* @param stepName the name of the step execution that might have run. * @param stepName the name of the step execution that might have run.

View File

@@ -53,6 +53,8 @@ import java.util.List;
* @author David Turanski * @author David Turanski
* @author Mahmoud Ben Hassine * @author Mahmoud Ben Hassine
* @author Baris Cubukcuoglu * @author Baris Cubukcuoglu
* @author Parikshit Dutta
*
* @see JobRepository * @see JobRepository
* @see JobInstanceDao * @see JobInstanceDao
* @see JobExecutionDao * @see JobExecutionDao
@@ -244,6 +246,11 @@ public class SimpleJobRepository implements JobRepository {
ecDao.updateExecutionContext(jobExecution); ecDao.updateExecutionContext(jobExecution);
} }
@Override
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
return jobInstanceDao.getJobInstance(jobName, jobParameters);
}
@Override @Override
@Nullable @Nullable
public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) { public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) {

View File

@@ -32,6 +32,8 @@ import org.springframework.lang.Nullable;
* @author Dan Garrette * @author Dan Garrette
* @author David Turanski * @author David Turanski
* @author Mahmoud Ben Hassine * @author Mahmoud Ben Hassine
* @author Parikshit Dutta
*
* @since 2.0.1 * @since 2.0.1
*/ */
public class DummyJobRepository implements JobRepository, BeanNameAware { public class DummyJobRepository implements JobRepository, BeanNameAware {
@@ -57,6 +59,12 @@ public class DummyJobRepository implements JobRepository, BeanNameAware {
return null; return null;
} }
@Nullable
@Override
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
return null;
}
@Nullable @Nullable
@Override @Override
public JobExecution getLastJobExecution(String jobName, JobParameters jobParameters) { public JobExecution getLastJobExecution(String jobName, JobParameters jobParameters) {

View File

@@ -19,6 +19,10 @@ package org.springframework.batch.core.explore.support;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -44,7 +48,7 @@ import org.springframework.batch.core.repository.dao.StepExecutionDao;
* @author Will Schipp * @author Will Schipp
* @author Michael Minella * @author Michael Minella
* @author Mahmoud Ben Hassine * @author Mahmoud Ben Hassine
* * @author Parikshit Dutta
*/ */
class SimpleJobExplorerTests { class SimpleJobExplorerTests {
@@ -150,6 +154,14 @@ class SimpleJobExplorerTests {
jobExplorer.getJobInstance(111L); jobExplorer.getJobInstance(111L);
} }
@Test
public void testGetJobInstanceWithNameAndParameters() throws Exception {
when(jobInstanceDao.getJobInstance("job", new JobParameters())).thenReturn(jobInstance);
JobInstance jobInstance = jobExplorer.getJobInstance("job", new JobParameters());
verify(jobInstanceDao).getJobInstance(anyString(), any(JobParameters.class));
assertEquals(jobInstance, jobInstance);
}
@Test @Test
void testGetLastJobInstances() { void testGetLastJobInstances() {
jobInstanceDao.getJobInstances("foo", 0, 1); jobInstanceDao.getJobInstances("foo", 0, 1);

View File

@@ -55,7 +55,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* @author Lucas Ward * @author Lucas Ward
* @author Mahmoud Ben Hassine * @author Mahmoud Ben Hassine
* * @author Parikshit Dutta
*/ */
class CommandLineJobRunnerTests { class CommandLineJobRunnerTests {
@@ -515,6 +515,12 @@ class CommandLineJobRunnerTests {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Nullable
@Override
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
throw new UnsupportedOperationException();
}
@Nullable @Nullable
@Override @Override
public JobInstance getLastJobInstance(String jobName) { public JobInstance getLastJobInstance(String jobName) {

View File

@@ -21,6 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -60,6 +62,7 @@ import org.springframework.batch.core.step.StepSupport;
* @author Dimitrios Liapis * @author Dimitrios Liapis
* @author Baris Cubukcuoglu * @author Baris Cubukcuoglu
* @author Mahmoud Ben Hassine * @author Mahmoud Ben Hassine
* @author Parikshit Dutta
* *
*/ */
class SimpleJobRepositoryTests { class SimpleJobRepositoryTests {
@@ -329,4 +332,12 @@ class SimpleJobRepositoryTests {
assertEquals(BatchStatus.STOPPED, jobExecution.getStatus()); assertEquals(BatchStatus.STOPPED, jobExecution.getStatus());
} }
@Test
public void testGetJobInstanceWithNameAndParameters() throws Exception {
when(jobInstanceDao.getJobInstance("job", new JobParameters())).thenReturn(jobInstance);
JobInstance jobInstance = jobRepository.getJobInstance("job", new JobParameters());
verify(jobInstanceDao).getJobInstance(anyString(), any(JobParameters.class));
assertEquals(jobInstance, jobInstance);
}
} }

View File

@@ -28,7 +28,7 @@ import org.springframework.lang.Nullable;
* @author Dave Syer * @author Dave Syer
* @author David Turanski * @author David Turanski
* @author Mahmoud Ben Hassine * @author Mahmoud Ben Hassine
* * @author Parikshit Dutta
*/ */
public class JobRepositorySupport implements JobRepository { public class JobRepositorySupport implements JobRepository {
@@ -66,6 +66,12 @@ public class JobRepositorySupport implements JobRepository {
public void update(JobInstance job) { public void update(JobInstance job) {
} }
@Nullable
@Override
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
return null;
}
@Nullable @Nullable
@Override @Override
public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) { public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) {

View File

@@ -62,7 +62,7 @@ import static org.springframework.batch.core.BatchStatus.UNKNOWN;
* @author Dave Syer * @author Dave Syer
* @author David Turanski * @author David Turanski
* @author Mahmoud Ben Hassine * @author Mahmoud Ben Hassine
* * @author Parikshit Dutta
*/ */
class TaskletStepExceptionTests { class TaskletStepExceptionTests {
@@ -520,6 +520,12 @@ class TaskletStepExceptionTests {
return null; return null;
} }
@Nullable
@Override
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
return null;
}
@Nullable @Nullable
@Override @Override
public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) { public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) {

View File

@@ -30,7 +30,7 @@ import org.springframework.lang.Nullable;
/** /**
* @author Dave Syer * @author Dave Syer
* @author Mahmoud Ben Hassine * @author Mahmoud Ben Hassine
* * @author Parikshit Dutta
*/ */
public class JobRepositorySupport implements JobRepository { public class JobRepositorySupport implements JobRepository {
@@ -48,6 +48,14 @@ public class JobRepositorySupport implements JobRepository {
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.batch.core.repository.JobRepository#getJobInstance(java.lang.String,
* org.springframework.batch.core.JobParameters)
*/
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
return null;
}
/* (non-Javadoc)
* *
* @see * @see
* org.springframework.batch.core.repository.JobRepository#getLastStepExecution(org. * org.springframework.batch.core.repository.JobRepository#getLastStepExecution(org.