2222import org .jspecify .annotations .Nullable ;
2323
2424import org .springframework .batch .core .BatchStatus ;
25+ import org .springframework .batch .core .job .DefaultJobKeyGenerator ;
2526import org .springframework .batch .core .job .JobExecution ;
2627import org .springframework .batch .core .job .JobInstance ;
28+ import org .springframework .batch .core .job .JobKeyGenerator ;
2729import org .springframework .batch .core .job .parameters .JobParameters ;
2830import org .springframework .batch .infrastructure .item .ExecutionContext ;
2931import org .springframework .batch .core .step .StepExecution ;
4547 *
4648 * @since 5.2.0
4749 * @author Mahmoud Ben Hassine
50+ * @author Sanghyuk Jung
4851 */
4952public class ResourcelessJobRepository implements JobRepository {
5053
@@ -54,6 +57,8 @@ public class ResourcelessJobRepository implements JobRepository {
5457
5558 private long stepExecutionIdIncrementer = 0L ;
5659
60+ private JobKeyGenerator jobKeyGenerator = new DefaultJobKeyGenerator ();
61+
5762 /*
5863 * ===================================================================================
5964 * Job operations
@@ -76,7 +81,7 @@ public List<String> getJobNames() {
7681
7782 @ Override
7883 public List <JobInstance > getJobInstances (String jobName , int start , int count ) {
79- if (this .jobInstance == null ) {
84+ if (this .jobInstance == null || ! this . jobInstance . getJobName (). equals ( jobName ) ) {
8085 return Collections .emptyList ();
8186 }
8287 return Collections .singletonList (this .jobInstance );
@@ -91,24 +96,36 @@ public List<JobInstance> getJobInstances(String jobName, int start, int count) {
9196 */
9297 @ Override
9398 public List <JobInstance > findJobInstances (String jobName ) {
94- if (this .jobInstance == null ) {
99+ if (this .jobInstance == null || ! this . jobInstance . getJobName (). equals ( jobName ) ) {
95100 return Collections .emptyList ();
96101 }
97102 return Collections .singletonList (this .jobInstance );
98103 }
99104
100105 @ Override
101106 @ Nullable public JobInstance getJobInstance (long instanceId ) {
107+ if (this .jobInstance == null || !(this .jobInstance .getId () == instanceId )) {
108+ return null ;
109+ }
102110 return this .jobInstance ;
103111 }
104112
105113 @ Override
106114 @ Nullable public JobInstance getLastJobInstance (String jobName ) {
115+ if (this .jobInstance == null || !this .jobInstance .getJobName ().equals (jobName )) {
116+ return null ;
117+ }
107118 return this .jobInstance ;
108119 }
109120
110121 @ Override
111122 @ Nullable public JobInstance getJobInstance (String jobName , JobParameters jobParameters ) {
123+ if (this .jobInstance == null || !this .jobInstance .getJobName ().equals (jobName )) {
124+ return null ;
125+ }
126+ if (!isJobKeyEquals (jobParameters )) {
127+ return null ;
128+ }
112129 return this .jobInstance ;
113130 }
114131
@@ -121,7 +138,9 @@ public boolean isJobInstanceExists(String jobName, JobParameters jobParameters)
121138
122139 @ Override
123140 public long getJobInstanceCount (String jobName ) {
124- // FIXME should return 0 if jobInstance is null or the name is not matching
141+ if (this .jobInstance == null || !this .jobInstance .getJobName ().equals (jobName )) {
142+ return 0 ;
143+ }
125144 return 1 ;
126145 }
127146
@@ -131,6 +150,14 @@ public JobInstance createJobInstance(String jobName, JobParameters jobParameters
131150 return this .jobInstance ;
132151 }
133152
153+ @ Override
154+ public void deleteJobInstance (JobInstance jobInstance ) {
155+ if (this .jobInstance != null && this .jobInstance .getId () == jobInstance .getId ()) {
156+ this .jobInstance = null ;
157+ this .jobExecution = null ;
158+ }
159+ }
160+
134161 /*
135162 * ===================================================================================
136163 * Job execution operations
@@ -139,24 +166,36 @@ public JobInstance createJobInstance(String jobName, JobParameters jobParameters
139166
140167 @ Override
141168 @ Nullable public JobExecution getJobExecution (long executionId ) {
142- // FIXME should return null if the id is not matching
169+ if (this .jobExecution == null || !(this .jobExecution .getId () == executionId )) {
170+ return null ;
171+ }
143172 return this .jobExecution ;
144173 }
145174
146175 @ Override
147176 @ Nullable public JobExecution getLastJobExecution (String jobName , JobParameters jobParameters ) {
148- // FIXME should return null if the job name is not matching
177+ if (this .jobInstance == null || !this .jobInstance .getJobName ().equals (jobName )) {
178+ return null ;
179+ }
180+ if (!isJobKeyEquals (jobParameters )) {
181+ return null ;
182+ }
149183 return this .jobExecution ;
150184 }
151185
152186 @ Override
153187 @ Nullable public JobExecution getLastJobExecution (JobInstance jobInstance ) {
154- // FIXME should return null if the job instance is not matching
188+ if (this .jobInstance == null || !(this .jobInstance .getId () == jobInstance .getId ())) {
189+ return null ;
190+ }
155191 return this .jobExecution ;
156192 }
157193
158194 @ Override
159195 public List <JobExecution > getJobExecutions (JobInstance jobInstance ) {
196+ if (this .jobInstance == null || !(this .jobInstance .getId () == jobInstance .getId ())) {
197+ return Collections .emptyList ();
198+ }
160199 if (this .jobExecution == null ) {
161200 return Collections .emptyList ();
162201 }
@@ -187,6 +226,13 @@ public void updateExecutionContext(JobExecution jobExecution) {
187226 jobExecution .setLastUpdated (LocalDateTime .now ());
188227 }
189228
229+ @ Override
230+ public void deleteJobExecution (JobExecution jobExecution ) {
231+ if (this .jobExecution != null && this .jobExecution .getId () == jobExecution .getId ()) {
232+ this .jobExecution = null ;
233+ }
234+ }
235+
190236 /*
191237 * ===================================================================================
192238 * Step execution operations
@@ -272,4 +318,13 @@ public void updateExecutionContext(StepExecution stepExecution) {
272318 stepExecution .setLastUpdated (LocalDateTime .now ());
273319 }
274320
275- }
321+ private boolean isJobKeyEquals (JobParameters params ) {
322+ if (this .jobExecution == null ) {
323+ return false ;
324+ }
325+ String jobKey1 = this .jobKeyGenerator .generateKey (this .jobExecution .getJobParameters ());
326+ String jobKey2 = this .jobKeyGenerator .generateKey (params );
327+ return jobKey1 .equals (jobKey2 );
328+ }
329+
330+ }
0 commit comments