1 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 ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.Set ; 21 import java.util.Timer ; 22 import java.util.TimerTask ; 23 import java.util.concurrent.atomic.AtomicInteger ; 24 25 32 public class EvictionTimerTask extends TimerTask 33 { 34 private Log log = LogFactory.getLog(EvictionTimerTask.class); 35 36 private final Set <Region> processedRegions; 37 private int wakeupIntervalSeconds; 38 private static AtomicInteger tcount = new AtomicInteger (); 39 40 public EvictionTimerTask() 41 { 42 processedRegions = Collections.synchronizedSet(new HashSet <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 61 public void addRegionToProcess(Region region) 62 { 63 processedRegions.add(region); 64 } 65 66 71 public void removeRegionToProcess(Region region) 72 { 73 processedRegions.remove(region); 74 } 75 76 82 public void run() 83 { 84 synchronized (processedRegions) 85 { 86 Iterator 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 ("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 evictionThread; 122 123 public void cacheStarted(CacheSPI cache) 124 { 125 evictionThread = new Timer ("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 |