BasicCapacityMonitor xref

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
    19  package org.codehaus.activemq.capacity;
    20  import java.util.Iterator;
    21  import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
    22
    23  /***
    24   * BasicCapacityMonitor implementation
    25   * @version $Revision: 1.2 $
    26   */
    27  public class BasicCapacityMonitor implements CapacityMonitor {
    28      private String name;
    29      private long valueLimit;
    30      private long currentValue = 0;
    31      private int currentCapacity = 100;
    32      private int roundedCapacity = 100;
    33      private int roundingFactor = 10;
    34      private CopyOnWriteArrayList listeners = new CopyOnWriteArrayList();
    35
    36      /***
    37       * Construct a CapacityMonitor
    38       *
    39       * @param name
    40       * @param valueLimit
    41       */
    42      public BasicCapacityMonitor(String name, long valueLimit) {
    43          this.name = name;
    44          this.valueLimit = valueLimit;
    45      }
    46
    47      /***
    48       * Get the name of the CapacityMonitor
    49       *
    50       * @return
    51       */
    52      public String getName() {
    53          return name;
    54      }
    55
    56      /***
    57       * @param newName
    58       */
    59      public void setName(String newName){
    60          name = newName;
    61      }
    62
    63      /***
    64       * Get the rounding factor - default is 10
    65       * @return the rounding factor
    66       */
    67      public int getRoundingFactor(){
    68          return roundingFactor;
    69      }
    70
    71      /***
    72       * Set the rounding factor (between 1-100)
    73       * @param newRoundingFactor
    74       */
    75      public void setRoundingFactor(int newRoundingFactor){
    76          if (newRoundingFactor < 1 || newRoundingFactor > 100){
    77              throw new IllegalArgumentException("invalid roundingFactor: " + newRoundingFactor);
    78          }
    79          roundingFactor = newRoundingFactor;
    80      }
    81
    82      /***
    83       * Add a CapacityMonitorEventListener
    84       *
    85       * @param l
    86       */
    87      public void addCapacityEventListener(CapacityMonitorEventListener l) {
    88          listeners.add(l);
    89      }
    90
    91      /***
    92       * Remove a CapacityMonitorEventListener
    93       *
    94       * @param l
    95       */
    96      public void removeCapacityEventListener(CapacityMonitorEventListener l) {
    97          listeners.remove(l);
    98      }
    99
    100     /***
    101      * Get the current capscity of the service as a percentage
    102      *
    103      * @return
    104      */
    105     public int getCurrentCapacity() {
    106         return currentCapacity;
    107     }
    108
    109     /***
    110      * Calculates the capacity rounded down to the rounding factor
    111      * @return
    112      */
    113     public int getRoundedCapacity(){
    114         return roundedCapacity;
    115     }
    116
    117     /***
    118      * Get the current value of the CapacityMonitor
    119      *
    120      * @return
    121      */
    122     public long getCurrentValue() {
    123         return currentValue;
    124     }
    125
    126     /***
    127      * set the current value of the capacity
    128      *
    129      * @param newCurrentValue
    130      */
    131     public void setCurrentValue(long newCurrentValue) {
    132         currentValue = newCurrentValue;
    133         int newCapacity = calculateCapacity();
    134         int newRoundedCapacity = newCapacity > 0 ?  (newCapacity/roundingFactor)*roundingFactor : 0;
    135         updateCapacityChanged(newRoundedCapacity);
    136         currentCapacity = newCapacity;
    137         roundedCapacity = newRoundedCapacity;
    138     }
    139
    140     /***
    141      * @return The upper limit of the value of the CapacityMonitor
    142      */
    143     public long getValueLimit() {
    144         return valueLimit;
    145     }
    146
    147     /***
    148      * set a new value limit for the CapacityMonitor
    149      *
    150      * @param newValueLimit
    151      */
    152     public void setValueLimit(long newValueLimit) {
    153         valueLimit = newValueLimit;
    154         //this could have changed the capacity
    155         setCurrentValue(currentValue);
    156
    157     }
    158
    159     private void updateCapacityChanged(int newRoundedCapacity) {
    160         if (listeners.size() > 0 && newRoundedCapacity != roundedCapacity) {
    161             CapacityMonitorEvent event = new CapacityMonitorEvent(name, newRoundedCapacity);
    162             for (Iterator i = listeners.iterator();i.hasNext();) {
    163                 CapacityMonitorEventListener listener = (CapacityMonitorEventListener) i.next();
    164                 listener.capacityChanged(event);
    165             }
    166         }
    167
    168     }
    169
    170     private int calculateCapacity() {
    171         int result = 100;
    172         if (currentValue != 0){
    173             result = (int) (100-(currentValue * 100)/valueLimit);
    174         }
    175         return result;
    176     }
    177 }