KickJava   Java API By Example, From Geeks To Geeks.

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


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.ResultCacheEntryRelaxed;
33
34 /**
35  * This thread manages relaxed cache entries and remove them from the cache if
36  * their deadline has expired or they are dirty.
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
39  * @version 1.0
40  */

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

58   public RelaxedCacheThread(ResultCache cache)
59   {
60     super("RelaxedCacheThread");
61     this.cache = cache;
62   }
63
64   /**
65    * Creates a new <code>RelaxedCacheThread</code> object
66    *
67    * @param cache ResultCache creating this thread
68    * @param refreshCacheRate cache refresh rate in seconds
69    */

70   public RelaxedCacheThread(ResultCache cache, int refreshCacheRate)
71   {
72     this(cache);
73     this.refreshCacheRate = refreshCacheRate;
74   }
75
76   /**
77    * Returns the threadWakeUpTime value.
78    *
79    * @return Returns the threadWakeUpTime.
80    */

81   public long getThreadWakeUpTime()
82   {
83     return threadWakeUpTime;
84   }
85
86   /**
87    * @see java.lang.Runnable#run()
88    */

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

175   public synchronized void shutdown()
176   {
177     isKilled = true;
178     notify();
179   }
180
181 }
Popular Tags