View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.timer.vm;
20  
21  import java.util.Collections;
22  import java.util.Iterator;
23  import java.util.LinkedHashMap;
24  import java.util.Map;
25  import java.util.Collection;
26  import java.util.ArrayList;
27  
28  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong;
29  
30  import org.apache.geronimo.timer.PersistenceException;
31  import org.apache.geronimo.timer.Playback;
32  import org.apache.geronimo.timer.WorkInfo;
33  import org.apache.geronimo.timer.WorkerPersistence;
34  
35  /**
36   *
37   *
38   * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
39   *
40   * */
41  public class VMWorkerPersistence implements WorkerPersistence {
42  
43      private final Map tasks = Collections.synchronizedMap(new LinkedHashMap());
44  
45      private final AtomicLong counter = new AtomicLong(0);
46  
47      public void save(WorkInfo workInfo) throws PersistenceException {
48          long id = counter.incrementAndGet();
49          workInfo.setId(id);
50          tasks.put(new Long(id), workInfo);
51      }
52  
53      public void cancel(long id) throws PersistenceException {
54          tasks.remove(new Long(id));
55      }
56  
57      public void playback(String key, Playback playback) throws PersistenceException {
58          synchronized (tasks) {
59              for (Iterator iterator = tasks.entrySet().iterator(); iterator.hasNext();) {
60                  Map.Entry entry = (Map.Entry) iterator.next();
61                  WorkInfo workInfo = (WorkInfo) entry.getValue();
62                  playback.schedule(workInfo);
63              }
64          }
65      }
66  
67      public void fixedRateWorkPerformed(long id) throws PersistenceException {
68          //don't do anything, we are sharing the object with NonTransactionalWork, which is incrementing the time itself.
69  //        Long key = new Long(id);
70  //        synchronized (tasks) {
71  //            WorkInfo task = (WorkInfo) tasks.get(key);
72  //            task.nextTime();
73  //            //see if task was cancelled while we executed.
74  //            if (task != null) {
75  //                tasks.put(key, TaskWrapper.nextTask(task));
76  //            }
77  //        }
78      }
79  
80      public void intervalWorkPerformed(long id, long period) throws PersistenceException {
81          //dont do anything... sharing data with WorkInfo.
82      }
83  
84      public Collection getIdsByKey(String key, Object userId) throws PersistenceException {
85          Collection ids = new ArrayList();
86          synchronized(tasks) {
87              for (Iterator iterator = tasks.values().iterator(); iterator.hasNext();) {
88                  WorkInfo workInfo = (WorkInfo) iterator.next();
89                  if (key.equals(workInfo.getKey()) && (userId == null || userId.equals(workInfo.getUserId()))) {
90                      ids.add(new Long(workInfo.getId()));
91                  }
92              }
93          }
94          return ids;
95      }
96  
97  }