KickJava   Java API By Example, From Geeks To Geeks.

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


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.ResultCacheEntryEager;
34
35 /**
36  * This thread manages eager cache entries and remove them from the cache if
37  * they have expired.
38  *
39  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
40  * @version 1.0
41  */

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

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

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

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

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