001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.management;
018
019import java.util.List;
020
021import org.apache.activemq.ActiveMQMessageConsumer;
022import org.apache.activemq.ActiveMQMessageProducer;
023import org.apache.activemq.util.IndentPrinter;
024
025/**
026 * Statistics for a JMS session
027 * 
028 * 
029 */
030public class JMSSessionStatsImpl extends StatsImpl {
031    private List producers;
032    private List consumers;
033    private CountStatisticImpl messageCount;
034    private CountStatisticImpl pendingMessageCount;
035    private CountStatisticImpl expiredMessageCount;
036    private TimeStatisticImpl messageWaitTime;
037    private CountStatisticImpl durableSubscriptionCount;
038
039    private TimeStatisticImpl messageRateTime;
040
041    public JMSSessionStatsImpl(List producers, List consumers) {
042        this.producers = producers;
043        this.consumers = consumers;
044        this.messageCount = new CountStatisticImpl("messageCount", "Number of messages exchanged");
045        this.pendingMessageCount = new CountStatisticImpl("pendingMessageCount", "Number of pending messages");
046        this.expiredMessageCount = new CountStatisticImpl("expiredMessageCount", "Number of expired messages");
047        this.messageWaitTime = new TimeStatisticImpl("messageWaitTime",
048                                                     "Time spent by a message before being delivered");
049        this.durableSubscriptionCount = new CountStatisticImpl("durableSubscriptionCount",
050                                                               "The number of durable subscriptions");
051        this.messageWaitTime = new TimeStatisticImpl("messageWaitTime",
052                                                     "Time spent by a message before being delivered");
053        this.messageRateTime = new TimeStatisticImpl("messageRateTime",
054                                                     "Time taken to process a message (thoughtput rate)");
055
056        // lets add named stats
057        addStatistic("messageCount", messageCount);
058        addStatistic("pendingMessageCount", pendingMessageCount);
059        addStatistic("expiredMessageCount", expiredMessageCount);
060        addStatistic("messageWaitTime", messageWaitTime);
061        addStatistic("durableSubscriptionCount", durableSubscriptionCount);
062        addStatistic("messageRateTime", messageRateTime);
063    }
064
065    public JMSProducerStatsImpl[] getProducers() {
066        // lets make a snapshot before we process them
067        Object[] producerArray = producers.toArray();
068        int size = producerArray.length;
069        JMSProducerStatsImpl[] answer = new JMSProducerStatsImpl[size];
070        for (int i = 0; i < size; i++) {
071            ActiveMQMessageProducer producer = (ActiveMQMessageProducer)producerArray[i];
072            answer[i] = producer.getProducerStats();
073        }
074        return answer;
075    }
076
077    public JMSConsumerStatsImpl[] getConsumers() {
078        // lets make a snapshot before we process them
079        Object[] consumerArray = consumers.toArray();
080        int size = consumerArray.length;
081        JMSConsumerStatsImpl[] answer = new JMSConsumerStatsImpl[size];
082        for (int i = 0; i < size; i++) {
083            ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer)consumerArray[i];
084            answer[i] = consumer.getConsumerStats();
085        }
086        return answer;
087    }
088
089    public void reset() {
090        super.reset();
091        JMSConsumerStatsImpl[] cstats = getConsumers();
092        int size = cstats.length;
093        for (int i = 0; i < size; i++) {
094            cstats[i].reset();
095        }
096        JMSProducerStatsImpl[] pstats = getProducers();
097        size = pstats.length;
098        for (int i = 0; i < size; i++) {
099            pstats[i].reset();
100        }
101    }
102
103    /**
104     * @param enabled the enabled to set
105     */
106    public void setEnabled(boolean enabled) {
107        super.setEnabled(enabled);
108        JMSConsumerStatsImpl[] cstats = getConsumers();
109        int size = cstats.length;
110        for (int i = 0; i < size; i++) {
111            cstats[i].setEnabled(enabled);
112        }
113        JMSProducerStatsImpl[] pstats = getProducers();
114        size = pstats.length;
115        for (int i = 0; i < size; i++) {
116            pstats[i].setEnabled(enabled);
117        }
118
119    }
120
121    public CountStatisticImpl getMessageCount() {
122        return messageCount;
123    }
124
125    public CountStatisticImpl getPendingMessageCount() {
126        return pendingMessageCount;
127    }
128
129    public CountStatisticImpl getExpiredMessageCount() {
130        return expiredMessageCount;
131    }
132
133    public TimeStatisticImpl getMessageWaitTime() {
134        return messageWaitTime;
135    }
136
137    public CountStatisticImpl getDurableSubscriptionCount() {
138        return durableSubscriptionCount;
139    }
140
141    public TimeStatisticImpl getMessageRateTime() {
142        return messageRateTime;
143    }
144
145    public String toString() {
146        StringBuffer buffer = new StringBuffer(" ");
147        buffer.append(messageCount);
148        buffer.append(" ");
149        buffer.append(messageRateTime);
150        buffer.append(" ");
151        buffer.append(pendingMessageCount);
152        buffer.append(" ");
153        buffer.append(expiredMessageCount);
154        buffer.append(" ");
155        buffer.append(messageWaitTime);
156        buffer.append(" ");
157        buffer.append(durableSubscriptionCount);
158
159        buffer.append(" producers{ ");
160        JMSProducerStatsImpl[] producerArray = getProducers();
161        for (int i = 0; i < producerArray.length; i++) {
162            if (i > 0) {
163                buffer.append(", ");
164            }
165            buffer.append(Integer.toString(i));
166            buffer.append(" = ");
167            buffer.append(producerArray[i]);
168        }
169        buffer.append(" } consumers{ ");
170        JMSConsumerStatsImpl[] consumerArray = getConsumers();
171        for (int i = 0; i < consumerArray.length; i++) {
172            if (i > 0) {
173                buffer.append(", ");
174            }
175            buffer.append(Integer.toString(i));
176            buffer.append(" = ");
177            buffer.append(consumerArray[i]);
178        }
179        buffer.append(" }");
180        return buffer.toString();
181    }
182
183    public void dump(IndentPrinter out) {
184        out.printIndent();
185        out.println(messageCount);
186        out.printIndent();
187        out.println(messageRateTime);
188        out.printIndent();
189        out.println(pendingMessageCount);
190        out.printIndent();
191        out.println(expiredMessageCount);
192        out.printIndent();
193        out.println(messageWaitTime);
194        out.printIndent();
195        out.println(durableSubscriptionCount);
196        out.println();
197
198        out.printIndent();
199        out.println("producers {");
200        out.incrementIndent();
201        JMSProducerStatsImpl[] producerArray = getProducers();
202        for (int i = 0; i < producerArray.length; i++) {
203            JMSProducerStatsImpl producer = (JMSProducerStatsImpl)producerArray[i];
204            producer.dump(out);
205        }
206        out.decrementIndent();
207        out.printIndent();
208        out.println("}");
209
210        out.printIndent();
211        out.println("consumers {");
212        out.incrementIndent();
213        JMSConsumerStatsImpl[] consumerArray = getConsumers();
214        for (int i = 0; i < consumerArray.length; i++) {
215            JMSConsumerStatsImpl consumer = (JMSConsumerStatsImpl)consumerArray[i];
216            consumer.dump(out);
217        }
218        out.decrementIndent();
219        out.printIndent();
220        out.println("}");
221    }
222
223    public void onCreateDurableSubscriber() {
224        durableSubscriptionCount.increment();
225    }
226
227    public void onRemoveDurableSubscriber() {
228        durableSubscriptionCount.decrement();
229    }
230}