1 17 18 package org.apache.geronimo.timer.vm; 19 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.LinkedHashMap ; 23 import java.util.Map ; 24 import java.util.Collection ; 25 import java.util.ArrayList ; 26 27 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong; 28 29 import org.apache.geronimo.timer.PersistenceException; 30 import org.apache.geronimo.timer.Playback; 31 import org.apache.geronimo.timer.WorkInfo; 32 import org.apache.geronimo.timer.WorkerPersistence; 33 34 40 public class VMWorkerPersistence implements WorkerPersistence { 41 42 private final Map tasks = Collections.synchronizedMap(new LinkedHashMap ()); 43 44 private final AtomicLong counter = new AtomicLong(0); 45 46 public void save(WorkInfo workInfo) throws PersistenceException { 47 long id = counter.incrementAndGet(); 48 workInfo.setId(id); 49 tasks.put(new Long (id), workInfo); 50 } 51 52 public void cancel(long id) throws PersistenceException { 53 tasks.remove(new Long (id)); 54 } 55 56 public void playback(String key, Playback playback) throws PersistenceException { 57 synchronized (tasks) { 58 for (Iterator iterator = tasks.entrySet().iterator(); iterator.hasNext();) { 59 Map.Entry entry = (Map.Entry ) iterator.next(); 60 WorkInfo workInfo = (WorkInfo) entry.getValue(); 61 playback.schedule(workInfo); 62 } 63 } 64 } 65 66 public void fixedRateWorkPerformed(long id) throws PersistenceException { 67 } 78 79 public void intervalWorkPerformed(long id, long period) throws PersistenceException { 80 } 82 83 public Collection getIdsByKey(String key, Object userId) throws PersistenceException { 84 Collection ids = new ArrayList (); 85 synchronized(tasks) { 86 for (Iterator iterator = tasks.values().iterator(); iterator.hasNext();) { 87 WorkInfo workInfo = (WorkInfo) iterator.next(); 88 if (key.equals(workInfo.getKey()) && (userId == null || userId.equals(workInfo.getUserId()))) { 89 ids.add(new Long (workInfo.getId())); 90 } 91 } 92 } 93 return ids; 94 } 95 96 } 97 | Popular Tags |