KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > cache > result > threads > EagerCacheThread


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Nicolas Modrzyk.
21  * Contributor(s): ______________________.
22  */

23
24 package org.continuent.sequoia.controller.cache.result.threads;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.continuent.sequoia.common.i18n.Translate;
30 import org.continuent.sequoia.common.log.Trace;
31 import org.continuent.sequoia.controller.cache.result.ResultCache;
32 import org.continuent.sequoia.controller.cache.result.entries.ResultCacheEntryEager;
33
34 /**
35  * This thread manages eager cache entries and remove them from the cache if
36  * they have expired.
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
39  * @version 1.0
40  */

41 public final class EagerCacheThread extends Thread JavaDoc
42 {
43   private final ResultCache cache;
44   private long threadWakeUpTime = 0;
45   int refreshCacheRate = 60;
46   int refreshCacheTime = 60 / refreshCacheRate;
47   private Trace logger = Trace
48                                                  .getLogger(EagerCacheThread.class
49                                                      .getName());
50   private boolean isKilled = false;
51
52   /**
53    * Creates a new <code>EagerCacheThread</code> object
54    *
55    * @param cache ResultCache creating this thread
56    */

57   public EagerCacheThread(ResultCache cache)
58   {
59     super("EagerCacheThread");
60     this.cache = cache;
61   }
62
63   /**
64    * Returns the threadWakeUpTime value.
65    *
66    * @return Returns the threadWakeUpTime.
67    */

68   public long getThreadWakeUpTime()
69   {
70     return threadWakeUpTime;
71   }
72
73   /**
74    * @see java.lang.Runnable#run()
75    */

76   public void run()
77   {
78     ResultCacheEntryEager entry;
79     long now;
80     long sleep;
81     // Keep trace of relaxed cache entries to delete
82
ArrayList JavaDoc toRemoveFromEagerCache = new ArrayList JavaDoc();
83     while (!isKilled)
84     {
85       synchronized (this)
86       {
87         try
88         {
89           threadWakeUpTime = 0;
90           if (cache.getEagerCache().isEmpty())
91           { // Nothing in the cache, just sleep!
92
if (logger.isDebugEnabled())
93               logger.debug(Translate.get("cachethread.cache.empty.sleeping"));
94             wait();
95           }
96           else
97           { // Look for first deadline
98
now = System.currentTimeMillis();
99             for (Iterator JavaDoc iter = cache.getEagerCache().iterator(); iter
100                 .hasNext();)
101             {
102               entry = (ResultCacheEntryEager) iter.next();
103               if (entry.getDeadline() < now)
104               { // Deadline has expired, remove entry
105
toRemoveFromEagerCache.add(entry);
106                 continue;
107               }
108
109               // Recompute next wakeup time
110
if ((threadWakeUpTime == 0)
111                   || (entry.getDeadline() < threadWakeUpTime))
112                 threadWakeUpTime = entry.getDeadline();
113             }
114
115             // Clean up all the entries from the eager cache
116
int size = toRemoveFromEagerCache.size();
117             for (int i = 0; i < size; i++)
118             {
119               entry = (ResultCacheEntryEager) toRemoveFromEagerCache.get(i);
120               if (logger.isDebugEnabled())
121                 logger.debug(Translate.get(
122                     "cachethread.remove.entry.from.cache", entry.getRequest()
123                         .getUniqueKey()));
124               try
125               {
126                 cache.removeFromCache(entry.getRequest());
127               }
128               catch (Exception JavaDoc e)
129               {
130                 logger.warn("cachethread.remove.entry.error", e);
131               }
132               try
133               {
134                 cache.getEagerCache().remove(entry);
135               }
136               catch (Exception JavaDoc e)
137               {
138                 logger.warn("cachethread.remove.entry.error", e);
139               }
140             }
141             toRemoveFromEagerCache.clear();
142             if (threadWakeUpTime == 0)
143             { // All entries were not kept in the cache, therefore
144
// there is no next deadline. (and no cache entry to wait for)
145
continue;
146             }
147             else
148             { // Sleep until the next deadline
149
sleep = (threadWakeUpTime - now) / 1000 + refreshCacheTime;
150               if (logger.isDebugEnabled())
151               {
152                 logger.debug(Translate.get("cachethread.sleeping", sleep));
153               }
154               sleep = (sleep) * 1000;
155               wait(sleep);
156             }
157           }
158         }
159         catch (Exception JavaDoc e)
160         {
161           logger.warn(e.getMessage(), e);
162         }
163       }
164     }
165   }
166
167   /**
168    * Shutdown the current thread.
169    */

170   public synchronized void shutdown()
171   {
172     isKilled = true;
173     notify();
174   }
175
176 }
Popular Tags