diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/JobExplorer.java b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/JobExplorer.java index 4077a4ccb..bdfa4e14a 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/JobExplorer.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/JobExplorer.java @@ -20,6 +20,7 @@ import java.util.Set; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobInstance; +import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.launch.NoSuchJobException; import org.springframework.batch.item.ExecutionContext; @@ -34,6 +35,7 @@ import org.springframework.lang.Nullable; * @author Michael Minella * @author Will Schipp * @author Mahmoud Ben Hassine + * @author Parikshit Dutta * @since 2.0 */ public interface JobExplorer { @@ -92,6 +94,16 @@ public interface JobExplorer { @Nullable 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 * may not be fully hydrated (for example, their execution context may be missing), diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java index 7893acf42..0a78e13a0 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java @@ -18,6 +18,7 @@ package org.springframework.batch.core.explore.support; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobInstance; +import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.explore.JobExplorer; import org.springframework.batch.core.launch.NoSuchJobException; @@ -38,6 +39,8 @@ import java.util.Set; * @author Michael Minella * @author Will Schipp * @author Mahmoud Ben Hassine + * @author Parikshit Dutta + * * @see JobExplorer * @see JobInstanceDao * @see JobExecutionDao @@ -185,6 +188,19 @@ public class SimpleJobExplorer implements JobExplorer { 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) * diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRepository.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRepository.java index 14d8695b5..d83daa8af 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRepository.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRepository.java @@ -46,6 +46,7 @@ import java.util.List; * @author David Turanski * @author Michael Minella * @author Mahmoud Ben Hassine + * @author Parikshit Dutta */ public interface JobRepository { @@ -186,6 +187,16 @@ public interface JobRepository { */ 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 stepName the name of the step execution that might have run. diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java index 658ef29dc..d04bed856 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java @@ -53,6 +53,8 @@ import java.util.List; * @author David Turanski * @author Mahmoud Ben Hassine * @author Baris Cubukcuoglu + * @author Parikshit Dutta + * * @see JobRepository * @see JobInstanceDao * @see JobExecutionDao @@ -244,6 +246,11 @@ public class SimpleJobRepository implements JobRepository { ecDao.updateExecutionContext(jobExecution); } + @Override + public JobInstance getJobInstance(String jobName, JobParameters jobParameters) { + return jobInstanceDao.getJobInstance(jobName, jobParameters); + } + @Override @Nullable public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/DummyJobRepository.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/DummyJobRepository.java index 290fd59f6..bfca29bfc 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/DummyJobRepository.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/DummyJobRepository.java @@ -32,6 +32,8 @@ import org.springframework.lang.Nullable; * @author Dan Garrette * @author David Turanski * @author Mahmoud Ben Hassine + * @author Parikshit Dutta + * * @since 2.0.1 */ public class DummyJobRepository implements JobRepository, BeanNameAware { @@ -57,6 +59,12 @@ public class DummyJobRepository implements JobRepository, BeanNameAware { return null; } + @Nullable + @Override + public JobInstance getJobInstance(String jobName, JobParameters jobParameters) { + return null; + } + @Nullable @Override public JobExecution getLastJobExecution(String jobName, JobParameters jobParameters) { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/explore/support/SimpleJobExplorerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/explore/support/SimpleJobExplorerTests.java index 9844cca56..6cd70bfcc 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/explore/support/SimpleJobExplorerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/explore/support/SimpleJobExplorerTests.java @@ -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.assertNull; 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.verify; import static org.mockito.Mockito.when; @@ -44,7 +48,7 @@ import org.springframework.batch.core.repository.dao.StepExecutionDao; * @author Will Schipp * @author Michael Minella * @author Mahmoud Ben Hassine - * + * @author Parikshit Dutta */ class SimpleJobExplorerTests { @@ -150,6 +154,14 @@ class SimpleJobExplorerTests { 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 void testGetLastJobInstances() { jobInstanceDao.getJobInstances("foo", 0, 1); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java index 0ebb60aaa..6afb5cae0 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java @@ -55,7 +55,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Lucas Ward * @author Mahmoud Ben Hassine - * + * @author Parikshit Dutta */ class CommandLineJobRunnerTests { @@ -515,6 +515,12 @@ class CommandLineJobRunnerTests { throw new UnsupportedOperationException(); } + @Nullable + @Override + public JobInstance getJobInstance(String jobName, JobParameters jobParameters) { + throw new UnsupportedOperationException(); + } + @Nullable @Override public JobInstance getLastJobInstance(String jobName) { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java index 260903d30..2d277981c 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java @@ -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.assertFalse; 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.verify; import static org.mockito.Mockito.when; @@ -60,6 +62,7 @@ import org.springframework.batch.core.step.StepSupport; * @author Dimitrios Liapis * @author Baris Cubukcuoglu * @author Mahmoud Ben Hassine + * @author Parikshit Dutta * */ class SimpleJobRepositoryTests { @@ -329,4 +332,12 @@ class SimpleJobRepositoryTests { 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); + } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/JobRepositorySupport.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/JobRepositorySupport.java index 6209ebe46..67bf9f1fc 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/JobRepositorySupport.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/JobRepositorySupport.java @@ -28,7 +28,7 @@ import org.springframework.lang.Nullable; * @author Dave Syer * @author David Turanski * @author Mahmoud Ben Hassine - * + * @author Parikshit Dutta */ public class JobRepositorySupport implements JobRepository { @@ -66,6 +66,12 @@ public class JobRepositorySupport implements JobRepository { public void update(JobInstance job) { } + @Nullable + @Override + public JobInstance getJobInstance(String jobName, JobParameters jobParameters) { + return null; + } + @Nullable @Override public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/TaskletStepExceptionTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/TaskletStepExceptionTests.java index 455254800..5bc1fc695 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/TaskletStepExceptionTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/TaskletStepExceptionTests.java @@ -62,7 +62,7 @@ import static org.springframework.batch.core.BatchStatus.UNKNOWN; * @author Dave Syer * @author David Turanski * @author Mahmoud Ben Hassine - * + * @author Parikshit Dutta */ class TaskletStepExceptionTests { @@ -520,6 +520,12 @@ class TaskletStepExceptionTests { return null; } + @Nullable + @Override + public JobInstance getJobInstance(String jobName, JobParameters jobParameters) { + return null; + } + @Nullable @Override public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) { diff --git a/spring-batch-integration/src/test/java/org/springframework/batch/integration/JobRepositorySupport.java b/spring-batch-integration/src/test/java/org/springframework/batch/integration/JobRepositorySupport.java index 2e23bb5a0..9617f64f3 100644 --- a/spring-batch-integration/src/test/java/org/springframework/batch/integration/JobRepositorySupport.java +++ b/spring-batch-integration/src/test/java/org/springframework/batch/integration/JobRepositorySupport.java @@ -30,7 +30,7 @@ import org.springframework.lang.Nullable; /** * @author Dave Syer * @author Mahmoud Ben Hassine - * + * @author Parikshit Dutta */ public class JobRepositorySupport implements JobRepository { @@ -48,6 +48,14 @@ public class JobRepositorySupport implements JobRepository { /* * (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 * org.springframework.batch.core.repository.JobRepository#getLastStepExecution(org.