View Javadoc

1   // BSD License (http://www.galagosearch.org/license)
2   
3   package org.galagosearch.tupleflow.execution;
4   
5   import java.util.HashMap;
6   import org.galagosearch.tupleflow.Counter;
7   
8   /***
9    *
10   * @author trevor
11   */
12  public class NetworkedCounterManager implements Runnable {
13      HashMap<String, NetworkedCounter> counters = new HashMap<String, NetworkedCounter>();
14      boolean stop = false;
15      int sleepInterval = 1000;
16      Thread thread;
17  
18      public synchronized Counter newCounter(
19              String counterName, String stageName, String instance, String url) {
20          String key = String.format("%s-%s-%s", counterName, stageName, instance);
21          if (counters.containsKey(key))
22              return counters.get(key);
23          NetworkedCounter counter = new NetworkedCounter(counterName, stageName, instance, url);
24          counters.put(key, counter);
25          return counter;
26      }
27  
28      public void start() {
29          thread = new Thread(this);
30          thread.start();
31      }
32  
33      public synchronized void stop() {
34          stop = true;
35          if (thread != null)
36              thread.interrupt();
37      }
38  
39      public void run() {
40          while (true) {
41              synchronized(this) {
42                  for (NetworkedCounter counter : counters.values()) {
43                      counter.flush();
44                  }
45  
46                  if (stop) break;
47              }
48  
49              try {
50                  Thread.sleep(sleepInterval);
51              } catch (InterruptedException ex) {
52                  // it's probably time to flush and quit now
53              }
54          }
55      }
56  }