Skip to content

Commit 5a35b03

Browse files
parikshitduttafmbenhassine
authored andcommitted
Add getJobInstance method in JobExplorer/JobRepository
Resolves #3930
1 parent fd7dd5f commit 5a35b03

File tree

11 files changed

+108
-5
lines changed

11 files changed

+108
-5
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.batch.core.JobExecution;
2222
import org.springframework.batch.core.JobInstance;
23+
import org.springframework.batch.core.JobParameters;
2324
import org.springframework.batch.core.StepExecution;
2425
import org.springframework.batch.core.launch.NoSuchJobException;
2526
import org.springframework.batch.item.ExecutionContext;
@@ -34,6 +35,7 @@
3435
* @author Michael Minella
3536
* @author Will Schipp
3637
* @author Mahmoud Ben Hassine
38+
* @author Parikshit Dutta
3739
* @since 2.0
3840
*/
3941
public interface JobExplorer {
@@ -92,6 +94,16 @@ default JobInstance getLastJobInstance(String jobName) {
9294
@Nullable
9395
JobInstance getJobInstance(@Nullable Long instanceId);
9496

97+
/**
98+
* @param jobName {@link String} name for the jobInstance.
99+
* @param jobParameters {@link JobParameters} parameters for the jobInstance.
100+
* @return the {@link JobInstance} with this name and parameters, or null
101+
*
102+
* @since 5.0
103+
*/
104+
@Nullable
105+
JobInstance getJobInstance(String jobName, JobParameters jobParameters);
106+
95107
/**
96108
* Retrieve job executions by their job instance. The corresponding step executions
97109
* may not be fully hydrated (for example, their execution context may be missing),

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.batch.core.JobExecution;
2020
import org.springframework.batch.core.JobInstance;
21+
import org.springframework.batch.core.JobParameters;
2122
import org.springframework.batch.core.StepExecution;
2223
import org.springframework.batch.core.explore.JobExplorer;
2324
import org.springframework.batch.core.launch.NoSuchJobException;
@@ -38,6 +39,8 @@
3839
* @author Michael Minella
3940
* @author Will Schipp
4041
* @author Mahmoud Ben Hassine
42+
* @author Parikshit Dutta
43+
*
4144
* @see JobExplorer
4245
* @see JobInstanceDao
4346
* @see JobExecutionDao
@@ -185,6 +188,19 @@ public JobInstance getJobInstance(@Nullable Long instanceId) {
185188
return jobInstanceDao.getJobInstance(instanceId);
186189
}
187190

191+
/*
192+
* (non-Javadoc)
193+
*
194+
* @see
195+
* org.springframework.batch.core.explore.JobExplorer#getJobInstance(java
196+
* .lang.String, org.springframework.batch.core.JobParameters)
197+
*/
198+
@Nullable
199+
@Override
200+
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
201+
return jobInstanceDao.getJobInstance(jobName, jobParameters);
202+
}
203+
188204
/*
189205
* (non-Javadoc)
190206
*

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @author David Turanski
4747
* @author Michael Minella
4848
* @author Mahmoud Ben Hassine
49+
* @author Parikshit Dutta
4950
*/
5051
public interface JobRepository {
5152

@@ -186,6 +187,16 @@ JobExecution createJobExecution(String jobName, JobParameters jobParameters)
186187
*/
187188
void updateExecutionContext(JobExecution jobExecution);
188189

190+
/**
191+
* @param jobName {@link String} the name of the jobInstance
192+
* @param jobParameters {@link JobParameters} parameters identifying the {@link JobInstance}
193+
* @return the {@link JobInstance} with name and parameters, or null
194+
*
195+
* @since 5.0
196+
*/
197+
@Nullable
198+
JobInstance getJobInstance(String jobName, JobParameters jobParameters);
199+
189200
/**
190201
* @param jobInstance {@link JobInstance} instance containing the step executions.
191202
* @param stepName the name of the step execution that might have run.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
* @author David Turanski
5454
* @author Mahmoud Ben Hassine
5555
* @author Baris Cubukcuoglu
56+
* @author Parikshit Dutta
57+
*
5658
* @see JobRepository
5759
* @see JobInstanceDao
5860
* @see JobExecutionDao
@@ -244,6 +246,11 @@ public void updateExecutionContext(JobExecution jobExecution) {
244246
ecDao.updateExecutionContext(jobExecution);
245247
}
246248

249+
@Override
250+
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
251+
return jobInstanceDao.getJobInstance(jobName, jobParameters);
252+
}
253+
247254
@Override
248255
@Nullable
249256
public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) {

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/DummyJobRepository.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
* @author Dan Garrette
3333
* @author David Turanski
3434
* @author Mahmoud Ben Hassine
35+
* @author Parikshit Dutta
36+
*
3537
* @since 2.0.1
3638
*/
3739
public class DummyJobRepository implements JobRepository, BeanNameAware {
@@ -57,6 +59,12 @@ public JobExecution createJobExecution(String jobName, JobParameters jobParamete
5759
return null;
5860
}
5961

62+
@Nullable
63+
@Override
64+
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
65+
return null;
66+
}
67+
6068
@Nullable
6169
@Override
6270
public JobExecution getLastJobExecution(String jobName, JobParameters jobParameters) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertNull;
2121
import static org.junit.jupiter.api.Assertions.assertThrows;
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertNull;
24+
import static org.mockito.ArgumentMatchers.any;
25+
import static org.mockito.ArgumentMatchers.anyString;
2226
import static org.mockito.Mockito.mock;
2327
import static org.mockito.Mockito.verify;
2428
import static org.mockito.Mockito.when;
@@ -44,7 +48,7 @@
4448
* @author Will Schipp
4549
* @author Michael Minella
4650
* @author Mahmoud Ben Hassine
47-
*
51+
* @author Parikshit Dutta
4852
*/
4953
class SimpleJobExplorerTests {
5054

@@ -150,6 +154,14 @@ void testGetJobInstance() {
150154
jobExplorer.getJobInstance(111L);
151155
}
152156

157+
@Test
158+
public void testGetJobInstanceWithNameAndParameters() throws Exception {
159+
when(jobInstanceDao.getJobInstance("job", new JobParameters())).thenReturn(jobInstance);
160+
JobInstance jobInstance = jobExplorer.getJobInstance("job", new JobParameters());
161+
verify(jobInstanceDao).getJobInstance(anyString(), any(JobParameters.class));
162+
assertEquals(jobInstance, jobInstance);
163+
}
164+
153165
@Test
154166
void testGetLastJobInstances() {
155167
jobInstanceDao.getJobInstances("foo", 0, 1);

spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/CommandLineJobRunnerTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
/**
5656
* @author Lucas Ward
5757
* @author Mahmoud Ben Hassine
58-
*
58+
* @author Parikshit Dutta
5959
*/
6060
class CommandLineJobRunnerTests {
6161

@@ -515,6 +515,12 @@ public JobInstance getJobInstance(@Nullable Long instanceId) {
515515
throw new UnsupportedOperationException();
516516
}
517517

518+
@Nullable
519+
@Override
520+
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
521+
throw new UnsupportedOperationException();
522+
}
523+
518524
@Nullable
519525
@Override
520526
public JobInstance getLastJobInstance(String jobName) {

spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import static org.junit.jupiter.api.Assertions.assertTrue;
2222
import static org.junit.jupiter.api.Assertions.assertFalse;
2323
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.mockito.ArgumentMatchers.any;
25+
import static org.mockito.ArgumentMatchers.anyString;
2426
import static org.mockito.Mockito.mock;
2527
import static org.mockito.Mockito.verify;
2628
import static org.mockito.Mockito.when;
@@ -60,6 +62,7 @@
6062
* @author Dimitrios Liapis
6163
* @author Baris Cubukcuoglu
6264
* @author Mahmoud Ben Hassine
65+
* @author Parikshit Dutta
6366
*
6467
*/
6568
class SimpleJobRepositoryTests {
@@ -329,4 +332,12 @@ public void testUpgradeStopping() {
329332
assertEquals(BatchStatus.STOPPED, jobExecution.getStatus());
330333
}
331334

335+
336+
@Test
337+
public void testGetJobInstanceWithNameAndParameters() throws Exception {
338+
when(jobInstanceDao.getJobInstance("job", new JobParameters())).thenReturn(jobInstance);
339+
JobInstance jobInstance = jobRepository.getJobInstance("job", new JobParameters());
340+
verify(jobInstanceDao).getJobInstance(anyString(), any(JobParameters.class));
341+
assertEquals(jobInstance, jobInstance);
342+
}
332343
}

spring-batch-core/src/test/java/org/springframework/batch/core/step/JobRepositorySupport.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @author Dave Syer
2929
* @author David Turanski
3030
* @author Mahmoud Ben Hassine
31-
*
31+
* @author Parikshit Dutta
3232
*/
3333
public class JobRepositorySupport implements JobRepository {
3434

@@ -66,6 +66,12 @@ public void update(JobExecution jobExecution) {
6666
public void update(JobInstance job) {
6767
}
6868

69+
@Nullable
70+
@Override
71+
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
72+
return null;
73+
}
74+
6975
@Nullable
7076
@Override
7177
public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) {

spring-batch-core/src/test/java/org/springframework/batch/core/step/item/TaskletStepExceptionTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
* @author Dave Syer
6363
* @author David Turanski
6464
* @author Mahmoud Ben Hassine
65-
*
65+
* @author Parikshit Dutta
6666
*/
6767
class TaskletStepExceptionTests {
6868

@@ -520,6 +520,12 @@ public JobExecution createJobExecution(String jobName, JobParameters jobParamete
520520
return null;
521521
}
522522

523+
@Nullable
524+
@Override
525+
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
526+
return null;
527+
}
528+
523529
@Nullable
524530
@Override
525531
public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) {

spring-batch-integration/src/test/java/org/springframework/batch/integration/JobRepositorySupport.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
/**
3131
* @author Dave Syer
3232
* @author Mahmoud Ben Hassine
33-
*
33+
* @author Parikshit Dutta
3434
*/
3535
public class JobRepositorySupport implements JobRepository {
3636

@@ -48,6 +48,14 @@ public JobExecution createJobExecution(String jobName, JobParameters jobParamete
4848

4949
/*
5050
* (non-Javadoc)
51+
* @see org.springframework.batch.core.repository.JobRepository#getJobInstance(java.lang.String,
52+
* org.springframework.batch.core.JobParameters)
53+
*/
54+
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
55+
return null;
56+
}
57+
58+
/* (non-Javadoc)
5159
*
5260
* @see
5361
* org.springframework.batch.core.repository.JobRepository#getLastStepExecution(org.

0 commit comments

Comments
 (0)