Skip to content

Commit c425137

Browse files
committed
Remove datasource dependency in JobRepositoryTestUtils
Before this commit, the `JobRepositoryTestUtils` was tied to the JDBC implementation of the `JobRepository` as it was requiring a datasource. This makes it unusable with implementations that do not rely on a datasource to store batch meta-data (A MongoDB job repository for instance where no datasource is used). This commit decouples the `JobRepositoryTestUtils` from the implementation details of the `JobRepository` by making it working against the `JobRepository` interface. This commit also introduces the necessary methods in the `JobRepository` interface as well as various DAOs to implement the utilities without having to deal with the details of the underlying repository implementation. Resolves #4070
1 parent f104baa commit c425137

File tree

19 files changed

+354
-96
lines changed

19 files changed

+354
-96
lines changed

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 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.
@@ -29,6 +29,8 @@
2929
import org.springframework.transaction.annotation.Isolation;
3030

3131
import java.util.Collection;
32+
import java.util.Collections;
33+
import java.util.List;
3234

3335
/**
3436
* <p>
@@ -47,6 +49,41 @@
4749
*/
4850
public interface JobRepository {
4951

52+
/**
53+
* Retrieve the names of all job instances sorted alphabetically - i.e. jobs that have
54+
* ever been executed.
55+
* @return the names of all job instances
56+
* @since 5.0
57+
*/
58+
default List<String> getJobNames() {
59+
return Collections.emptyList();
60+
}
61+
62+
/**
63+
* Fetch the last job instances with the provided name, sorted backwards by primary
64+
* key, using a 'like' criteria
65+
* @param jobName {@link String} containing the name of the job.
66+
* @param start int containing the offset of where list of job instances results
67+
* should begin.
68+
* @param count int containing the number of job instances to return.
69+
* @return a list of {@link JobInstance} for the job name requested.
70+
* @since 5.0
71+
*/
72+
default List<JobInstance> findJobInstancesByName(String jobName, int start, int count) {
73+
return Collections.emptyList();
74+
}
75+
76+
/**
77+
* Return all {@link JobExecution}s for given {@link JobInstance}, sorted backwards by
78+
* creation order (so the first element is the most recent).
79+
* @param jobInstance parent {@link JobInstance} of the {@link JobExecution}s to find.
80+
* @return {@link List} containing JobExecutions for the jobInstance.
81+
* @since 5.0
82+
*/
83+
default List<JobExecution> findJobExecutions(JobInstance jobInstance) {
84+
return Collections.emptyList();
85+
}
86+
5087
/**
5188
* Check if an instance of this job already exists with the parameters provided.
5289
* @param jobName the name of the job
@@ -172,4 +209,33 @@ JobExecution createJobExecution(String jobName, JobParameters jobParameters)
172209
@Nullable
173210
JobExecution getLastJobExecution(String jobName, JobParameters jobParameters);
174211

212+
/**
213+
* Delete the step execution along with its execution context.
214+
* @param stepExecution the step execution to delete
215+
* @since 5.0
216+
*/
217+
default void deleteStepExecution(StepExecution stepExecution) {
218+
throw new UnsupportedOperationException();
219+
}
220+
221+
/**
222+
* Delete the job execution object graph (ie the job execution with its execution
223+
* context, all related step executions and their executions contexts, as well as
224+
* associated job parameters)
225+
* @param jobExecution the job execution to delete
226+
* @since 5.0
227+
*/
228+
default void deleteJobExecution(JobExecution jobExecution) {
229+
throw new UnsupportedOperationException();
230+
}
231+
232+
/**
233+
* Delete the job instance.
234+
* @param jobInstance the job instance to delete
235+
* @since 5.0
236+
*/
237+
default void deleteJobInstance(JobInstance jobInstance) {
238+
throw new UnsupportedOperationException();
239+
}
240+
175241
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2013 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.
@@ -27,6 +27,7 @@
2727
*
2828
* @author Robert Kasanicky
2929
* @author David Turanski
30+
* @author Mahmoud Ben Hassine
3031
*/
3132
public interface ExecutionContextDao {
3233

@@ -78,4 +79,22 @@ public interface ExecutionContextDao {
7879
*/
7980
void updateExecutionContext(final StepExecution stepExecution);
8081

82+
/**
83+
* Delete the execution context of the given {@link JobExecution}.
84+
* @param jobExecution {@link JobExecution} that contains the context to delete.
85+
* @since 5.0
86+
*/
87+
default void deleteExecutionContext(JobExecution jobExecution) {
88+
throw new UnsupportedOperationException();
89+
}
90+
91+
/**
92+
* Delete the execution context of the given {@link StepExecution}.
93+
* @param stepExecution {@link StepExecution} that contains the context to delete.
94+
* @since 5.0
95+
*/
96+
default void deleteExecutionContext(StepExecution stepExecution) {
97+
throw new UnsupportedOperationException();
98+
}
99+
81100
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implem
7777
private static final String UPDATE_STEP_EXECUTION_CONTEXT = "UPDATE %PREFIX%STEP_EXECUTION_CONTEXT "
7878
+ "SET SHORT_CONTEXT = ?, SERIALIZED_CONTEXT = ? " + "WHERE STEP_EXECUTION_ID = ?";
7979

80+
private static final String DELETE_STEP_EXECUTION_CONTEXT = "DELETE FROM %PREFIX%STEP_EXECUTION_CONTEXT "
81+
+ "WHERE STEP_EXECUTION_ID = ?";
82+
83+
private static final String DELETE_JOB_EXECUTION_CONTEXT = "DELETE FROM %PREFIX%JOB_EXECUTION_CONTEXT "
84+
+ "WHERE JOB_EXECUTION_ID = ?";
85+
8086
private Charset charset = StandardCharsets.UTF_8;
8187

8288
private static final int DEFAULT_MAX_VARCHAR_LENGTH = 2500;
@@ -217,6 +223,22 @@ public void saveExecutionContexts(Collection<StepExecution> stepExecutions) {
217223
persistSerializedContexts(serializedContexts, INSERT_STEP_EXECUTION_CONTEXT);
218224
}
219225

226+
/**
227+
* Delete the execution context of the given {@link JobExecution}.
228+
* @param jobExecution {@link JobExecution} that contains the context to delete.
229+
*/
230+
public void deleteExecutionContext(JobExecution jobExecution) {
231+
getJdbcTemplate().update(getQuery(DELETE_JOB_EXECUTION_CONTEXT), jobExecution.getId());
232+
}
233+
234+
/**
235+
* Delete the execution context of the given {@link StepExecution}.
236+
* @param stepExecution {@link StepExecution} that contains the context to delete.
237+
*/
238+
public void deleteExecutionContext(StepExecution stepExecution) {
239+
getJdbcTemplate().update(getQuery(DELETE_STEP_EXECUTION_CONTEXT), stepExecution.getId());
240+
}
241+
220242
public void setLobHandler(LobHandler lobHandler) {
221243
this.lobHandler = lobHandler;
222244
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 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.
@@ -94,6 +94,10 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements
9494
private static final String CREATE_JOB_PARAMETERS = "INSERT into %PREFIX%JOB_EXECUTION_PARAMS(JOB_EXECUTION_ID, KEY_NAME, TYPE_CD, "
9595
+ "STRING_VAL, DATE_VAL, LONG_VAL, DOUBLE_VAL, IDENTIFYING) values (?, ?, ?, ?, ?, ?, ?, ?)";
9696

97+
private static final String DELETE_JOB_EXECUTION = "DELETE FROM %PREFIX%JOB_EXECUTION WHERE JOB_EXECUTION_ID = ?";
98+
99+
private static final String DELETE_JOB_EXECUTION_PARAMETERS = "DELETE FROM %PREFIX%JOB_EXECUTION_PARAMS WHERE JOB_EXECUTION_ID = ?";
100+
97101
private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;
98102

99103
private DataFieldMaxValueIncrementer jobExecutionIncrementer;
@@ -303,6 +307,22 @@ public void synchronizeStatus(JobExecution jobExecution) {
303307
}
304308
}
305309

310+
/**
311+
* Delete the given job execution.
312+
* @param jobExecution the job execution to delete
313+
*/
314+
public void deleteJobExecution(JobExecution jobExecution) {
315+
getJdbcTemplate().update(getQuery(DELETE_JOB_EXECUTION), jobExecution.getId());
316+
}
317+
318+
/**
319+
* Delete the parameters associated with the given job execution.
320+
* @param jobExecution the job execution for which job parameters should be deleted
321+
*/
322+
public void deleteJobExecutionParameters(JobExecution jobExecution) {
323+
getJdbcTemplate().update(getQuery(DELETE_JOB_EXECUTION_PARAMETERS), jobExecution.getId());
324+
}
325+
306326
/**
307327
* Convenience method that inserts all parameters from the provided JobParameters.
308328
*

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 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.
@@ -85,6 +85,8 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements
8585

8686
private static final String FIND_LAST_JOBS_LIKE_NAME = "SELECT JOB_INSTANCE_ID, JOB_NAME from %PREFIX%JOB_INSTANCE where JOB_NAME like ? order by JOB_INSTANCE_ID desc";
8787

88+
private static final String DELETE_JOB_INSTANCE = "DELETE FROM %PREFIX%JOB_INSTANCE WHERE JOB_INSTANCE_ID = ?";
89+
8890
private DataFieldMaxValueIncrementer jobInstanceIncrementer;
8991

9092
private JobKeyGenerator<JobParameters> jobKeyGenerator = new DefaultJobKeyGenerator();
@@ -276,6 +278,14 @@ public int getJobInstanceCount(@Nullable String jobName) throws NoSuchJobExcepti
276278
}
277279
}
278280

281+
/**
282+
* Delete the job instance.
283+
* @param jobInstance the job instance to delete
284+
*/
285+
public void deleteJobInstance(JobInstance jobInstance) {
286+
getJdbcTemplate().update(getQuery(DELETE_JOB_INSTANCE), jobInstance.getId());
287+
}
288+
279289
/**
280290
* Setter for {@link DataFieldMaxValueIncrementer} to be used when generating primary
281291
* keys for {@link JobInstance} instances.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
105105
+ " on SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID " + "where JE.JOB_INSTANCE_ID = ?"
106106
+ " and SE.STEP_NAME = ?";
107107

108+
private static final String DELETE_STEP_EXECUTION = "DELETE FROM %PREFIX%STEP_EXECUTION "
109+
+ "WHERE STEP_EXECUTION_ID = ?";
110+
108111
private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;
109112

110113
private DataFieldMaxValueIncrementer stepExecutionIncrementer;
@@ -353,6 +356,14 @@ public int countStepExecutions(JobInstance jobInstance, String stepName) {
353356
jobInstance.getInstanceId(), stepName);
354357
}
355358

359+
/**
360+
* Delete the given step execution.
361+
* @param stepExecution the step execution to delete
362+
*/
363+
public void deleteStepExecution(StepExecution stepExecution) {
364+
getJdbcTemplate().update(getQuery(DELETE_STEP_EXECUTION), stepExecution.getId());
365+
}
366+
356367
private static class StepExecutionRowMapper implements RowMapper<StepExecution> {
357368

358369
private final JobExecution jobExecution;

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2018 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.
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.batch.core.JobExecution;
2323
import org.springframework.batch.core.JobInstance;
24+
import org.springframework.batch.core.StepExecution;
2425
import org.springframework.lang.Nullable;
2526

2627
/**
@@ -88,4 +89,22 @@ public interface JobExecutionDao {
8889
*/
8990
void synchronizeStatus(JobExecution jobExecution);
9091

92+
/**
93+
* Delete the given job execution.
94+
* @param jobExecution the job execution to delete
95+
* @since 5.0
96+
*/
97+
default void deleteJobExecution(JobExecution jobExecution) {
98+
throw new UnsupportedOperationException();
99+
}
100+
101+
/**
102+
* Delete the parameters associated with the given job execution.
103+
* @param jobExecution the job execution for which job parameters should be deleted
104+
* @since 5.0
105+
*/
106+
default void deleteJobExecutionParameters(JobExecution jobExecution) {
107+
throw new UnsupportedOperationException();
108+
}
109+
91110
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 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.
@@ -130,4 +130,13 @@ default JobInstance getLastJobInstance(String jobName) {
130130
*/
131131
int getJobInstanceCount(@Nullable String jobName) throws NoSuchJobException;
132132

133+
/**
134+
* Delete the job instance.
135+
* @param jobInstance the job instance to delete
136+
* @since 5.0
137+
*/
138+
default void deleteJobInstance(JobInstance jobInstance) {
139+
throw new UnsupportedOperationException();
140+
}
141+
133142
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,13 @@ default int countStepExecutions(JobInstance jobInstance, String stepName) {
9191
throw new UnsupportedOperationException();
9292
}
9393

94+
/**
95+
* Delete the given step execution.
96+
* @param stepExecution the step execution to delete
97+
* @since 5.0
98+
*/
99+
default void deleteStepExecution(StepExecution stepExecution) {
100+
throw new UnsupportedOperationException();
101+
}
102+
94103
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,24 @@ public JobExecution getLastJobExecution(String jobName, JobParameters jobParamet
279279

280280
}
281281

282+
@Override
283+
public void deleteStepExecution(StepExecution stepExecution) {
284+
this.ecDao.deleteExecutionContext(stepExecution);
285+
this.stepExecutionDao.deleteStepExecution(stepExecution);
286+
}
287+
288+
@Override
289+
public void deleteJobExecution(JobExecution jobExecution) {
290+
this.ecDao.deleteExecutionContext(jobExecution);
291+
this.jobExecutionDao.deleteJobExecutionParameters(jobExecution);
292+
this.jobExecutionDao.deleteJobExecution(jobExecution);
293+
}
294+
295+
@Override
296+
public void deleteJobInstance(JobInstance jobInstance) {
297+
this.jobInstanceDao.deleteJobInstance(jobInstance);
298+
}
299+
282300
@Override
283301
public JobInstance createJobInstance(String jobName, JobParameters jobParameters) {
284302
Assert.notNull(jobName, "A job name is required to create a JobInstance");

0 commit comments

Comments
 (0)