Skip to content

Commit 171a3c8

Browse files
committed
Add method getJobInstance in JobOperator
Issue #3930
1 parent 596ee70 commit 171a3c8

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/launch/JobOperator.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2007 the original author or authors.
2+
* Copyright 2006-2022 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.
@@ -30,13 +30,15 @@
3030
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
3131
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
3232
import org.springframework.batch.core.repository.JobRestartException;
33+
import org.springframework.lang.Nullable;
3334

3435
/**
3536
* Low level interface for inspecting and controlling jobs with access only to primitive
3637
* and collection types. Suitable for a command-line client (e.g. that launches a new
3738
* process for each operation), or a remote launcher like a JMX console.
3839
*
3940
* @author Dave Syer
41+
* @author Mahmoud Ben Hassine
4042
* @since 2.0
4143
*/
4244
public interface JobOperator {
@@ -65,6 +67,19 @@ public interface JobOperator {
6567
*/
6668
List<Long> getJobInstances(String jobName, int start, int count) throws NoSuchJobException;
6769

70+
/**
71+
* @param jobName {@link String} name of the job.
72+
* @param jobParameters {@link JobParameters} parameters for the job instance.
73+
* @return the {@link JobInstance} with the given name and parameters, or
74+
* {@code null}.
75+
*
76+
* @since 5.0
77+
*/
78+
@Nullable
79+
default JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
80+
throw new UnsupportedOperationException();
81+
}
82+
6883
/**
6984
* Get the id values of all the running {@link JobExecution JobExecutions} with the
7085
* given job name.

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.springframework.batch.core.step.tasklet.Tasklet;
6262
import org.springframework.batch.core.step.tasklet.TaskletStep;
6363
import org.springframework.beans.factory.InitializingBean;
64+
import org.springframework.lang.Nullable;
6465
import org.springframework.transaction.annotation.Transactional;
6566
import org.springframework.util.Assert;
6667

@@ -198,6 +199,17 @@ public List<Long> getJobInstances(String jobName, int start, int count) throws N
198199
return list;
199200
}
200201

202+
/*
203+
* (non-Javadoc)
204+
*
205+
* @see org.springframework.batch.core.launch.JobOperator#getJobInstance(String,
206+
* JobParameters)
207+
*/
208+
@Nullable
209+
public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
210+
return this.jobExplorer.getJobInstance(jobName, jobParameters);
211+
}
212+
201213
/*
202214
* (non-Javadoc)
203215
*

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,22 @@ void testGetLastInstancesNoSuchJob() {
272272
assertThrows(NoSuchJobException.class, () -> jobOperator.getJobInstances("no-such-job", 0, 2));
273273
}
274274

275+
@Test
276+
public void testGetJobInstanceWithNameAndParameters() {
277+
// given
278+
String jobName = "job";
279+
JobParameters jobParameters = new JobParameters();
280+
JobInstance jobInstance = mock(JobInstance.class);
281+
282+
// when
283+
when(this.jobExplorer.getJobInstance(jobName, jobParameters)).thenReturn(jobInstance);
284+
JobInstance actualJobInstance = this.jobOperator.getJobInstance(jobName, jobParameters);
285+
286+
// then
287+
verify(this.jobExplorer).getJobInstance(jobName, jobParameters);
288+
assertEquals(jobInstance, actualJobInstance);
289+
}
290+
275291
@Test
276292
void testGetJobNames() {
277293
Set<String> names = jobOperator.getJobNames();

0 commit comments

Comments
 (0)