KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.List JavaDoc;
21 import javax.management.j2ee.statistics.*;
22
23 import org.apache.activemq.ActiveMQMessageConsumer;
24 import org.apache.activemq.ActiveMQMessageProducer;
25 import org.apache.activemq.util.IndentPrinter;
26
27 /**
28  * Statistics for a JMS session
29  *
30  * @version $Revision: 1.2 $
31  */

32 public class JMSSessionStatsImpl extends StatsImpl {
33     private List JavaDoc producers;
34     private List JavaDoc consumers;
35     private CountStatisticImpl messageCount;
36     private CountStatisticImpl pendingMessageCount;
37     private CountStatisticImpl expiredMessageCount;
38     private TimeStatisticImpl messageWaitTime;
39     private CountStatisticImpl durableSubscriptionCount;
40
41     private TimeStatisticImpl messageRateTime;
42
43     public JMSSessionStatsImpl(List JavaDoc producers, List JavaDoc consumers) {
44         this.producers = producers;
45         this.consumers = consumers;
46         this.messageCount = new CountStatisticImpl("messageCount", "Number of messages exchanged");
47         this.pendingMessageCount = new CountStatisticImpl("pendingMessageCount", "Number of pending messages");
48         this.expiredMessageCount = new CountStatisticImpl("expiredMessageCount", "Number of expired messages");
49         this.messageWaitTime = new TimeStatisticImpl("messageWaitTime", "Time spent by a message before being delivered");
50         this.durableSubscriptionCount = new CountStatisticImpl("durableSubscriptionCount", "The number of durable subscriptions");
51         this.messageWaitTime = new TimeStatisticImpl("messageWaitTime", "Time spent by a message before being delivered");
52         this.messageRateTime = new TimeStatisticImpl("messageRateTime", "Time taken to process a message (thoughtput rate)");
53
54         // lets add named stats
55
addStatistic("messageCount", messageCount);
56         addStatistic("pendingMessageCount", pendingMessageCount);
57         addStatistic("expiredMessageCount", expiredMessageCount);
58         addStatistic("messageWaitTime", messageWaitTime);
59         addStatistic("durableSubscriptionCount", durableSubscriptionCount);
60         addStatistic("messageRateTime", messageRateTime);
61     }
62
63     public JMSProducerStatsImpl[] getProducers() {
64         // lets make a snapshot before we process them
65
Object JavaDoc[] producerArray = producers.toArray();
66         int size = producerArray.length;
67         JMSProducerStatsImpl[] answer = new JMSProducerStatsImpl[size];
68         for (int i = 0; i < size; i++) {
69             ActiveMQMessageProducer producer = (ActiveMQMessageProducer) producerArray[i];
70             answer[i] = producer.getProducerStats();
71         }
72         return answer;
73     }
74
75     public JMSConsumerStatsImpl[] getConsumers() {
76         // lets make a snapshot before we process them
77
Object JavaDoc[] consumerArray = consumers.toArray();
78         int size = consumerArray.length;
79         JMSConsumerStatsImpl[] answer = new JMSConsumerStatsImpl[size];
80         for (int i = 0; i < size; i++) {
81             ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) consumerArray[i];
82             answer[i] = consumer.getConsumerStats();
83         }
84         return answer;
85     }
86     
87     public void reset() {
88         super.reset();
89         JMSConsumerStatsImpl[] cstats = getConsumers();
90         for (int i = 0, size = cstats.length; i < size; i++) {
91             cstats[i].reset();
92         }
93         JMSProducerStatsImpl[] pstats = getProducers();
94         for (int i = 0, size = pstats.length; i < size; i++) {
95             pstats[i].reset();
96         }
97     }
98     
99     /**
100      * @param enabled the enabled to set
101      */

102     public void setEnabled(boolean enabled){
103         super.setEnabled(enabled);
104         JMSConsumerStatsImpl[] cstats = getConsumers();
105         for (int i = 0, size = cstats.length; i < size; i++) {
106             cstats[i].setEnabled(enabled);
107         }
108         JMSProducerStatsImpl[] pstats = getProducers();
109         for (int i = 0, size = pstats.length; i < size; i++) {
110             pstats[i].setEnabled(enabled);
111         }
112         
113     }
114
115     public CountStatisticImpl getMessageCount() {
116         return messageCount;
117     }
118
119     public CountStatisticImpl getPendingMessageCount() {
120         return pendingMessageCount;
121     }
122
123     public CountStatisticImpl getExpiredMessageCount() {
124         return expiredMessageCount;
125     }
126
127     public TimeStatisticImpl getMessageWaitTime() {
128         return messageWaitTime;
129     }
130
131     public CountStatisticImpl getDurableSubscriptionCount() {
132         return durableSubscriptionCount;
133     }
134
135     public TimeStatisticImpl getMessageRateTime() {
136         return messageRateTime;
137     }
138
139     public String JavaDoc toString() {
140         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(" ");
141         buffer.append(messageCount);
142         buffer.append(" ");
143         buffer.append(messageRateTime);
144         buffer.append(" ");
145         buffer.append(pendingMessageCount);
146         buffer.append(" ");
147         buffer.append(expiredMessageCount);
148         buffer.append(" ");
149         buffer.append(messageWaitTime);
150         buffer.append(" ");
151         buffer.append(durableSubscriptionCount);
152
153         buffer.append(" producers{ ");
154         JMSProducerStatsImpl[] producerArray = getProducers();
155         for (int i = 0; i < producerArray.length; i++) {
156             if (i > 0) {
157                 buffer.append(", ");
158             }
159             buffer.append(Integer.toString(i));
160             buffer.append(" = ");
161             buffer.append(producerArray[i]);
162         }
163         buffer.append(" } consumers{ ");
164         JMSConsumerStatsImpl[] consumerArray = getConsumers();
165         for (int i = 0; i < consumerArray.length; i++) {
166             if (i > 0) {
167                 buffer.append(", ");
168             }
169             buffer.append(Integer.toString(i));
170             buffer.append(" = ");
171             buffer.append(consumerArray[i]);
172         }
173         buffer.append(" }");
174         return buffer.toString();
175     }
176
177     public void dump(IndentPrinter out) {
178         out.printIndent();
179         out.println(messageCount);
180         out.printIndent();
181         out.println(messageRateTime);
182         out.printIndent();
183         out.println(pendingMessageCount);
184         out.printIndent();
185         out.println(expiredMessageCount);
186         out.printIndent();
187         out.println(messageWaitTime);
188         out.printIndent();
189         out.println(durableSubscriptionCount);
190         out.println();
191
192         out.printIndent();
193         out.println("producers {");
194         out.incrementIndent();
195         JMSProducerStatsImpl[] producerArray = getProducers();
196         for (int i = 0; i < producerArray.length; i++) {
197             JMSProducerStatsImpl producer = (JMSProducerStatsImpl) producerArray[i];
198             producer.dump(out);
199         }
200         out.decrementIndent();
201         out.printIndent();
202         out.println("}");
203
204         out.printIndent();
205         out.println("consumers {");
206         out.incrementIndent();
207         JMSConsumerStatsImpl[] consumerArray = getConsumers();
208         for (int i = 0; i < consumerArray.length; i++) {
209             JMSConsumerStatsImpl consumer = (JMSConsumerStatsImpl) consumerArray[i];
210             consumer.dump(out);
211         }
212         out.decrementIndent();
213         out.printIndent();
214         out.println("}");
215     }
216
217     public void onCreateDurableSubscriber() {
218         durableSubscriptionCount.increment();
219     }
220
221     public void onRemoveDurableSubscriber() {
222         durableSubscriptionCount.decrement();
223     }
224 }
225
Popular Tags