1 18 package org.apache.activemq.management; 19 20 import javax.management.j2ee.statistics.CountStatistic ; 21 22 import java.util.concurrent.atomic.AtomicLong ; 23 24 29 public class CountStatisticImpl extends StatisticImpl implements CountStatistic { 30 31 private final AtomicLong counter = new AtomicLong (0); 32 private CountStatisticImpl parent; 33 34 public CountStatisticImpl(CountStatisticImpl parent, String name, String description) { 35 this(name, description); 36 this.parent = parent; 37 } 38 39 public CountStatisticImpl(String name, String description) { 40 this(name, "count", description); 41 } 42 43 public CountStatisticImpl(String name, String unit, String description) { 44 super(name, unit, description); 45 } 46 47 public void reset() { 48 super.reset(); 49 counter.set(0); 50 } 51 52 public long getCount() { 53 return counter.get(); 54 } 55 56 public void setCount(long count) { 57 if(isEnabled()) { 58 counter.set(count); 59 } 60 } 61 62 public void add(long amount) { 63 if (isEnabled()) { 64 counter.addAndGet(amount); 65 updateSampleTime(); 66 if (parent != null) { 67 parent.add(amount); 68 } 69 } 70 } 71 72 public void increment() { 73 if (isEnabled()) { 74 counter.incrementAndGet(); 75 updateSampleTime(); 76 if (parent != null) { 77 parent.increment(); 78 } 79 } 80 } 81 82 public void subtract(long amount) { 83 if (isEnabled()) { 84 counter.addAndGet(-amount); 85 updateSampleTime(); 86 if (parent != null) { 87 parent.subtract(amount); 88 } 89 } 90 } 91 92 public void decrement() { 93 if (isEnabled()) { 94 counter.decrementAndGet(); 95 updateSampleTime(); 96 if (parent != null) { 97 parent.decrement(); 98 } 99 } 100 } 101 102 public CountStatisticImpl getParent() { 103 return parent; 104 } 105 106 public void setParent(CountStatisticImpl parent) { 107 this.parent = parent; 108 } 109 110 protected void appendFieldDescription(StringBuffer buffer) { 111 buffer.append(" count: "); 112 buffer.append(Long.toString(counter.get())); 113 super.appendFieldDescription(buffer); 114 } 115 116 119 public double getPeriod() { 120 double count = counter.get(); 121 if( count == 0 ) 122 return 0; 123 double time = (System.currentTimeMillis() - getStartTime()); 124 return (time/(count*1000.0)); 125 } 126 127 130 public double getFrequency() { 131 double count = counter.get(); 132 double time = (System.currentTimeMillis() - getStartTime()); 133 return (count*1000.0/time); 134 } 135 136 } 137 | Popular Tags |