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