1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activemq.management;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.codehaus.activemq.util.IndentPrinter;
23
24 import javax.jms.Message;
25 import javax.management.j2ee.statistics.CountStatistic;
26 import javax.management.j2ee.statistics.JMSEndpointStats;
27 import javax.management.j2ee.statistics.TimeStatistic;
28
29 /***
30 * Statistics for a JMS endpoint
31 *
32 * @version $Revision: 1.8 $
33 */
34 public class JMSEndpointStatsImpl extends StatsImpl implements JMSEndpointStats {
35 private static final Log log = LogFactory.getLog(JMSEndpointStatsImpl.class);
36
37 protected CountStatisticImpl messageCount;
38 protected CountStatisticImpl pendingMessageCount;
39 protected CountStatisticImpl expiredMessageCount;
40 protected TimeStatistic messageWaitTime;
41 protected TimeStatisticImpl messageRateTime;
42
43 public JMSEndpointStatsImpl(JMSSessionStatsImpl sessionStats) {
44 this();
45 setParent(messageCount, sessionStats.getMessageCount());
46 setParent(pendingMessageCount, sessionStats.getPendingMessageCount());
47 setParent(expiredMessageCount, sessionStats.getExpiredMessageCount());
48 setParent(messageWaitTime, sessionStats.getMessageWaitTime());
49 setParent(messageRateTime, sessionStats.getMessageRateTime());
50 }
51
52 protected void setParent(CountStatistic child, CountStatistic parent) {
53 if (child instanceof CountStatisticImpl && parent instanceof CountStatisticImpl) {
54 CountStatisticImpl c = (CountStatisticImpl) child;
55 c.setParent((CountStatisticImpl) parent);
56 }
57 else {
58 log.warn("Cannot associate endpoint counters with session level counters as they are not both CountStatisticImpl clases. Endpoint: " + child + " session: " + parent);
59 }
60 }
61
62 protected void setParent(TimeStatistic child, TimeStatistic parent) {
63 if (child instanceof TimeStatisticImpl && parent instanceof TimeStatisticImpl) {
64 TimeStatisticImpl c = (TimeStatisticImpl) child;
65 c.setParent((TimeStatisticImpl) parent);
66 }
67 else {
68 log.warn("Cannot associate endpoint counters with session level counters as they are not both TimeStatisticImpl clases. Endpoint: " + child + " session: " + parent);
69 }
70 }
71
72
73 public JMSEndpointStatsImpl() {
74 this(new CountStatisticImpl("messageCount", "Number of messages processed"),
75 new CountStatisticImpl("pendingMessageCount", "Number of pending messages"),
76 new CountStatisticImpl("expiredMessageCount", "Number of expired messages"),
77 new TimeStatisticImpl("messageWaitTime", "Time spent by a message before being delivered"),
78 new TimeStatisticImpl("messageRateTime", "Time taken to process a message (thoughtput rate)"));
79 }
80
81 public JMSEndpointStatsImpl(CountStatisticImpl messageCount, CountStatisticImpl pendingMessageCount, CountStatisticImpl expiredMessageCount, TimeStatisticImpl messageWaitTime, TimeStatisticImpl messageRateTime) {
82 this.messageCount = messageCount;
83 this.pendingMessageCount = pendingMessageCount;
84 this.expiredMessageCount = expiredMessageCount;
85 this.messageWaitTime = messageWaitTime;
86 this.messageRateTime = messageRateTime;
87
88
89 addStatistic("messageCount", messageCount);
90 addStatistic("pendingMessageCount", pendingMessageCount);
91 addStatistic("expiredMessageCount", expiredMessageCount);
92 addStatistic("messageWaitTime", messageWaitTime);
93 addStatistic("messageRateTime", messageRateTime);
94 }
95
96 public CountStatistic getMessageCount() {
97 return messageCount;
98 }
99
100 public CountStatistic getPendingMessageCount() {
101 return pendingMessageCount;
102 }
103
104 public CountStatistic getExpiredMessageCount() {
105 return expiredMessageCount;
106 }
107
108 public TimeStatistic getMessageWaitTime() {
109 return messageWaitTime;
110 }
111
112 public String toString() {
113 StringBuffer buffer = new StringBuffer();
114 buffer.append(messageCount);
115 buffer.append(" ");
116 buffer.append(messageRateTime);
117 buffer.append(" ");
118 buffer.append(pendingMessageCount);
119 buffer.append(" ");
120 buffer.append(expiredMessageCount);
121 buffer.append(" ");
122 buffer.append(messageWaitTime);
123 return buffer.toString();
124 }
125
126 public void onMessage(Message message) {
127 long start = messageCount.getLastSampleTime();
128 messageCount.increment();
129 long end = messageCount.getLastSampleTime();
130 messageRateTime.addTime(end - start);
131 }
132
133 public void dump(IndentPrinter out) {
134 out.printIndent();
135 out.println(messageCount);
136 out.printIndent();
137 out.println(messageRateTime);
138 out.printIndent();
139 out.println(pendingMessageCount);
140 out.printIndent();
141 out.println(messageRateTime);
142 out.printIndent();
143 out.println(expiredMessageCount);
144 out.printIndent();
145 out.println(messageWaitTime);
146 }
147 }