1 19 package org.apache.avalon.excalibur.component.example_im; 20 21 import org.apache.avalon.framework.activity.Startable; 22 import org.apache.avalon.framework.logger.AbstractLogEnabled; 23 import org.apache.excalibur.instrument.CounterInstrument; 24 import org.apache.excalibur.instrument.Instrument; 25 import org.apache.excalibur.instrument.Instrumentable; 26 import org.apache.excalibur.instrument.ValueInstrument; 27 28 38 public class DefaultExampleInstrumentable 39 extends AbstractLogEnabled 40 implements ExampleInstrumentable, Startable, Runnable , Instrumentable 41 { 42 public static final String INSTRUMENT_RANDOM_QUICK_NAME = "random-quick"; 43 public static final String INSTRUMENT_RANDOM_SLOW_NAME = "random-slow"; 44 public static final String INSTRUMENT_RANDOM_RANDOM_NAME = "random-random"; 45 public static final String INSTRUMENT_COUNTER_QUICK_NAME = "counter-quick"; 46 public static final String INSTRUMENT_COUNTER_SLOW_NAME = "counter-slow"; 47 public static final String INSTRUMENT_COUNTER_RANDOM_NAME = "counter-random"; 48 public static final String INSTRUMENT_DOACTION_NAME = "doaction-counter"; 49 50 51 private String m_instrumentableName; 52 53 54 private ValueInstrument m_randomQuickInstrument; 55 56 57 private ValueInstrument m_randomSlowInstrument; 58 59 60 private ValueInstrument m_randomRandomInstrument; 61 62 63 private CounterInstrument m_counterQuickInstrument; 64 65 66 private CounterInstrument m_counterSlowInstrument; 67 68 69 private CounterInstrument m_counterRandomInstrument; 70 71 72 private CounterInstrument m_doActionInstrument; 73 74 75 private Thread m_runner; 76 77 80 public DefaultExampleInstrumentable() 81 { 82 m_randomQuickInstrument = new ValueInstrument( INSTRUMENT_RANDOM_QUICK_NAME ); 84 m_randomSlowInstrument = new ValueInstrument( INSTRUMENT_RANDOM_SLOW_NAME ); 85 m_randomRandomInstrument = new ValueInstrument( INSTRUMENT_RANDOM_RANDOM_NAME ); 86 m_counterQuickInstrument = new CounterInstrument( INSTRUMENT_COUNTER_QUICK_NAME ); 87 m_counterSlowInstrument = new CounterInstrument( INSTRUMENT_COUNTER_SLOW_NAME ); 88 m_counterRandomInstrument = new CounterInstrument( INSTRUMENT_COUNTER_RANDOM_NAME ); 89 m_doActionInstrument = new CounterInstrument( INSTRUMENT_DOACTION_NAME ); 90 } 91 92 95 98 public void doAction() 99 { 100 getLogger().debug( "ExampleInstrumentable.doAction() called." ); 101 102 m_doActionInstrument.increment(); 104 } 105 106 109 112 public void start() 113 { 114 if( m_runner == null ) 115 { 116 m_runner = new Thread ( this, "ExampleInstrumentableRunner" ); 117 m_runner.start(); 118 } 119 } 120 121 124 public void stop() 125 { 126 if( m_runner != null ) 127 { 128 m_runner.interrupt(); 129 m_runner = null; 130 } 131 } 132 133 136 140 public void run() 141 { 142 int counter = 0; 143 while( m_runner != null ) 144 { 145 try 147 { 148 Thread.sleep( 100 ); 149 } 150 catch( InterruptedException e ) 151 { 152 if( m_runner == null ) 153 { 154 return; 155 } 156 } 157 158 m_randomQuickInstrument.setValue( (int)( Math.random() * 100 ) ); 160 m_counterQuickInstrument.increment(); 161 162 counter++; 164 if( counter >= 20 ) 165 { 166 m_randomSlowInstrument.setValue( (int)( Math.random() * 100 ) ); 167 m_counterSlowInstrument.increment(); 168 counter = 0; 169 } 170 171 if( 100 * Math.random() < 10 ) 173 { 174 m_randomRandomInstrument.setValue( (int)( Math.random() * 100 ) ); 175 m_counterRandomInstrument.increment(); 176 } 177 } 178 } 179 180 183 196 public void setInstrumentableName( String name ) 197 { 198 m_instrumentableName = name; 199 } 200 201 206 public String getInstrumentableName() 207 { 208 return m_instrumentableName; 209 } 210 211 222 public Instrument[] getInstruments() 223 { 224 return new Instrument[] 225 { 226 m_randomQuickInstrument, 227 m_randomSlowInstrument, 228 m_randomRandomInstrument, 229 m_counterQuickInstrument, 230 m_counterSlowInstrument, 231 m_counterRandomInstrument, 232 m_doActionInstrument 233 }; 234 } 235 236 245 public Instrumentable[] getChildInstrumentables() 246 { 247 return Instrumentable.EMPTY_INSTRUMENTABLE_ARRAY; 249 } 250 } 251 252 | Popular Tags |