KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > counter > CounterService


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.varia.counter;
23
24 import java.text.DecimalFormat JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Vector JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import javax.naming.Reference JavaDoc;
31 import javax.naming.StringRefAddr JavaDoc;
32 import org.jboss.system.ServiceMBeanSupport;
33 import org.jboss.naming.NonSerializableFactory;
34
35 /**
36  * A service offering accumulator style counters to help in diagnosing
37  * performance issues.
38  *
39  * @jmx:mbean extends="org.jboss.system.ServiceMBean"
40  *
41  * @author <a HREF="mailto:danch@nvisia.com">Dan Christopherson</href>
42  * @version $Revision: 37459 $
43  */

44 public class CounterService
45    extends ServiceMBeanSupport
46    implements CounterServiceMBean
47 {
48    public static final String JavaDoc JNDI_NAME = "java:/CounterService";
49
50    private HashMap JavaDoc counterMap = new HashMap JavaDoc();
51    
52    /**
53     * accumulate a count into a named counter. will initialize the named
54     * counter if neccessary.
55     */

56    public void accumulate(String JavaDoc counterName, long add)
57    {
58       Counter counter = null;
59       synchronized (counterMap)
60       {
61          counter = (Counter)counterMap.get(counterName);
62          if (counter == null)
63          {
64             counter = new Counter(counterName);
65             counterMap.put(counterName, counter);
66          }
67       }
68       counter.addToCount(add);
69    }
70    
71    protected void startService() throws Exception JavaDoc
72    {
73       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
74       
75       //bind myself into JNDI, at java:/CounterService
76
NonSerializableFactory.bind(JNDI_NAME, this);
77       StringRefAddr JavaDoc addr = new StringRefAddr JavaDoc("nns", JNDI_NAME);
78       Reference JavaDoc ref = new Reference JavaDoc(this.getClass().getName(), addr, NonSerializableFactory.class.getName(), null);
79       ctx.bind(JNDI_NAME, ref);
80    }
81    
82    protected void stopService() throws Exception JavaDoc
83    {
84       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
85       ctx.unbind(JNDI_NAME);
86       NonSerializableFactory.unbind(JNDI_NAME);
87    }
88    
89    /**
90     * @jmx:managed-operation
91     */

92    public String JavaDoc list()
93    {
94       DecimalFormat JavaDoc format = new DecimalFormat JavaDoc("####0.0000");
95       String JavaDoc retVal = "";
96       Iterator JavaDoc keys = counterMap.keySet().iterator();
97       while (keys.hasNext())
98       {
99          String JavaDoc key = (String JavaDoc)keys.next();
100          Counter counter = (Counter)counterMap.get(key);
101          long total = 0;
102          int entries = 0;
103          synchronized (counter)
104          {//so we dont catch half of it.
105
total = counter.getCount();
106             entries = counter.getEntries();
107          }
108          double avg = ((double)total)/((double)entries);
109          String JavaDoc descrip = key+": total="+total+" on "+entries+"entries for "+
110          "an average of "+format.format(avg)+"<br>\n";
111          retVal += descrip;
112       }
113       return retVal;
114    }
115    
116    private static class Counter
117    {
118       private String JavaDoc name;
119       private long count=0;
120       private int entries=0;
121       
122       public Counter(String JavaDoc n)
123       {
124          name = n;
125       }
126       
127       public String JavaDoc getName()
128       {
129          return name;
130       }
131       
132       public synchronized long getCount()
133       {
134          return count;
135       }
136       
137       public synchronized int getEntries()
138       {
139          return entries;
140       }
141       
142       public synchronized void addToCount(long add)
143       {
144          count += add;
145          entries++;
146       }
147    }
148 }
149
Popular Tags