View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with 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,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.integration.jmx;
    21
    22  import java.net.SocketAddress;
    23  import java.util.Iterator;
    24
    25  import org.apache.mina.common.IoService;
    26  import org.apache.mina.common.IoSession;
    27  import org.apache.mina.management.StatCollector;
    28
    29  /**
    30   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    31   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
    32   */
    33  public class IoServiceManager implements IoServiceManagerMBean {
    34      private IoService service;
    35
    36      private StatCollector collector = null;
    37
    38      public IoServiceManager(IoService service) {
    39          this.service = service;
    40      }
    41
    42      public int getManagedSessionCount() {
    43
    44          int count = 0;
    45          for (Iterator iter = service.getManagedServiceAddresses().iterator(); iter
    46                  .hasNext();) {
    47              SocketAddress element = (SocketAddress) iter.next();
    48
    49              count += service.getManagedSessions(element).size();
    50          }
    51          return count;
    52      }
    53
    54      public void startCollectingStats(int millisecondsPolling) {
    55          if (collector != null && collector.isRunning()) {
    56              throw new RuntimeException("Already collecting stats");
    57          }
    58
    59          collector = new StatCollector(service, millisecondsPolling);
    60          collector.start();
    61
    62      }
    63
    64      public void stopCollectingStats() {
    65          if (collector != null && collector.isRunning())
    66              collector.stop();
    67
    68      }
    69
    70      public float getTotalByteReadThroughput() {
    71          return collector.getBytesReadThroughput();
    72      }
    73
    74      public float getTotalByteWrittenThroughput() {
    75          return collector.getBytesWrittenThroughput();
    76      }
    77
    78      public float getTotalMessageReadThroughput() {
    79          return collector.getMsgReadThroughput();
    80      }
    81
    82      public float getTotalMessageWrittenThroughput() {
    83          return collector.getMsgWrittenThroughput();
    84      }
    85
    86      public float getAverageByteReadThroughput() {
    87          return collector.getBytesReadThroughput() / collector.getSessionCount();
    88      }
    89
    90      public float getAverageByteWrittenThroughput() {
    91          return collector.getBytesWrittenThroughput()
    92                  / collector.getSessionCount();
    93      }
    94
    95      public float getAverageMessageReadThroughput() {
    96          return collector.getMsgReadThroughput() / collector.getSessionCount();
    97      }
    98
    99      public float getAverageMessageWrittenThroughput() {
    100         return collector.getMsgWrittenThroughput()
    101                 / collector.getSessionCount();
    102     }
    103
    104     public void closeAllSessions() {
    105         for (Iterator iter = service.getManagedServiceAddresses().iterator(); iter
    106                 .hasNext();) {
    107             SocketAddress element = (SocketAddress) iter.next();
    108
    109             for (Iterator iter2 = service.getManagedSessions(element)
    110                     .iterator(); iter2.hasNext();) {
    111                 IoSession session = (IoSession) iter2.next();
    112                 session.close();
    113             }
    114         }
    115
    116     }
    117 }