KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > eviction > EvictionTimerTask


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  * Created on March 25 2003
7  */

8 package org.jboss.cache.eviction;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.jboss.cache.AbstractCacheListener;
13 import org.jboss.cache.CacheSPI;
14 import org.jboss.cache.Region;
15 import org.jboss.cache.notifications.Notifier;
16
17 import java.util.Collections JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Set JavaDoc;
21 import java.util.Timer JavaDoc;
22 import java.util.TimerTask JavaDoc;
23 import java.util.concurrent.atomic.AtomicInteger JavaDoc;
24
25 /**
26  * Timer threads to do periodic node clean up by running the eviction policy.
27  *
28  * @author Ben Wang 2-2004
29  * @author Daniel Huang (dhuang@jboss.org)
30  * @version $Revision: 1.11 $
31  */

32 public class EvictionTimerTask extends TimerTask JavaDoc
33 {
34    private Log log = LogFactory.getLog(EvictionTimerTask.class);
35
36    private final Set JavaDoc<Region> processedRegions;
37    private int wakeupIntervalSeconds;
38    private static AtomicInteger JavaDoc tcount = new AtomicInteger JavaDoc();
39
40    public EvictionTimerTask()
41    {
42       // synchronized set because we need to maintain thread safety
43
// for dynamic configuration purposes.
44
processedRegions = Collections.synchronizedSet(new HashSet JavaDoc<Region>());
45    }
46
47    public void init(int wakeupIntervalSeconds, Notifier notifier)
48    {
49       this.wakeupIntervalSeconds = wakeupIntervalSeconds;
50       if (log.isTraceEnabled())
51          log.trace("Creating a new eviction listener with wakeupIntervalSeconds set at " + wakeupIntervalSeconds);
52       EvictionListener l = new EvictionListener();
53       notifier.setEvictionPolicyListener(l);
54    }
55
56    /**
57     * Add a MarshRegion to process by the Eviction Thread.
58     *
59     * @param region MarshRegion to process.
60     */

61    public void addRegionToProcess(Region region)
62    {
63       processedRegions.add(region);
64    }
65
66    /**
67     * Remove a MarshRegion to process from the Eviction thread.
68     *
69     * @param region
70     */

71    public void removeRegionToProcess(Region region)
72    {
73       processedRegions.remove(region);
74    }
75
76    /**
77     * Run the eviction thread.
78     * <p/>
79     * This thread will synchronize the set of regions and iterate through every MarshRegion registered w/ the
80     * Eviction thread. It also synchronizes on each individual region as it is being processed.
81     */

82    public void run()
83    {
84       synchronized (processedRegions)
85       {
86          Iterator JavaDoc it = processedRegions.iterator();
87          while (it.hasNext())
88          {
89             final Region region = (Region) it.next();
90             final EvictionPolicy policy = region.getEvictionPolicy();
91
92             synchronized (region)
93             {
94                final EvictionAlgorithm algo = policy.getEvictionAlgorithm();
95                if (algo == null)
96                   throw new NullPointerException JavaDoc("algorithm null");
97                try
98                {
99                   algo.process(region);
100                }
101                catch (EvictionException e)
102                {
103                   log.error("run(): error processing eviction with exception: " + e.toString()
104                           + " will reset the eviction queue list.");
105                   region.resetEvictionQueues();
106                   log.debug("trace", e);
107                }
108             }
109          }
110       }
111    }
112
113    public boolean isRegionRegisteredForProcessing(Region region)
114    {
115       return processedRegions.contains(region);
116    }
117
118    class EvictionListener extends AbstractCacheListener
119    {
120       private Log log = LogFactory.getLog(EvictionListener.class);
121       private Timer JavaDoc evictionThread;
122
123       public void cacheStarted(CacheSPI cache)
124       {
125          evictionThread = new Timer JavaDoc("EvictionTimer-" + tcount.getAndIncrement());
126          evictionThread.schedule(EvictionTimerTask.this, wakeupIntervalSeconds * 1000, wakeupIntervalSeconds * 1000);
127       }
128
129       public void cacheStopped(CacheSPI cache)
130       {
131          log.debug("Stopping eviction timer");
132
133          if (evictionThread != null)
134          {
135             evictionThread.cancel();
136          }
137          evictionThread = null;
138       }
139    }
140
141 }
142
143
144
Popular Tags