Skip to content

Commit 7eaaa02

Browse files
benelogfmbenhassine
authored andcommitted
Refine ResourcelessJobRepository lookups and add delete operations
Resolves #5319 Signed-off-by: Sanghyuk Jung <[email protected]>
1 parent f9cff16 commit 7eaaa02

File tree

2 files changed

+447
-14
lines changed

2 files changed

+447
-14
lines changed

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

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
import org.jspecify.annotations.Nullable;
2323

2424
import org.springframework.batch.core.BatchStatus;
25+
import org.springframework.batch.core.job.DefaultJobKeyGenerator;
2526
import org.springframework.batch.core.job.JobExecution;
2627
import org.springframework.batch.core.job.JobInstance;
28+
import org.springframework.batch.core.job.JobKeyGenerator;
2729
import org.springframework.batch.core.job.parameters.JobParameters;
2830
import org.springframework.batch.infrastructure.item.ExecutionContext;
2931
import org.springframework.batch.core.step.StepExecution;
@@ -45,6 +47,7 @@
4547
*
4648
* @since 5.2.0
4749
* @author Mahmoud Ben Hassine
50+
* @author Sanghyuk Jung
4851
*/
4952
public class ResourcelessJobRepository implements JobRepository {
5053

@@ -54,6 +57,34 @@ public class ResourcelessJobRepository implements JobRepository {
5457

5558
private long stepExecutionIdIncrementer = 0L;
5659

60+
private JobKeyGenerator jobKeyGenerator;
61+
62+
/**
63+
* Create a new {@link ResourcelessJobRepository} instance with a
64+
* {@link DefaultJobKeyGenerator}.
65+
*/
66+
public ResourcelessJobRepository() {
67+
this(new DefaultJobKeyGenerator());
68+
}
69+
70+
/**
71+
* Create a new {@link ResourcelessJobRepository} instance with the provided
72+
* {@link JobKeyGenerator}.
73+
* @param jobKeyGenerator the job key generator to use
74+
* @since 6.0.1
75+
*/
76+
public ResourcelessJobRepository(JobKeyGenerator jobKeyGenerator) {
77+
this.jobKeyGenerator = jobKeyGenerator;
78+
}
79+
80+
/**
81+
* Set the {@link JobKeyGenerator} to use.
82+
* @param jobKeyGenerator the job key generator
83+
*/
84+
public void setJobKeyGenerator(JobKeyGenerator jobKeyGenerator) {
85+
this.jobKeyGenerator = jobKeyGenerator;
86+
}
87+
5788
/*
5889
* ===================================================================================
5990
* Job operations
@@ -76,7 +107,7 @@ public List<String> getJobNames() {
76107

77108
@Override
78109
public List<JobInstance> getJobInstances(String jobName, int start, int count) {
79-
if (this.jobInstance == null) {
110+
if (this.jobInstance == null || !this.jobInstance.getJobName().equals(jobName)) {
80111
return Collections.emptyList();
81112
}
82113
return Collections.singletonList(this.jobInstance);
@@ -91,25 +122,34 @@ public List<JobInstance> getJobInstances(String jobName, int start, int count) {
91122
*/
92123
@Override
93124
public List<JobInstance> findJobInstances(String jobName) {
94-
if (this.jobInstance == null) {
125+
if (this.jobInstance == null || !this.jobInstance.getJobName().equals(jobName)) {
95126
return Collections.emptyList();
96127
}
97128
return Collections.singletonList(this.jobInstance);
98129
}
99130

100131
@Override
101132
@Nullable public JobInstance getJobInstance(long instanceId) {
133+
if (this.jobInstance == null || !(this.jobInstance.getId() == instanceId)) {
134+
return null;
135+
}
102136
return this.jobInstance;
103137
}
104138

105139
@Override
106140
@Nullable public JobInstance getLastJobInstance(String jobName) {
141+
if (this.jobInstance == null || !this.jobInstance.getJobName().equals(jobName)) {
142+
return null;
143+
}
107144
return this.jobInstance;
108145
}
109146

110147
@Override
111148
@Nullable public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
112-
return this.jobInstance;
149+
if (this.jobInstance == null || !this.jobInstance.getJobName().equals(jobName)) {
150+
return null;
151+
}
152+
return isJobKeyEquals(jobParameters) ? this.jobInstance : null;
113153
}
114154

115155
@SuppressWarnings("removal")
@@ -121,7 +161,9 @@ public boolean isJobInstanceExists(String jobName, JobParameters jobParameters)
121161

122162
@Override
123163
public long getJobInstanceCount(String jobName) {
124-
// FIXME should return 0 if jobInstance is null or the name is not matching
164+
if (this.jobInstance == null || !this.jobInstance.getJobName().equals(jobName)) {
165+
return 0;
166+
}
125167
return 1;
126168
}
127169

@@ -131,6 +173,14 @@ public JobInstance createJobInstance(String jobName, JobParameters jobParameters
131173
return this.jobInstance;
132174
}
133175

176+
@Override
177+
public void deleteJobInstance(JobInstance jobInstance) {
178+
if (this.jobInstance != null && this.jobInstance.getId() == jobInstance.getId()) {
179+
this.jobInstance = null;
180+
this.jobExecution = null;
181+
}
182+
}
183+
134184
/*
135185
* ===================================================================================
136186
* Job execution operations
@@ -139,24 +189,33 @@ public JobInstance createJobInstance(String jobName, JobParameters jobParameters
139189

140190
@Override
141191
@Nullable public JobExecution getJobExecution(long executionId) {
142-
// FIXME should return null if the id is not matching
192+
if (this.jobExecution == null || !(this.jobExecution.getId() == executionId)) {
193+
return null;
194+
}
143195
return this.jobExecution;
144196
}
145197

146198
@Override
147199
@Nullable public JobExecution getLastJobExecution(String jobName, JobParameters jobParameters) {
148-
// FIXME should return null if the job name is not matching
149-
return this.jobExecution;
200+
if (this.jobInstance == null || !this.jobInstance.getJobName().equals(jobName)) {
201+
return null;
202+
}
203+
return isJobKeyEquals(jobParameters) ? this.jobExecution : null;
150204
}
151205

152206
@Override
153207
@Nullable public JobExecution getLastJobExecution(JobInstance jobInstance) {
154-
// FIXME should return null if the job instance is not matching
208+
if (this.jobInstance == null || !(this.jobInstance.getId() == jobInstance.getId())) {
209+
return null;
210+
}
155211
return this.jobExecution;
156212
}
157213

158214
@Override
159215
public List<JobExecution> getJobExecutions(JobInstance jobInstance) {
216+
if (this.jobInstance == null || !(this.jobInstance.getId() == jobInstance.getId())) {
217+
return Collections.emptyList();
218+
}
160219
if (this.jobExecution == null) {
161220
return Collections.emptyList();
162221
}
@@ -187,6 +246,13 @@ public void updateExecutionContext(JobExecution jobExecution) {
187246
jobExecution.setLastUpdated(LocalDateTime.now());
188247
}
189248

249+
@Override
250+
public void deleteJobExecution(JobExecution jobExecution) {
251+
if (this.jobExecution != null && this.jobExecution.getId() == jobExecution.getId()) {
252+
this.jobExecution = null;
253+
}
254+
}
255+
190256
/*
191257
* ===================================================================================
192258
* Step execution operations
@@ -272,4 +338,13 @@ public void updateExecutionContext(StepExecution stepExecution) {
272338
stepExecution.setLastUpdated(LocalDateTime.now());
273339
}
274340

275-
}
341+
private boolean isJobKeyEquals(JobParameters jobParameters) {
342+
if (this.jobExecution == null) {
343+
return false;
344+
}
345+
String currentKey = this.jobKeyGenerator.generateKey(this.jobExecution.getJobParameters());
346+
String expectedKey = this.jobKeyGenerator.generateKey(jobParameters);
347+
return currentKey.equals(expectedKey);
348+
}
349+
350+
}

0 commit comments

Comments
 (0)