KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > timer > vm > VMWorkerPersistence


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.timer.vm;
19
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedHashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.ArrayList JavaDoc;
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 /**
35  *
36  *
37  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
38  *
39  * */

40 public class VMWorkerPersistence implements WorkerPersistence {
41
42     private final Map JavaDoc tasks = Collections.synchronizedMap(new LinkedHashMap JavaDoc());
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 JavaDoc(id), workInfo);
50     }
51
52     public void cancel(long id) throws PersistenceException {
53         tasks.remove(new Long JavaDoc(id));
54     }
55
56     public void playback(String JavaDoc key, Playback playback) throws PersistenceException {
57         synchronized (tasks) {
58             for (Iterator JavaDoc iterator = tasks.entrySet().iterator(); iterator.hasNext();) {
59                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) 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         //don't do anything, we are sharing the object with NonTransactionalWork, which is incrementing the time itself.
68
// Long key = new Long(id);
69
// synchronized (tasks) {
70
// WorkInfo task = (WorkInfo) tasks.get(key);
71
// task.nextTime();
72
// //see if task was cancelled while we executed.
73
// if (task != null) {
74
// tasks.put(key, TaskWrapper.nextTask(task));
75
// }
76
// }
77
}
78
79     public void intervalWorkPerformed(long id, long period) throws PersistenceException {
80         //dont do anything... sharing data with WorkInfo.
81
}
82
83     public Collection JavaDoc getIdsByKey(String JavaDoc key, Object JavaDoc userId) throws PersistenceException {
84         Collection JavaDoc ids = new ArrayList JavaDoc();
85         synchronized(tasks) {
86             for (Iterator JavaDoc 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 JavaDoc(workInfo.getId()));
90                 }
91             }
92         }
93         return ids;
94     }
95
96 }
97
Popular Tags