Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.jspecify.annotations.Nullable;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.job.DefaultJobKeyGenerator;
import org.springframework.batch.core.job.JobExecution;
import org.springframework.batch.core.job.JobInstance;
import org.springframework.batch.core.job.JobKeyGenerator;
import org.springframework.batch.core.job.parameters.JobParameters;
import org.springframework.batch.infrastructure.item.ExecutionContext;
import org.springframework.batch.core.step.StepExecution;
Expand All @@ -45,6 +47,7 @@
*
* @since 5.2.0
* @author Mahmoud Ben Hassine
* @author Sanghyuk Jung
*/
public class ResourcelessJobRepository implements JobRepository {

Expand All @@ -54,6 +57,8 @@ public class ResourcelessJobRepository implements JobRepository {

private long stepExecutionIdIncrementer = 0L;

private JobKeyGenerator jobKeyGenerator = new DefaultJobKeyGenerator();

/*
* ===================================================================================
* Job operations
Expand All @@ -76,7 +81,7 @@ public List<String> getJobNames() {

@Override
public List<JobInstance> getJobInstances(String jobName, int start, int count) {
if (this.jobInstance == null) {
if (this.jobInstance == null || !this.jobInstance.getJobName().equals(jobName)) {
return Collections.emptyList();
}
return Collections.singletonList(this.jobInstance);
Expand All @@ -91,24 +96,36 @@ public List<JobInstance> getJobInstances(String jobName, int start, int count) {
*/
@Override
public List<JobInstance> findJobInstances(String jobName) {
if (this.jobInstance == null) {
if (this.jobInstance == null || !this.jobInstance.getJobName().equals(jobName)) {
return Collections.emptyList();
}
return Collections.singletonList(this.jobInstance);
}

@Override
@Nullable public JobInstance getJobInstance(long instanceId) {
if (this.jobInstance == null || !(this.jobInstance.getId() == instanceId)) {
return null;
}
return this.jobInstance;
}

@Override
@Nullable public JobInstance getLastJobInstance(String jobName) {
if (this.jobInstance == null || !this.jobInstance.getJobName().equals(jobName)) {
return null;
}
return this.jobInstance;
}

@Override
@Nullable public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
if (this.jobInstance == null || !this.jobInstance.getJobName().equals(jobName)) {
return null;
}
if (!isJobKeyEquals(jobParameters)) {
return null;
}
return this.jobInstance;
}

Expand All @@ -121,7 +138,9 @@ public boolean isJobInstanceExists(String jobName, JobParameters jobParameters)

@Override
public long getJobInstanceCount(String jobName) {
// FIXME should return 0 if jobInstance is null or the name is not matching
if (this.jobInstance == null || !this.jobInstance.getJobName().equals(jobName)) {
return 0;
}
return 1;
}

Expand All @@ -131,6 +150,14 @@ public JobInstance createJobInstance(String jobName, JobParameters jobParameters
return this.jobInstance;
}

@Override
public void deleteJobInstance(JobInstance jobInstance) {
if (this.jobInstance != null && this.jobInstance.getId() == jobInstance.getId()) {
this.jobInstance = null;
this.jobExecution = null;
}
}

/*
* ===================================================================================
* Job execution operations
Expand All @@ -139,24 +166,36 @@ public JobInstance createJobInstance(String jobName, JobParameters jobParameters

@Override
@Nullable public JobExecution getJobExecution(long executionId) {
// FIXME should return null if the id is not matching
if (this.jobExecution == null || !(this.jobExecution.getId() == executionId)) {
return null;
}
return this.jobExecution;
}

@Override
@Nullable public JobExecution getLastJobExecution(String jobName, JobParameters jobParameters) {
// FIXME should return null if the job name is not matching
if (this.jobInstance == null || !this.jobInstance.getJobName().equals(jobName)) {
return null;
}
if (!isJobKeyEquals(jobParameters)) {
return null;
}
return this.jobExecution;
}

@Override
@Nullable public JobExecution getLastJobExecution(JobInstance jobInstance) {
// FIXME should return null if the job instance is not matching
if (this.jobInstance == null || !(this.jobInstance.getId() == jobInstance.getId())) {
return null;
}
return this.jobExecution;
}

@Override
public List<JobExecution> getJobExecutions(JobInstance jobInstance) {
if (this.jobInstance == null || !(this.jobInstance.getId() == jobInstance.getId())) {
return Collections.emptyList();
}
if (this.jobExecution == null) {
return Collections.emptyList();
}
Expand Down Expand Up @@ -187,6 +226,13 @@ public void updateExecutionContext(JobExecution jobExecution) {
jobExecution.setLastUpdated(LocalDateTime.now());
}

@Override
public void deleteJobExecution(JobExecution jobExecution) {
if (this.jobExecution != null && this.jobExecution.getId() == jobExecution.getId()) {
this.jobExecution = null;
}
}

/*
* ===================================================================================
* Step execution operations
Expand Down Expand Up @@ -272,4 +318,13 @@ public void updateExecutionContext(StepExecution stepExecution) {
stepExecution.setLastUpdated(LocalDateTime.now());
}

}
private boolean isJobKeyEquals(JobParameters params) {
if (this.jobExecution == null) {
return false;
}
String jobKey1 = this.jobKeyGenerator.generateKey(this.jobExecution.getJobParameters());
String jobKey2 = this.jobKeyGenerator.generateKey(params);
return jobKey1.equals(jobKey2);
}

}
Loading