KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > monitor > EntityLockMonitor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.monitor;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.TreeSet JavaDoc;
30
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.Reference JavaDoc;
35 import javax.naming.StringRefAddr JavaDoc;
36
37 import org.jboss.naming.NonSerializableFactory;
38 import org.jboss.system.ServiceMBeanSupport;
39
40 /**
41  * MBean implementation for providing Locking Stats for EntityBeans
42  *
43  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
44  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
45  * @version $Revision: 37459 $
46  */

47 public class EntityLockMonitor extends ServiceMBeanSupport
48    implements EntityLockMonitorMBean
49 {
50    // Constants ----------------------------------------------------
51

52    public static final String JavaDoc JNDI_NAME = "EntityLockMonitor";
53    
54    // Protected -----------------------------------------------------
55

56    protected HashMap JavaDoc monitorMap = new HashMap JavaDoc();
57    protected long contenders = 0;
58    protected long maxContenders = 0;
59    protected ArrayList JavaDoc times = new ArrayList JavaDoc();
60    protected long contentions = 0;
61    protected long totalTime = 0;
62    protected long sumContenders = 0;
63    
64    // Constructors -------------------------------------------------
65

66    public EntityLockMonitor()
67    {
68       // empty
69
}
70    
71    // ServiceMBeanSupport overrides ---------------------------------
72

73    protected void startService()
74       throws Exception JavaDoc
75    {
76       bind();
77
78      log.info("EntityLockMonitor started");
79    }
80
81    protected void stopService() {
82       try
83       {
84          unbind();
85       }
86       catch (Exception JavaDoc ignored) {}
87
88      log.info("EntityLockMonitor stopped");
89    }
90    
91    // Attributes ----------------------------------------------------
92

93    /**
94     * @jmx.managed-attribute
95     */

96    public synchronized long getAverageContenders()
97    {
98       if (contentions == 0)
99       {
100          return 0;
101       }
102       else
103       {
104          return sumContenders / contentions;
105       }
106    }
107
108    /**
109     * @jmx.managed-attribute
110     */

111    public synchronized long getMaxContenders()
112    {
113       return maxContenders;
114    }
115
116    /**
117     * @jmx.managed-attribute
118     */

119    public synchronized long getMedianWaitTime()
120    {
121       if (times.size() < 1)
122       {
123          return 0;
124       }
125
126       Long JavaDoc[] alltimes = (Long JavaDoc[])times.toArray(new Long JavaDoc[times.size()]);
127       long[] thetimes = new long[alltimes.length];
128       for (int i = 0; i < thetimes.length; i++)
129       {
130          thetimes[i] = alltimes[i].longValue();
131       }
132       Arrays.sort(thetimes);
133       return thetimes[thetimes.length / 2];
134    }
135
136    /**
137     * @jmx.managed-attribute
138     */

139    public synchronized long getTotalContentions()
140    {
141       return contentions;
142    }
143    
144    // Operations ----------------------------------------------------
145

146    /**
147     * @jmx.managed-operation
148     */

149    public Set JavaDoc listMonitoredBeans()
150    {
151       synchronized(monitorMap)
152       {
153          return new TreeSet JavaDoc(monitorMap.keySet());
154       }
155    }
156    
157    /**
158     * @jmx.managed-operation
159     *
160     * @return the LockMonitor that corresponds to the jndiName or null
161     */

162    public LockMonitor getLockMonitor(String JavaDoc jndiName)
163    {
164       synchronized(monitorMap)
165       {
166          return (LockMonitor)monitorMap.get(jndiName);
167       }
168    }
169    
170    /**
171     * @jmx.managed-operation
172     */

173    public String JavaDoc printLockMonitor()
174    {
175       StringBuffer JavaDoc rtn = new StringBuffer JavaDoc();
176       rtn.append("<table width=\"1\" border=\"1\">");
177       rtn.append("<tr><td><b>EJB JNDI-NAME</b></td><td><b>Total Lock Time</b></td><td><b>Num Contentions</b></td><td><b>Time Outs</b></td><td><b>Max Contenders</b></td></tr>");
178       synchronized(monitorMap)
179       {
180          Iterator JavaDoc it = monitorMap.keySet().iterator();
181          while (it.hasNext())
182          {
183             rtn.append("<tr>");
184             String JavaDoc jndiName = (String JavaDoc)it.next();
185             rtn.append("<td>");
186             rtn.append(jndiName);
187             rtn.append("</td>");
188             LockMonitor lm = (LockMonitor)monitorMap.get(jndiName);
189             rtn.append("<td>");
190             rtn.append(("" + lm.getTotalTime()));
191             rtn.append("</td><td>");
192             rtn.append(("" + lm.getNumContentions()));
193             rtn.append("</td><td>");
194             rtn.append(("" + lm.getTimeouts()));
195             rtn.append("</td><td>");
196             rtn.append(("" + lm.getMaxContenders()));
197             rtn.append("</td></tr>");
198          }
199       }
200       rtn.append("</table>");
201       return rtn.toString();
202    }
203    
204    /**
205     * @jmx.managed-operation
206     */

207    public synchronized void clearMonitor()
208    {
209       contenders = 0;
210       maxContenders = 0;
211       times.clear();
212       contentions = 0;
213       totalTime = 0;
214       sumContenders = 0;
215
216       synchronized(monitorMap)
217       {
218          Iterator JavaDoc it = monitorMap.keySet().iterator();
219          while (it.hasNext())
220          {
221             String JavaDoc jndiName = (String JavaDoc)it.next();
222             LockMonitor lm = (LockMonitor)monitorMap.get(jndiName);
223             lm.reset();
224          }
225       }
226    }
227    
228    // Public -------------------------------------------------------
229

230    public synchronized void incrementContenders()
231    {
232       ++contenders;
233       ++contentions;
234       sumContenders += contenders;
235       
236       if (contenders > maxContenders)
237       {
238          maxContenders = contenders;
239       }
240    }
241    
242    public synchronized void decrementContenders(long time)
243    {
244       times.add(new Long JavaDoc(time));
245       --contenders;
246    }
247
248    public LockMonitor getEntityLockMonitor(String JavaDoc jndiName)
249    {
250       LockMonitor lm = null;
251       
252       synchronized(monitorMap)
253       {
254          lm = (LockMonitor)monitorMap.get(jndiName);
255          if (lm == null)
256          {
257             lm = new LockMonitor(this);
258             monitorMap.put(jndiName, lm);
259          }
260       }
261       return lm;
262    }
263    
264    // Private -------------------------------------------------------
265

266    private void bind() throws NamingException JavaDoc
267    {
268       Context JavaDoc ctx = new InitialContext JavaDoc();
269
270       // Ah ! We aren't serializable, so we use a helper class
271
NonSerializableFactory.bind(JNDI_NAME, this);
272       
273       // The helper class NonSerializableFactory uses address type nns, we go on to
274
// use the helper class to bind ourselves in JNDI
275
StringRefAddr JavaDoc addr = new StringRefAddr JavaDoc("nns", JNDI_NAME);
276       Reference JavaDoc ref = new Reference JavaDoc(EntityLockMonitor.class.getName(), addr, NonSerializableFactory.class.getName(), null);
277       ctx.bind(JNDI_NAME, ref);
278    }
279    
280    private void unbind() throws NamingException JavaDoc
281    {
282       new InitialContext JavaDoc().unbind(JNDI_NAME);
283       NonSerializableFactory.unbind(JNDI_NAME);
284    }
285    
286 }
Popular Tags