KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > management > beans > tx > ClientTxMonitor


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.management.beans.tx;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.management.NotCompliantMBeanException JavaDoc;
11 import javax.management.openmbean.CompositeData JavaDoc;
12 import javax.management.openmbean.OpenDataException JavaDoc;
13 import javax.management.openmbean.TabularData JavaDoc;
14
15 import com.tc.management.AbstractTerracottaMBean;
16 import com.tc.management.opentypes.adapters.ClassCreationCount;
17 import com.tc.management.stats.AggregateInteger;
18
19 public final class ClientTxMonitor extends AbstractTerracottaMBean implements ClientTxMonitorMBean {
20
21   final AggregateInteger readTransactions;
22   final AggregateInteger writeTransactions;
23   final AggregateInteger writesPerTransaction;
24   final AggregateInteger modifiedObjectsPerTransaction;
25   final AggregateInteger newObjectsPerTransaction;
26
27   final AggregateInteger notifiesPerTransaction;
28   final AggregateInteger writesPerObject;
29   final Map JavaDoc objectCreationCountByClass;
30
31   public ClientTxMonitor() throws NotCompliantMBeanException JavaDoc {
32     super(ClientTxMonitorMBean.class, false);
33     readTransactions = new AggregateInteger("Client transactions (read)", 100);
34     writeTransactions = new AggregateInteger("Client transactions (write)", 100);
35     writesPerTransaction = new AggregateInteger("Writes per transaction (includes Object.notify() calls)", 100);
36     modifiedObjectsPerTransaction = new AggregateInteger("Objects modified per transaction", 100);
37     newObjectsPerTransaction = new AggregateInteger("New objects created per transaction", 100);
38     notifiesPerTransaction = new AggregateInteger("Object.notify() invocations per transaction");
39     writesPerObject = new AggregateInteger("Modifications per object");
40     objectCreationCountByClass = new HashMap JavaDoc();
41   }
42
43   public int getReadTransactionCount() {
44     return readTransactions.getN();
45   }
46
47   public int getReadTransactionRatePerSecond() {
48     return readTransactions.getSampleRate(1000);
49   }
50
51   public int getWriteTransactionCount() {
52     return writeTransactions.getN();
53   }
54
55   public int getWriteTransactionRatePerSecond() {
56     return writeTransactions.getSampleRate(1000);
57   }
58
59   public int getMinWritesPerWriteTransaction() {
60     return writesPerTransaction.getMinimum();
61   }
62
63   public int getMaxWritesPerWriteTransaction() {
64     return writesPerTransaction.getMaximum();
65   }
66
67   public int getMaxModifiedObjectsPerTransaction() {
68     return modifiedObjectsPerTransaction.getMaximum();
69   }
70
71   public int getAvgModifiedObjectsPerTransaction() {
72     return (int) modifiedObjectsPerTransaction.getAverage();
73   }
74
75   public int getObjectModificationRatePerSecond() {
76     return modifiedObjectsPerTransaction.getSampleRate(1000);
77   }
78
79   public int getMaxNewObjectsPerTransaction() {
80     return newObjectsPerTransaction.getMaximum();
81   }
82
83   public int getAvgNewObjectsPerTransaction() {
84     return (int) newObjectsPerTransaction.getAverage();
85   }
86
87   public int getObjectCreationRatePerSecond() {
88     return newObjectsPerTransaction.getSampleRate(1000);
89   }
90
91   public int getMaxNotificationsPerTransaction() {
92     return notifiesPerTransaction.getMaximum();
93   }
94
95   public int getAvgNotificationsPerTransaction() {
96     return (int) notifiesPerTransaction.getAverage();
97   }
98
99   public int getMaxWritesPerObject() {
100     return writesPerObject.getMaximum();
101   }
102
103   public int getAvgWritesPerObject() {
104     return (int) writesPerObject.getAverage();
105   }
106
107   public TabularData JavaDoc getObjectCreationCountByClass() throws OpenDataException JavaDoc {
108     TabularData JavaDoc tabularData = ClassCreationCount.newTabularDataInstance();
109     CompositeData JavaDoc compositeData;
110
111     synchronized (objectCreationCountByClass) {
112       for (Iterator JavaDoc iter = objectCreationCountByClass.keySet().iterator(); iter.hasNext();) {
113         Class JavaDoc classNameItemValue = (Class JavaDoc) iter.next();
114         AggregateInteger objectCreationCountItemValue = (AggregateInteger) objectCreationCountByClass
115             .get(classNameItemValue);
116         compositeData = new ClassCreationCount(classNameItemValue.getName(), new Integer JavaDoc(objectCreationCountItemValue
117             .getSum())).toCompositeData();
118         tabularData.put(compositeData);
119       }
120     }
121     return tabularData;
122   }
123
124   public synchronized void reset() {
125     readTransactions.reset();
126     writeTransactions.reset();
127     writesPerTransaction.reset();
128     modifiedObjectsPerTransaction.reset();
129     newObjectsPerTransaction.reset();
130     notifiesPerTransaction.reset();
131     writesPerObject.reset();
132     synchronized (objectCreationCountByClass) {
133       objectCreationCountByClass.clear();
134     }
135   }
136
137   public synchronized void committedReadTransaction() {
138     if (isEnabled()) readTransactions.addSample(1);
139   }
140
141   public synchronized void committedWriteTransaction(final int notifyCount, final int modifiedObjectCount,
142                                                      final int[] writeCountPerObject, final Map JavaDoc newObjectCountByClass) {
143     if (isEnabled()) {
144       writeTransactions.addSample(1);
145       modifiedObjectsPerTransaction.addSample(modifiedObjectCount);
146       notifiesPerTransaction.addSample(notifyCount);
147       int totalWriteCount = 0;
148       for (int i = 0; i < writeCountPerObject.length; i++) {
149         totalWriteCount += writeCountPerObject[i];
150         writesPerObject.addSample(writeCountPerObject[i]);
151       }
152       writesPerTransaction.addSample(totalWriteCount + notifyCount);
153       if (newObjectCountByClass != null && !newObjectCountByClass.isEmpty()) {
154         int totalNewObjectCount = 0;
155         for (Iterator JavaDoc iter = newObjectCountByClass.keySet().iterator(); iter.hasNext();) {
156           final Class JavaDoc createdObjectClass = (Class JavaDoc) iter.next();
157           final Integer JavaDoc classCreationCount = (Integer JavaDoc) newObjectCountByClass.get(createdObjectClass);
158           synchronized (objectCreationCountByClass) {
159             AggregateInteger instanceCounter = (AggregateInteger) objectCreationCountByClass.get(createdObjectClass);
160             if (instanceCounter == null) {
161               instanceCounter = new AggregateInteger("Object creation count for class[" + createdObjectClass.getName()
162                                                      + "]");
163               objectCreationCountByClass.put(createdObjectClass, instanceCounter);
164             }
165             instanceCounter.addSample(classCreationCount.intValue());
166             totalNewObjectCount += classCreationCount.intValue();
167           }
168         }
169         newObjectsPerTransaction.addSample(totalNewObjectCount);
170       } else {
171         newObjectsPerTransaction.addSample(0);
172       }
173     }
174   }
175 }
176
Popular Tags