| 1 | package org.springframework.batch.core.repository.dao; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Iterator; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import org.springframework.batch.core.JobExecution; |
| 9 | import org.springframework.batch.core.JobInstance; |
| 10 | import org.springframework.batch.support.transaction.TransactionAwareProxyFactory; |
| 11 | import org.springframework.util.Assert; |
| 12 | |
| 13 | /** |
| 14 | * In-memory implementation of {@link JobExecutionDao}. |
| 15 | * |
| 16 | */ |
| 17 | public class MapJobExecutionDao implements JobExecutionDao { |
| 18 | |
| 19 | private static Map executionsById = TransactionAwareProxyFactory.createTransactionalMap(); |
| 20 | |
| 21 | private static long currentId; |
| 22 | |
| 23 | public static void clear() { |
| 24 | executionsById.clear(); |
| 25 | } |
| 26 | |
| 27 | public int getJobExecutionCount(JobInstance jobInstance) { |
| 28 | int count = 0; |
| 29 | for (Iterator iterator = executionsById.values().iterator(); iterator.hasNext();) { |
| 30 | JobExecution exec = (JobExecution) iterator.next(); |
| 31 | if (exec.getJobInstance().equals(jobInstance)) { |
| 32 | count++; |
| 33 | } |
| 34 | } |
| 35 | return count; |
| 36 | } |
| 37 | |
| 38 | public void saveJobExecution(JobExecution jobExecution) { |
| 39 | Assert.isTrue(jobExecution.getId() == null); |
| 40 | Long newId = new Long(currentId++); |
| 41 | jobExecution.setId(newId); |
| 42 | jobExecution.incrementVersion(); |
| 43 | executionsById.put(newId, jobExecution); |
| 44 | } |
| 45 | |
| 46 | public List findJobExecutions(JobInstance jobInstance) { |
| 47 | List executions = new ArrayList(); |
| 48 | for (Iterator iterator = executionsById.values().iterator(); iterator.hasNext();) { |
| 49 | JobExecution exec = (JobExecution) iterator.next(); |
| 50 | if (exec.getJobInstance().equals(jobInstance)) { |
| 51 | executions.add(exec); |
| 52 | } |
| 53 | } |
| 54 | return executions; |
| 55 | } |
| 56 | |
| 57 | public void updateJobExecution(JobExecution jobExecution) { |
| 58 | Long id = jobExecution.getId(); |
| 59 | Assert.notNull(id, "JobExecution is expected to have an id (should be saved already)"); |
| 60 | Assert.notNull(executionsById.get(id), "JobExecution must already be saved"); |
| 61 | jobExecution.incrementVersion(); |
| 62 | executionsById.put(id, jobExecution); |
| 63 | } |
| 64 | |
| 65 | public JobExecution getLastJobExecution(JobInstance jobInstance) { |
| 66 | JobExecution lastExec = null; |
| 67 | for (Iterator iterator = executionsById.values().iterator(); iterator.hasNext();) { |
| 68 | JobExecution exec = (JobExecution) iterator.next(); |
| 69 | if (!exec.getJobInstance().equals(jobInstance)) { |
| 70 | continue; |
| 71 | } |
| 72 | if (lastExec == null) { |
| 73 | lastExec = exec; |
| 74 | } |
| 75 | if (lastExec.getStartTime().getTime() < exec.getStartTime().getTime()) { |
| 76 | lastExec = exec; |
| 77 | } |
| 78 | } |
| 79 | return lastExec; |
| 80 | } |
| 81 | } |