1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.ning.metrics.goodwill.access;
18
19 import com.mogwee.executors.Executors;
20
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeUnit;
27
28 public class CachingGoodwillAccessor extends Accessor
29 {
30 private static final int DEFAULT_CACHE_TIMEOUT_IN_SECONDS = 90;
31 private final Object cacheMonitor = new Object();
32
33 private final ConcurrentHashMap<String, GoodwillSchema> knownSchemata = new ConcurrentHashMap<String, GoodwillSchema>();
34 private final GoodwillAccessor delegate;
35
36 public CachingGoodwillAccessor(final String host, final int port)
37 {
38 this(host, port, DEFAULT_CACHE_TIMEOUT_IN_SECONDS);
39 }
40
41 public CachingGoodwillAccessor(final String host, final int port, final int cacheTimeoutInSeconds)
42 {
43 super(host, port);
44
45 delegate = new GoodwillAccessor(host, port);
46
47 Executors.newScheduledThreadPool(1, "GoodwillCacheRefresher").scheduleWithFixedDelay(new Runnable()
48 {
49 @Override
50 public void run()
51 {
52 refreshSchemataCache();
53 }
54 }, 0, cacheTimeoutInSeconds, TimeUnit.SECONDS);
55 }
56
57
58
59
60
61
62 public void refreshSchemataCache()
63 {
64 try {
65 final List<GoodwillSchema> schemata = delegate.getSchemata().get();
66
67
68 if (schemata == null) {
69 return;
70 }
71
72 final Map<String,GoodwillSchema> newSchemataCache = new ConcurrentHashMap<String, GoodwillSchema>();
73 for (final GoodwillSchema schema : schemata) {
74 newSchemataCache.put(schema.getName(), schema);
75 }
76
77 synchronized (cacheMonitor) {
78 knownSchemata.clear();
79 knownSchemata.putAll(newSchemataCache);
80 }
81 }
82 catch (InterruptedException e) {
83 log.warn("Interrupted while refreshing the cache");
84 Thread.currentThread().interrupt();
85 }
86 catch (ExecutionException e) {
87 log.warn("Unable to refresh schemata cache: {}", e.getLocalizedMessage());
88 }
89 }
90
91
92
93
94
95
96
97
98 public GoodwillSchema getSchema(final String schemaName)
99 {
100 GoodwillSchema schema = knownSchemata.get(schemaName);
101 if (schema == null) {
102 refreshSchemataCache();
103 schema = knownSchemata.get(schemaName);
104 }
105
106 return schema;
107 }
108
109 public List<GoodwillSchema> getSchemata()
110 {
111 return Collections.list(knownSchemata.elements());
112 }
113
114
115
116
117 public synchronized void close()
118 {
119 delegate.close();
120 }
121 }