KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > management > JMSEndpointStatsImpl


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.management;
19
20 import javax.jms.Destination JavaDoc;
21 import javax.jms.MessageConsumer JavaDoc;
22 import javax.jms.MessageProducer JavaDoc;
23 import javax.jms.Session JavaDoc;
24
25 import org.apache.activemq.util.IndentPrinter;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * Statistics for a JMS endpoint, typically a MessageProducer or MessageConsumer
31  * but this class can also be used to represent statistics on a {@link Destination} as well.
32  *
33  * @version $Revision: 1.3 $
34  */

35 public class JMSEndpointStatsImpl extends StatsImpl {
36     private static final Log log = LogFactory.getLog(JMSEndpointStatsImpl.class);
37
38     protected CountStatisticImpl messageCount;
39     protected CountStatisticImpl pendingMessageCount;
40     protected CountStatisticImpl expiredMessageCount;
41     protected TimeStatisticImpl messageWaitTime;
42     protected TimeStatisticImpl messageRateTime;
43
44     /**
45      * This constructor is used to create statistics for a
46      * {@link MessageProducer} or {@link MessageConsumer} as it passes in a
47      * {@link Session} parent statistic.
48      *
49      * @param sessionStats
50      */

51     public JMSEndpointStatsImpl(JMSSessionStatsImpl sessionStats) {
52         this();
53         setParent(messageCount, sessionStats.getMessageCount());
54         setParent(pendingMessageCount, sessionStats.getPendingMessageCount());
55         setParent(expiredMessageCount, sessionStats.getExpiredMessageCount());
56         setParent(messageWaitTime, sessionStats.getMessageWaitTime());
57         setParent(messageRateTime, sessionStats.getMessageRateTime());
58     }
59
60     /**
61      * This constructor is typically used to create a statistics object for a
62      * {@link Destination}
63      */

64     public JMSEndpointStatsImpl() {
65         this(new CountStatisticImpl("messageCount", "Number of messages processed"),
66                 new CountStatisticImpl("pendingMessageCount", "Number of pending messages"),
67                 new CountStatisticImpl("expiredMessageCount", "Number of expired messages"),
68                 new TimeStatisticImpl("messageWaitTime", "Time spent by a message before being delivered"),
69                 new TimeStatisticImpl("messageRateTime", "Time taken to process a message (thoughtput rate)"));
70     }
71
72     public JMSEndpointStatsImpl(CountStatisticImpl messageCount, CountStatisticImpl pendingMessageCount, CountStatisticImpl expiredMessageCount, TimeStatisticImpl messageWaitTime, TimeStatisticImpl messageRateTime) {
73         this.messageCount = messageCount;
74         this.pendingMessageCount = pendingMessageCount;
75         this.expiredMessageCount = expiredMessageCount;
76         this.messageWaitTime = messageWaitTime;
77         this.messageRateTime = messageRateTime;
78
79         // lets add named stats
80
addStatistic("messageCount", messageCount);
81         addStatistic("pendingMessageCount", pendingMessageCount);
82         addStatistic("expiredMessageCount", expiredMessageCount);
83         addStatistic("messageWaitTime", messageWaitTime);
84         addStatistic("messageRateTime", messageRateTime);
85     }
86
87     public synchronized void reset() {
88         super.reset();
89         messageCount.reset();
90         messageRateTime.reset();
91         pendingMessageCount.reset();
92         expiredMessageCount.reset();
93         messageWaitTime.reset();
94     }
95
96     public CountStatisticImpl getMessageCount() {
97         return messageCount;
98     }
99
100     public CountStatisticImpl getPendingMessageCount() {
101         return pendingMessageCount;
102     }
103
104     public CountStatisticImpl getExpiredMessageCount() {
105         return expiredMessageCount;
106     }
107
108     public TimeStatisticImpl getMessageRateTime() {
109         return messageRateTime;
110     }
111
112     public TimeStatisticImpl getMessageWaitTime() {
113         return messageWaitTime;
114     }
115
116     public String JavaDoc toString() {
117         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
118         buffer.append(messageCount);
119         buffer.append(" ");
120         buffer.append(messageRateTime);
121         buffer.append(" ");
122         buffer.append(pendingMessageCount);
123         buffer.append(" ");
124         buffer.append(expiredMessageCount);
125         buffer.append(" ");
126         buffer.append(messageWaitTime);
127         return buffer.toString();
128     }
129
130     public void onMessage() {
131         if (enabled) {
132             long start = messageCount.getLastSampleTime();
133             messageCount.increment();
134             long end = messageCount.getLastSampleTime();
135             messageRateTime.addTime(end - start);
136         }
137     }
138
139     public void dump(IndentPrinter out) {
140         out.printIndent();
141         out.println(messageCount);
142         out.printIndent();
143         out.println(messageRateTime);
144         out.printIndent();
145         out.println(pendingMessageCount);
146         out.printIndent();
147         out.println(messageRateTime);
148         out.printIndent();
149         out.println(expiredMessageCount);
150         out.printIndent();
151         out.println(messageWaitTime);
152     }
153
154     // Implementation methods
155
//-------------------------------------------------------------------------
156
protected void setParent(CountStatisticImpl child, CountStatisticImpl parent) {
157         if (child instanceof CountStatisticImpl && parent instanceof CountStatisticImpl) {
158             CountStatisticImpl c = (CountStatisticImpl) child;
159             c.setParent((CountStatisticImpl) parent);
160         }
161         else {
162             log.warn("Cannot associate endpoint counters with session level counters as they are not both CountStatisticImpl clases. Endpoint: " + child + " session: " + parent);
163         }
164     }
165
166     protected void setParent(TimeStatisticImpl child, TimeStatisticImpl parent) {
167         if (child instanceof TimeStatisticImpl && parent instanceof TimeStatisticImpl) {
168             TimeStatisticImpl c = (TimeStatisticImpl) child;
169             c.setParent((TimeStatisticImpl) parent);
170         }
171         else {
172             log.warn("Cannot associate endpoint counters with session level counters as they are not both TimeStatisticImpl clases. Endpoint: " + child + " session: " + parent);
173         }
174     }
175 }
176
Popular Tags