| 1 | /* |
| 2 | * Copyright 2006-2013 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 | * http://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 | |
| 17 | package org.springframework.batch.core.repository.dao; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.Collections; |
| 21 | import java.util.Comparator; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.concurrent.ConcurrentHashMap; |
| 25 | import java.util.concurrent.atomic.AtomicLong; |
| 26 | |
| 27 | import org.springframework.batch.core.DefaultJobKeyGenerator; |
| 28 | import org.springframework.batch.core.JobExecution; |
| 29 | import org.springframework.batch.core.JobInstance; |
| 30 | import org.springframework.batch.core.JobKeyGenerator; |
| 31 | import org.springframework.batch.core.JobParameters; |
| 32 | import org.springframework.util.Assert; |
| 33 | |
| 34 | /** |
| 35 | * In-memory implementation of {@link JobInstanceDao}. |
| 36 | */ |
| 37 | public class MapJobInstanceDao implements JobInstanceDao { |
| 38 | |
| 39 | // JDK6 Make a ConcurrentSkipListSet: tends to add on end |
| 40 | private final Map<String, JobInstance> jobInstances = new ConcurrentHashMap<String, JobInstance>(); |
| 41 | // private final Set<JobInstance> jobInstances = new CopyOnWriteArraySet<JobInstance>(); |
| 42 | |
| 43 | private JobKeyGenerator<JobParameters> jobKeyGenerator = new DefaultJobKeyGenerator(); |
| 44 | |
| 45 | private final AtomicLong currentId = new AtomicLong(0L); |
| 46 | |
| 47 | public void clear() { |
| 48 | jobInstances.clear(); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public JobInstance createJobInstance(String jobName, JobParameters jobParameters) { |
| 53 | |
| 54 | Assert.state(getJobInstance(jobName, jobParameters) == null, "JobInstance must not already exist"); |
| 55 | |
| 56 | JobInstance jobInstance = new JobInstance(currentId.getAndIncrement(), jobName); |
| 57 | jobInstance.incrementVersion(); |
| 58 | jobInstances.put(jobName + jobKeyGenerator.generateKey(jobParameters), jobInstance); |
| 59 | |
| 60 | return jobInstance; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public JobInstance getJobInstance(String jobName, JobParameters jobParameters) { |
| 65 | return jobInstances.get(jobName + jobKeyGenerator.generateKey(jobParameters)); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public JobInstance getJobInstance(Long instanceId) { |
| 70 | for (Map.Entry<String, JobInstance> instanceEntry : jobInstances.entrySet()) { |
| 71 | JobInstance instance = instanceEntry.getValue(); |
| 72 | if (instance.getId().equals(instanceId)) { |
| 73 | return instance; |
| 74 | } |
| 75 | } |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public List<String> getJobNames() { |
| 81 | List<String> result = new ArrayList<String>(); |
| 82 | for (Map.Entry<String, JobInstance> instanceEntry : jobInstances.entrySet()) { |
| 83 | result.add(instanceEntry.getValue().getJobName()); |
| 84 | } |
| 85 | Collections.sort(result); |
| 86 | return result; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public List<JobInstance> getJobInstances(String jobName, int start, int count) { |
| 91 | List<JobInstance> result = new ArrayList<JobInstance>(); |
| 92 | for (Map.Entry<String, JobInstance> instanceEntry : jobInstances.entrySet()) { |
| 93 | JobInstance instance = instanceEntry.getValue(); |
| 94 | if (instance.getJobName().equals(jobName)) { |
| 95 | result.add(instance); |
| 96 | } |
| 97 | } |
| 98 | Collections.sort(result, new Comparator<JobInstance>() { |
| 99 | // sort by ID descending |
| 100 | @Override |
| 101 | public int compare(JobInstance o1, JobInstance o2) { |
| 102 | return Long.signum(o2.getId() - o1.getId()); |
| 103 | } |
| 104 | }); |
| 105 | |
| 106 | int startIndex = Math.min(start, result.size()); |
| 107 | int endIndex = Math.min(start + count, result.size()); |
| 108 | return result.subList(startIndex, endIndex); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public JobInstance getJobInstance(JobExecution jobExecution) { |
| 113 | return jobExecution.getJobInstance(); |
| 114 | } |
| 115 | |
| 116 | } |