|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.batch.core.repository.dao; |
| 17 | + |
| 18 | +import java.util.HashSet; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import org.springframework.batch.core.JobExecution; |
| 23 | +import org.springframework.batch.core.JobInstance; |
| 24 | +import org.springframework.batch.core.repository.persistence.converter.JobExecutionConverter; |
| 25 | +import org.springframework.batch.core.repository.persistence.converter.JobInstanceConverter; |
| 26 | +import org.springframework.data.domain.Sort; |
| 27 | +import org.springframework.data.mongodb.core.MongoOperations; |
| 28 | +import org.springframework.data.mongodb.core.query.Query; |
| 29 | +import org.springframework.data.mongodb.core.query.Update; |
| 30 | +import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer; |
| 31 | + |
| 32 | +import static org.springframework.data.mongodb.core.query.Criteria.where; |
| 33 | +import static org.springframework.data.mongodb.core.query.Query.query; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author Mahmoud Ben Hassine |
| 37 | + * @since 5.2.0 |
| 38 | + */ |
| 39 | +public class MongoJobExecutionDao implements JobExecutionDao { |
| 40 | + |
| 41 | + private static final String JOB_EXECUTIONS_COLLECTION_NAME = "BATCH_JOB_EXECUTION"; |
| 42 | + |
| 43 | + private static final String JOB_EXECUTIONS_SEQUENCE_NAME = "BATCH_JOB_EXECUTION_SEQ"; |
| 44 | + |
| 45 | + private static final String JOB_INSTANCES_COLLECTION_NAME = "BATCH_JOB_INSTANCE"; |
| 46 | + |
| 47 | + private final MongoOperations mongoOperations; |
| 48 | + |
| 49 | + private final JobExecutionConverter jobExecutionConverter = new JobExecutionConverter(); |
| 50 | + |
| 51 | + private final JobInstanceConverter jobInstanceConverter = new JobInstanceConverter(); |
| 52 | + |
| 53 | + private DataFieldMaxValueIncrementer jobExecutionIncrementer; |
| 54 | + |
| 55 | + public MongoJobExecutionDao(MongoOperations mongoOperations) { |
| 56 | + this.mongoOperations = mongoOperations; |
| 57 | + this.jobExecutionIncrementer = new MongoSequenceIncrementer(mongoOperations, JOB_EXECUTIONS_SEQUENCE_NAME); |
| 58 | + } |
| 59 | + |
| 60 | + public void setJobExecutionIncrementer(DataFieldMaxValueIncrementer jobExecutionIncrementer) { |
| 61 | + this.jobExecutionIncrementer = jobExecutionIncrementer; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void saveJobExecution(JobExecution jobExecution) { |
| 66 | + org.springframework.batch.core.repository.persistence.JobExecution jobExecutionToSave = this.jobExecutionConverter |
| 67 | + .fromJobExecution(jobExecution); |
| 68 | + long jobExecutionId = this.jobExecutionIncrementer.nextLongValue(); |
| 69 | + jobExecutionToSave.setJobExecutionId(jobExecutionId); |
| 70 | + this.mongoOperations.insert(jobExecutionToSave, JOB_EXECUTIONS_COLLECTION_NAME); |
| 71 | + jobExecution.setId(jobExecutionId); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void updateJobExecution(JobExecution jobExecution) { |
| 76 | + Query query = query(where("jobExecutionId").is(jobExecution.getId())); |
| 77 | + org.springframework.batch.core.repository.persistence.JobExecution jobExecutionToUpdate = this.jobExecutionConverter |
| 78 | + .fromJobExecution(jobExecution); |
| 79 | + this.mongoOperations.findAndReplace(query, jobExecutionToUpdate, JOB_EXECUTIONS_COLLECTION_NAME); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public List<JobExecution> findJobExecutions(JobInstance jobInstance) { |
| 84 | + Query query = query(where("jobInstanceId").is(jobInstance.getId())); |
| 85 | + List<org.springframework.batch.core.repository.persistence.JobExecution> jobExecutions = this.mongoOperations |
| 86 | + .find(query, org.springframework.batch.core.repository.persistence.JobExecution.class, |
| 87 | + JOB_EXECUTIONS_COLLECTION_NAME); |
| 88 | + return jobExecutions.stream() |
| 89 | + .map(jobExecution -> this.jobExecutionConverter.toJobExecution(jobExecution, jobInstance)) |
| 90 | + .toList(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public JobExecution getLastJobExecution(JobInstance jobInstance) { |
| 95 | + Query query = query(where("jobInstanceId").is(jobInstance.getId())); |
| 96 | + Sort.Order sortOrder = Sort.Order.desc("jobExecutionId"); |
| 97 | + org.springframework.batch.core.repository.persistence.JobExecution jobExecution = this.mongoOperations.findOne( |
| 98 | + query.with(Sort.by(sortOrder)), |
| 99 | + org.springframework.batch.core.repository.persistence.JobExecution.class, |
| 100 | + JOB_EXECUTIONS_COLLECTION_NAME); |
| 101 | + return jobExecution != null ? this.jobExecutionConverter.toJobExecution(jobExecution, jobInstance) : null; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Set<JobExecution> findRunningJobExecutions(String jobName) { |
| 106 | + Query query = query(where("jobName").is(jobName)); |
| 107 | + List<JobInstance> jobInstances = this.mongoOperations |
| 108 | + .find(query, org.springframework.batch.core.repository.persistence.JobInstance.class, |
| 109 | + JOB_INSTANCES_COLLECTION_NAME) |
| 110 | + .stream() |
| 111 | + .map(this.jobInstanceConverter::toJobInstance) |
| 112 | + .toList(); |
| 113 | + Set<JobExecution> runningJobExecutions = new HashSet<>(); |
| 114 | + for (JobInstance jobInstance : jobInstances) { |
| 115 | + query = query( |
| 116 | + where("jobInstanceId").is(jobInstance.getId()).and("status").in("STARTING", "STARTED", "STOPPING")); |
| 117 | + this.mongoOperations |
| 118 | + .find(query, org.springframework.batch.core.repository.persistence.JobExecution.class, |
| 119 | + JOB_EXECUTIONS_COLLECTION_NAME) |
| 120 | + .stream() |
| 121 | + .map(jobExecution -> this.jobExecutionConverter.toJobExecution(jobExecution, jobInstance)) |
| 122 | + .forEach(runningJobExecutions::add); |
| 123 | + } |
| 124 | + return runningJobExecutions; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public JobExecution getJobExecution(Long executionId) { |
| 129 | + org.springframework.batch.core.repository.persistence.JobExecution jobExecution = this.mongoOperations.findById( |
| 130 | + executionId, org.springframework.batch.core.repository.persistence.JobExecution.class, |
| 131 | + JOB_EXECUTIONS_COLLECTION_NAME); |
| 132 | + if (jobExecution == null) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + org.springframework.batch.core.repository.persistence.JobInstance jobInstance = this.mongoOperations.findById( |
| 136 | + jobExecution.getJobInstanceId(), |
| 137 | + org.springframework.batch.core.repository.persistence.JobInstance.class, JOB_INSTANCES_COLLECTION_NAME); |
| 138 | + return this.jobExecutionConverter.toJobExecution(jobExecution, |
| 139 | + this.jobInstanceConverter.toJobInstance(jobInstance)); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void synchronizeStatus(JobExecution jobExecution) { |
| 144 | + Query query = query(where("jobExecutionId").is(jobExecution.getId())); |
| 145 | + Update update = Update.update("status", jobExecution.getStatus()); |
| 146 | + // TODO the contract mentions to update the version as well. Double check if this |
| 147 | + // is needed as the version is not used in the tests following the call sites of |
| 148 | + // synchronizeStatus |
| 149 | + this.mongoOperations.updateFirst(query, update, |
| 150 | + org.springframework.batch.core.repository.persistence.JobExecution.class, |
| 151 | + JOB_EXECUTIONS_COLLECTION_NAME); |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments