Skip to content

Commit 03497b3

Browse files
committed
Add method in JobRepository to find job instances by job name
1 parent 04f2116 commit 03497b3

File tree

13 files changed

+105
-22
lines changed

13 files changed

+105
-22
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ JobExecution startNextInstance(Job job) throws JobRestartException, JobExecution
286286
* @throws NoSuchJobException is thrown if no {@link JobInstance}s for the jobName
287287
* exist.
288288
* @deprecated Since 6.0 in favor of
289-
* {@link org.springframework.batch.core.repository.JobRepository#getJobInstances(String, int, int)}.
289+
* {@link org.springframework.batch.core.repository.JobRepository#findJobInstances(String)}.
290290
* Scheduled for removal in 6.2 or later.
291291
*/
292292
@Deprecated(since = "6.0", forRemoval = true)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ default List<JobInstance> getJobInstances(String jobName, int start, int count)
8787
return Collections.emptyList();
8888
}
8989

90+
/**
91+
* Fetch all {@link JobInstance} values for a given job name.
92+
* @param jobName The name of the job.
93+
* @return the {@link JobInstance} values.
94+
* @since 6.0
95+
*/
96+
default List<JobInstance> findJobInstances(String jobName) {
97+
return Collections.emptyList();
98+
}
99+
90100
/**
91101
* @param jobInstanceId The ID for the {@link JobInstance} to obtain.
92102
* @return the {@code JobInstance} that has this ID, or {@code null} if not found.

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ public interface JobInstanceDao {
9292
*/
9393
List<JobInstance> getJobInstances(String jobName, int start, int count);
9494

95+
/**
96+
* Fetch all job instances for the given job name.
97+
* @param jobName the job name
98+
* @return the job instances for the given name empty if none
99+
* @since 6.0
100+
*/
101+
default List<JobInstance> getJobInstances(String jobName) {
102+
return getJobInstanceIds(jobName).stream().map(jobInstanceId -> getJobInstance(jobInstanceId)).toList();
103+
}
104+
95105
/**
96106
* Fetch the last job instance by Id for the given job.
97107
* @param jobName name of the job
@@ -131,7 +141,7 @@ default List<Long> getJobInstanceIds(String jobName) {
131141
* @param count int containing the number of job instances to return.
132142
* @return a list of {@link JobInstance} for the job name requested.
133143
* @deprecated Since v6.0 and scheduled for removal in v6.2. Use
134-
* {@link #getJobInstances(String, int, int)}
144+
* {@link #getJobInstances(String)}
135145
*/
136146
@Deprecated(forRemoval = true)
137147
List<JobInstance> findJobInstancesByName(String jobName, int start, int count);

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/jdbc/JdbcJobInstanceDao.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ SELECT COUNT(*)
114114
ORDER BY JOB_INSTANCE_ID DESC
115115
""";
116116

117+
private static final String FIND_JOB_INSTANCES_BY_JOB_NAME = """
118+
SELECT JOB_INSTANCE_ID, JOB_NAME
119+
FROM %PREFIX%JOB_INSTANCE
120+
WHERE JOB_NAME LIKE ?
121+
""";
122+
117123
private static final String FIND_LAST_JOB_INSTANCE_BY_JOB_NAME = """
118124
SELECT JOB_INSTANCE_ID, JOB_NAME
119125
FROM %PREFIX%JOB_INSTANCE I1
@@ -236,6 +242,17 @@ public List<JobInstance> extractData(ResultSet rs) throws SQLException, DataAcce
236242
return getJdbcTemplate().query(getQuery(FIND_LAST_JOBS_BY_NAME), extractor, jobName);
237243
}
238244

245+
/**
246+
* Fetch all job instances for the given job name.
247+
* @param jobName the job name
248+
* @return the job instances for the given name empty if none
249+
* @since 6.0
250+
*/
251+
@Override
252+
public List<JobInstance> getJobInstances(String jobName) {
253+
return getJdbcTemplate().query(getQuery(FIND_JOB_INSTANCES_BY_JOB_NAME), new JobInstanceRowMapper(), jobName);
254+
}
255+
239256
@Override
240257
public List<Long> getJobInstanceIds(String jobName) {
241258
return getJdbcTemplate().queryForList(getQuery(GET_JOB_INSTANCE_IDS_BY_JOB_NAME), Long.class, jobName);

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/mongodb/MongoJobInstanceDao.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,32 @@ public List<JobInstance> getJobInstances(String jobName, int start, int count) {
124124
.toList();
125125
}
126126

127+
/**
128+
* Fetch all job instances for the given job name.
129+
* @param jobName the job name
130+
* @return the job instances for the given name empty if none
131+
* @since 6.0
132+
*/
133+
@Override
134+
public List<JobInstance> getJobInstances(String jobName) {
135+
Query query = query(where("jobName").is(jobName));
136+
return this.mongoOperations
137+
.find(query, org.springframework.batch.core.repository.persistence.JobInstance.class, COLLECTION_NAME)
138+
.stream()
139+
.map(this.jobInstanceConverter::toJobInstance)
140+
.toList();
141+
}
142+
143+
@Override
144+
public List<Long> getJobInstanceIds(String jobName) {
145+
Query query = query(where("jobName").is(jobName));
146+
return this.mongoOperations
147+
.find(query, org.springframework.batch.core.repository.persistence.JobInstance.class, COLLECTION_NAME)
148+
.stream()
149+
.map(org.springframework.batch.core.repository.persistence.JobInstance::getJobInstanceId)
150+
.toList();
151+
}
152+
127153
public List<JobInstance> findJobInstancesByName(String jobName) {
128154
Query query = query(where("jobName").is(jobName));
129155
return this.mongoOperations

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ public List<JobInstance> getJobInstances(String jobName, int start, int count) {
8181
return Collections.singletonList(this.jobInstance);
8282
}
8383

84+
/**
85+
* Find all {@link JobInstance}s for a given job name. In this implementation, only
86+
* one job instance is held, so if it is initialized, it is returned in a single-item
87+
* list.
88+
* @param jobName The name of the job to query.
89+
* @return a list of {@link JobInstance}s for the given job name.
90+
*/
91+
@Override
92+
public List<JobInstance> findJobInstances(String jobName) {
93+
if (this.jobInstance == null) {
94+
return Collections.emptyList();
95+
}
96+
return Collections.singletonList(this.jobInstance);
97+
}
98+
8499
@Override
85100
@Nullable
86101
public JobInstance getJobInstance(long instanceId) {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ public SimpleJobRepository(JobInstanceDao jobInstanceDao, JobExecutionDao jobExe
6969
super(jobInstanceDao, jobExecutionDao, stepExecutionDao, ecDao);
7070
}
7171

72+
/**
73+
* Fetch all {@link JobInstance} values for a given job name.
74+
* @param jobName The name of the job.
75+
* @return the {@link JobInstance} values.
76+
* @since 6.0
77+
*/
78+
@Override
79+
public List<JobInstance> findJobInstances(String jobName) {
80+
return this.jobInstanceDao.getJobInstances(jobName);
81+
}
82+
7283
/**
7384
* Create a new {@link JobExecution} for the given {@link JobInstance} and
7485
* {@link JobParameters}, and associate the provided {@link ExecutionContext} with the

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ void testDeleteJobInstance() throws Exception {
275275

276276
jobRepository.deleteJobInstance(jobExecution.getJobInstance());
277277

278-
assertEquals(0, jobRepository.getJobInstances(job.getName(), 0, 1).size());
278+
assertEquals(0, jobRepository.findJobInstances(job.getName()).size());
279279
assertNull(jobRepository.getLastJobExecution(job.getName(), jobParameters));
280280
}
281281

spring-batch-integration/src/test/java/org/springframework/batch/integration/partition/JmsIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ void testSimpleProperties() {
6060

6161
@Test
6262
void testLaunchJob() throws Exception {
63-
int before = jobRepository.getJobInstances(job.getName(), 0, 100).size();
63+
int before = jobRepository.findJobInstances(job.getName()).size();
6464
assertNotNull(jobOperator.start(job, new JobParameters()));
65-
List<JobInstance> jobInstances = jobRepository.getJobInstances(job.getName(), 0, 100);
65+
List<JobInstance> jobInstances = jobRepository.findJobInstances(job.getName());
6666
int after = jobInstances.size();
6767
assertEquals(1, after - before);
6868
JobExecution jobExecution = jobRepository.getJobExecutions(jobInstances.get(jobInstances.size() - 1)).get(0);

spring-batch-integration/src/test/java/org/springframework/batch/integration/partition/PollingIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ void testSimpleProperties() {
5555

5656
@Test
5757
void testLaunchJob() throws Exception {
58-
int before = jobRepository.getJobInstances(job.getName(), 0, 100).size();
58+
int before = jobRepository.findJobInstances(job.getName()).size();
5959
assertNotNull(jobOperator.start(job, new JobParameters()));
60-
List<JobInstance> jobInstances = jobRepository.getJobInstances(job.getName(), 0, 100);
60+
List<JobInstance> jobInstances = jobRepository.findJobInstances(job.getName());
6161
int after = jobInstances.size();
6262
assertEquals(1, after - before);
6363
JobExecution jobExecution = jobRepository.getJobExecutions(jobInstances.get(jobInstances.size() - 1)).get(0);

0 commit comments

Comments
 (0)