View Javadoc

    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.codehaus.activemq.ActiveMQMessageConsumer;
    21  import org.codehaus.activemq.ActiveMQMessageProducer;
    22  import org.codehaus.activemq.util.IndentPrinter;
    23
    24  import javax.management.j2ee.statistics.CountStatistic;
    25  import javax.management.j2ee.statistics.JMSConsumerStats;
    26  import javax.management.j2ee.statistics.JMSProducerStats;
    27  import javax.management.j2ee.statistics.JMSSessionStats;
    28  import javax.management.j2ee.statistics.TimeStatistic;
    29  import java.util.List;
    30
    31  /***
    32   * Statistics for a JMS session
    33   *
    34   * @version $Revision: 1.6 $
    35   */
    36  public class JMSSessionStatsImpl extends StatsImpl implements JMSSessionStats {
    37      private List producers;
    38      private List consumers;
    39      private CountStatistic messageCount;
    40      private CountStatistic pendingMessageCount;
    41      private CountStatistic expiredMessageCount;
    42      private TimeStatistic messageWaitTime;
    43      private CountStatisticImpl durableSubscriptionCount;
    44
    45      private TimeStatisticImpl messageRateTime;
    46
    47      public JMSSessionStatsImpl(List producers, List consumers) {
    48          this.producers = producers;
    49          this.consumers = consumers;
    50          this.messageCount = new CountStatisticImpl("messageCount", "Number of messages exchanged");
    51          this.pendingMessageCount = new CountStatisticImpl("pendingMessageCount", "Number of pending messages");
    52          this.expiredMessageCount = new CountStatisticImpl("expiredMessageCount", "Number of expired messages");
    53          this.messageWaitTime = new TimeStatisticImpl("messageWaitTime", "Time spent by a message before being delivered");
    54          this.durableSubscriptionCount = new CountStatisticImpl("durableSubscriptionCount", "The number of durable subscriptions");
    55          this.messageWaitTime = new TimeStatisticImpl("messageWaitTime", "Time spent by a message before being delivered");
    56          this.messageRateTime = new TimeStatisticImpl("messageRateTime", "Time taken to process a message (thoughtput rate)");
    57
    58          // lets add named stats
    59          addStatistic("messageCount", messageCount);
    60          addStatistic("pendingMessageCount", pendingMessageCount);
    61          addStatistic("expiredMessageCount", expiredMessageCount);
    62          addStatistic("messageWaitTime", messageWaitTime);
    63          addStatistic("durableSubscriptionCount", durableSubscriptionCount);
    64          addStatistic("messageRateTime", messageRateTime);
    65      }
    66
    67      public JMSProducerStats[] getProducers() {
    68          // lets make a snapshot before we process them
    69          Object[] producerArray = producers.toArray();
    70          int size = producerArray.length;
    71          JMSProducerStats[] answer = new JMSProducerStats[size];
    72          for (int i = 0; i < size; i++) {
    73              ActiveMQMessageProducer producer = (ActiveMQMessageProducer) producerArray[i];
    74              answer[i] = producer.getProducerStats();
    75          }
    76          return answer;
    77      }
    78
    79      public JMSConsumerStats[] getConsumers() {
    80          // lets make a snapshot before we process them
    81          Object[] consumerArray = consumers.toArray();
    82          int size = consumerArray.length;
    83          JMSConsumerStats[] answer = new JMSConsumerStats[size];
    84          for (int i = 0; i < size; i++) {
    85              ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) consumerArray[i];
    86              answer[i] = consumer.getConsumerStats();
    87          }
    88          return answer;
    89      }
    90
    91      public CountStatistic getMessageCount() {
    92          return messageCount;
    93      }
    94
    95      public CountStatistic getPendingMessageCount() {
    96          return pendingMessageCount;
    97      }
    98
    99      public CountStatistic getExpiredMessageCount() {
    100         return expiredMessageCount;
    101     }
    102
    103     public TimeStatistic getMessageWaitTime() {
    104         return messageWaitTime;
    105     }
    106
    107     public CountStatistic getDurableSubscriptionCount() {
    108         return durableSubscriptionCount;
    109     }
    110
    111     public TimeStatisticImpl getMessageRateTime() {
    112         return messageRateTime;
    113     }
    114
    115     public String toString() {
    116         StringBuffer buffer = new StringBuffer(" ");
    117         buffer.append(messageCount);
    118         buffer.append(" ");
    119         buffer.append(messageRateTime);
    120         buffer.append(" ");
    121         buffer.append(pendingMessageCount);
    122         buffer.append(" ");
    123         buffer.append(expiredMessageCount);
    124         buffer.append(" ");
    125         buffer.append(messageWaitTime);
    126         buffer.append(" ");
    127         buffer.append(durableSubscriptionCount);
    128
    129         buffer.append(" producers{ ");
    130         JMSProducerStats[] producerArray = getProducers();
    131         for (int i = 0; i < producerArray.length; i++) {
    132             if (i > 0) {
    133                 buffer.append(", ");
    134             }
    135             buffer.append(Integer.toString(i));
    136             buffer.append(" = ");
    137             buffer.append(producerArray[i]);
    138         }
    139         buffer.append(" } consumers{ ");
    140         JMSConsumerStats[] consumerArray = getConsumers();
    141         for (int i = 0; i < consumerArray.length; i++) {
    142             if (i > 0) {
    143                 buffer.append(", ");
    144             }
    145             buffer.append(Integer.toString(i));
    146             buffer.append(" = ");
    147             buffer.append(consumerArray[i]);
    148         }
    149         buffer.append(" }");
    150         return buffer.toString();
    151     }
    152
    153     public void dump(IndentPrinter out) {
    154         out.printIndent();
    155         out.println(messageCount);
    156         out.printIndent();
    157         out.println(messageRateTime);
    158         out.printIndent();
    159         out.println(pendingMessageCount);
    160         out.printIndent();
    161         out.println(expiredMessageCount);
    162         out.printIndent();
    163         out.println(messageWaitTime);
    164         out.printIndent();
    165         out.println(durableSubscriptionCount);
    166         out.println();
    167
    168         out.printIndent();
    169         out.println("producers {");
    170         out.incrementIndent();
    171         JMSProducerStats[] producerArray = getProducers();
    172         for (int i = 0; i < producerArray.length; i++) {
    173             JMSProducerStatsImpl producer = (JMSProducerStatsImpl) producerArray[i];
    174             producer.dump(out);
    175         }
    176         out.decrementIndent();
    177         out.printIndent();
    178         out.println("}");
    179
    180         out.printIndent();
    181         out.println("consumers {");
    182         out.incrementIndent();
    183         JMSConsumerStats[] consumerArray = getConsumers();
    184         for (int i = 0; i < consumerArray.length; i++) {
    185             JMSConsumerStatsImpl consumer = (JMSConsumerStatsImpl) consumerArray[i];
    186             consumer.dump(out);
    187         }
    188         out.decrementIndent();
    189         out.printIndent();
    190         out.println("}");
    191     }
    192
    193     public void onCreateDurableSubscriber() {
    194         durableSubscriptionCount.increment();
    195     }
    196
    197     public void onRemoveDurableSubscriber() {
    198         durableSubscriptionCount.decrement();
    199     }
    200 }