001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package org.apache.geronimo.timer.vm; 020 021 import java.util.Collections; 022 import java.util.Iterator; 023 import java.util.LinkedHashMap; 024 import java.util.Map; 025 import java.util.Collection; 026 import java.util.ArrayList; 027 028 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong; 029 030 import org.apache.geronimo.timer.PersistenceException; 031 import org.apache.geronimo.timer.Playback; 032 import org.apache.geronimo.timer.WorkInfo; 033 import org.apache.geronimo.timer.WorkerPersistence; 034 035 /** 036 * 037 * 038 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $ 039 * 040 * */ 041 public class VMWorkerPersistence implements WorkerPersistence { 042 043 private final Map tasks = Collections.synchronizedMap(new LinkedHashMap()); 044 045 private final AtomicLong counter = new AtomicLong(0); 046 047 public void save(WorkInfo workInfo) throws PersistenceException { 048 long id = counter.incrementAndGet(); 049 workInfo.setId(id); 050 tasks.put(new Long(id), workInfo); 051 } 052 053 public void cancel(long id) throws PersistenceException { 054 tasks.remove(new Long(id)); 055 } 056 057 public void playback(String key, Playback playback) throws PersistenceException { 058 synchronized (tasks) { 059 for (Iterator iterator = tasks.entrySet().iterator(); iterator.hasNext();) { 060 Map.Entry entry = (Map.Entry) iterator.next(); 061 WorkInfo workInfo = (WorkInfo) entry.getValue(); 062 playback.schedule(workInfo); 063 } 064 } 065 } 066 067 public void fixedRateWorkPerformed(long id) throws PersistenceException { 068 //don't do anything, we are sharing the object with NonTransactionalWork, which is incrementing the time itself. 069 // Long key = new Long(id); 070 // synchronized (tasks) { 071 // WorkInfo task = (WorkInfo) tasks.get(key); 072 // task.nextTime(); 073 // //see if task was cancelled while we executed. 074 // if (task != null) { 075 // tasks.put(key, TaskWrapper.nextTask(task)); 076 // } 077 // } 078 } 079 080 public void intervalWorkPerformed(long id, long period) throws PersistenceException { 081 //dont do anything... sharing data with WorkInfo. 082 } 083 084 public Collection getIdsByKey(String key, Object userId) throws PersistenceException { 085 Collection ids = new ArrayList(); 086 synchronized(tasks) { 087 for (Iterator iterator = tasks.values().iterator(); iterator.hasNext();) { 088 WorkInfo workInfo = (WorkInfo) iterator.next(); 089 if (key.equals(workInfo.getKey()) && (userId == null || userId.equals(workInfo.getUserId()))) { 090 ids.add(new Long(workInfo.getId())); 091 } 092 } 093 } 094 return ids; 095 } 096 097 }