KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > cache > result > threads > RelaxedCacheThread


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.controller.cache.result.threads;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import org.objectweb.cjdbc.common.i18n.Translate;
31 import org.objectweb.cjdbc.common.log.Trace;
32 import org.objectweb.cjdbc.controller.cache.result.ResultCache;
33 import org.objectweb.cjdbc.controller.cache.result.entries.ResultCacheEntryRelaxed;
34
35 /**
36  * This thread manages relaxed cache entries and remove them from the cache if
37  * their deadline has expired or they are dirty.
38  *
39  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
40  * @version 1.0
41  */

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

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

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

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

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

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