KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > monitor > ExpiryMonitor


1 /*
2  * $Id: ExpiryMonitor.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

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 JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Timer JavaDoc;
22 import java.util.TimerTask JavaDoc;
23
24 /**
25  * <code>ExpiryMonitor</code> can monitor objects beased on an expiry time and can
26  * invoke a callback method once the object time has expired. If the object does
27  * expire it is removed from this monitor
28  *
29  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
30  * @version $Revision: 3798 $
31  */

32
33 public class ExpiryMonitor extends TimerTask JavaDoc implements Disposable
34 {
35     /**
36      * logger used by this class
37      */

38     protected static Log logger = LogFactory.getLog(ExpiryMonitor.class);
39
40     private Timer JavaDoc 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 JavaDoc(true);
52         timer.schedule(this, monitorFrequency, monitorFrequency);
53         monitors = new ConcurrentHashMap();
54     }
55
56     /**
57      * Adds an expirable object to monitor. If the Object is already being monitored
58      * it will be reset and the millisecond timeout will be ignored
59      *
60      * @param milliseconds
61      * @param expirable
62      */

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     /**
107      * The action to be performed by this timer task.
108      */

109     public void run()
110     {
111         ExpirableHolder holder;
112         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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