KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > stats > CacheListener


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.stats;
23
24 import org.jboss.ejb.plugins.cmp.jdbc2.schema.Cache;
25 import org.jboss.logging.Logger;
26 import org.jboss.system.ServiceMBeanSupport;
27
28 import javax.management.ObjectName JavaDoc;
29
30 /**
31  * @jmx:mbean extends="org.jboss.system.ServiceMBean"
32  *
33  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
34  * @version <tt>$Revision: 37459 $</tt>
35  */

36 public class CacheListener
37    extends ServiceMBeanSupport
38    implements Cache.Listener, CacheListenerMBean
39 {
40    private static final Logger log = Logger.getLogger(CacheListener.class);
41
42    private ObjectName JavaDoc statsCollector;
43    private ObjectName JavaDoc cacheName;
44    private String JavaDoc tableName;
45
46    /**
47     * @jmx.managed-attribute
48     */

49    public ObjectName JavaDoc getStatsCollector()
50    {
51       return statsCollector;
52    }
53
54    /**
55     * @jmx.managed-attribute
56     */

57    public void setStatsCollector(ObjectName JavaDoc statsCollector)
58    {
59       this.statsCollector = statsCollector;
60    }
61
62    /**
63     * @jmx.managed-attribute
64     */

65    public ObjectName JavaDoc getCacheName()
66    {
67       return cacheName;
68    }
69
70    /**
71     * @jmx.managed-attribute
72     */

73    public void setCacheName(ObjectName JavaDoc cacheName)
74    {
75       this.cacheName = cacheName;
76    }
77
78    public void startService() throws Exception JavaDoc
79    {
80       tableName = cacheName.getKeyProperty("table");
81       getServer().invoke(cacheName, "registerListener", new Object JavaDoc[]{this}, new String JavaDoc[]{Cache.Listener.class.getName()});
82    }
83
84    // Cache.Listener implementation
85

86    public void contention(int partitionIndex, long time)
87    {
88       try
89       {
90          StatisticalItem item = new ContentionStats(tableName, partitionIndex, time);
91          server.invoke(statsCollector, "addStatisticalItem",
92             new Object JavaDoc[]{item},
93             new String JavaDoc[]{StatisticalItem.class.getName()});
94       }
95       catch(Exception JavaDoc e)
96       {
97          log.error("Failed to add invocation.", e);
98       }
99    }
100
101    public void eviction(int partitionIndex, Object JavaDoc pk, int size)
102    {
103       try
104       {
105          StatisticalItem item = new EvictionStats(tableName, partitionIndex, pk);
106          server.invoke(statsCollector, "addStatisticalItem",
107             new Object JavaDoc[]{item},
108             new String JavaDoc[]{StatisticalItem.class.getName()});
109       }
110       catch(Exception JavaDoc e)
111       {
112          log.error("Failed to add invocation.", e);
113       }
114    }
115
116    public void hit(int partitionIndex)
117    {
118       try
119       {
120          StatisticalItem item = new HitStats(tableName, partitionIndex);
121          server.invoke(statsCollector, "addStatisticalItem",
122             new Object JavaDoc[]{item},
123             new String JavaDoc[]{StatisticalItem.class.getName()});
124       }
125       catch(Exception JavaDoc e)
126       {
127          log.error("Failed to add invocation.", e);
128       }
129    }
130
131    public void miss(int partitionIndex)
132    {
133       try
134       {
135          StatisticalItem item = new MissStats(tableName, partitionIndex);
136          server.invoke(statsCollector, "addStatisticalItem",
137             new Object JavaDoc[]{item},
138             new String JavaDoc[]{StatisticalItem.class.getName()});
139       }
140       catch(Exception JavaDoc e)
141       {
142          log.error("Failed to add invocation.", e);
143       }
144    }
145
146    public static class HitStats extends AbstractStatisticalItem
147    {
148       public static final String JavaDoc NAME = "Cache Hits Per Transaction";
149
150       private final String JavaDoc tableName;
151       private final int partitionIndex;
152
153       public HitStats(String JavaDoc name, int partitionIndex)
154       {
155          super(NAME);
156          value = name + partitionIndex;
157          this.tableName = name;
158          this.partitionIndex = partitionIndex;
159       }
160
161       public String JavaDoc getTableName()
162       {
163          return tableName;
164       }
165
166       public int getPartitionIndex()
167       {
168          return partitionIndex;
169       }
170    }
171
172    public static class MissStats extends AbstractStatisticalItem
173    {
174       public static final String JavaDoc NAME = "Cache Misses Per Transaction";
175
176       public MissStats(String JavaDoc name, int partitionIndex)
177       {
178          super(NAME);
179          value = name;
180       }
181    }
182
183    public static class ContentionStats extends AbstractStatisticalItem
184    {
185       public static final String JavaDoc NAME = "Cache Contention Statistics Per Transaction";
186
187       private long maxContentionTime;
188       private long contentionTimeTotal;
189
190       public ContentionStats(String JavaDoc name, int partitionIndex, long ms)
191       {
192          super(NAME);
193          value = name;
194          contentionTimeTotal = maxContentionTime = ms;
195       }
196
197       public void merge(StatisticalItem item)
198       {
199          super.merge(item);
200
201          ContentionStats cs = (ContentionStats)item;
202          if(cs.maxContentionTime > maxContentionTime)
203          {
204             maxContentionTime = cs.maxContentionTime;
205          }
206          contentionTimeTotal += cs.contentionTimeTotal;
207       }
208
209       public long getContentionTimeTotal()
210       {
211          return contentionTimeTotal;
212       }
213
214       public long getMaxContentionTime()
215       {
216          return maxContentionTime;
217       }
218    }
219
220    public static class EvictionStats extends AbstractStatisticalItem
221    {
222       public static final String JavaDoc NAME = "Cache Eviction Statistics Per Transaction";
223
224       private final String JavaDoc tableName;
225       private final Object JavaDoc pk;
226
227       public EvictionStats(String JavaDoc tableName, int partitionIndex, Object JavaDoc pk)
228       {
229          super(NAME);
230          value = tableName;
231
232          this.tableName = tableName;
233          this.pk = pk;
234       }
235
236       public String JavaDoc getTableName()
237       {
238          return tableName;
239       }
240
241       public Object JavaDoc getPk()
242       {
243          return pk;
244       }
245    }
246 }
247
Popular Tags