1 10 11 package org.mule.util.monitor; 12 13 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.mule.umo.lifecycle.Disposable; 18 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.Timer ; 22 import java.util.TimerTask ; 23 24 32 33 public class ExpiryMonitor extends TimerTask implements Disposable 34 { 35 38 protected static Log logger = LogFactory.getLog(ExpiryMonitor.class); 39 40 private Timer timer; 41 private Map monitors; 42 43 public ExpiryMonitor() 44 { 45 this(1000); 46 } 47 48 public ExpiryMonitor(long monitorFrequency) 49 { 50 51 timer = new Timer (true); 52 timer.schedule(this, monitorFrequency, monitorFrequency); 53 monitors = new ConcurrentHashMap(); 54 } 55 56 63 public void addExpirable(long milliseconds, Expirable expirable) 64 { 65 if (isRegistered(expirable)) 66 { 67 resetExpirable(expirable); 68 } 69 else 70 { 71 if (logger.isDebugEnabled()) 72 { 73 logger.debug("Adding new expirable: " + expirable); 74 } 75 monitors.put(expirable, new ExpirableHolder(milliseconds, expirable)); 76 } 77 } 78 79 public boolean isRegistered(Expirable expirable) 80 { 81 return (monitors.get(expirable) != null); 82 } 83 84 public void removeExpirable(Expirable expirable) 85 { 86 if (logger.isDebugEnabled()) 87 { 88 logger.debug("Removing expirable: " + expirable); 89 } 90 monitors.remove(expirable); 91 } 92 93 public void resetExpirable(Expirable expirable) 94 { 95 ExpirableHolder eh = (ExpirableHolder)monitors.get(expirable); 96 if (eh != null) 97 { 98 eh.reset(); 99 if (logger.isDebugEnabled()) 100 { 101 logger.debug("Reset expirable: " + expirable); 102 } 103 } 104 } 105 106 109 public void run() 110 { 111 ExpirableHolder holder; 112 for (Iterator iterator = monitors.values().iterator(); iterator.hasNext();) 113 { 114 holder = (ExpirableHolder)iterator.next(); 115 if (holder.isExpired()) 116 { 117 removeExpirable(holder.getExpirable()); 118 holder.getExpirable().expired(); 119 } 120 } 121 } 122 123 public void dispose() 124 { 125 logger.info("disposing monitor"); 126 timer.cancel(); 127 ExpirableHolder holder; 128 for (Iterator iterator = monitors.values().iterator(); iterator.hasNext();) 129 { 130 holder = (ExpirableHolder)iterator.next(); 131 removeExpirable(holder.getExpirable()); 132 try 133 { 134 holder.getExpirable().expired(); 135 } 136 catch (Exception e) 137 { 138 logger.debug(e.getMessage()); 139 } 140 } 141 } 142 143 private class ExpirableHolder 144 { 145 146 private long milliseconds; 147 private Expirable expirable; 148 private long created; 149 150 public ExpirableHolder(long milliseconds, Expirable expirable) 151 { 152 this.milliseconds = milliseconds; 153 this.expirable = expirable; 154 created = System.currentTimeMillis(); 155 } 156 157 public long getMilliseconds() 158 { 159 return milliseconds; 160 } 161 162 public Expirable getExpirable() 163 { 164 return expirable; 165 } 166 167 public boolean isExpired() 168 { 169 return (System.currentTimeMillis() - milliseconds) > created; 170 } 171 172 public void reset() 173 { 174 created = System.currentTimeMillis(); 175 } 176 } 177 } 178 | Popular Tags |