use of org.apache.curator.framework.recipes.cache.ChildData in project xian by happyyangyuan.
the class GroupMember method getCurrentMembers.
/**
* Return the current view of membership. The keys are the IDs
* of the members. The values are each member's payload
*
* @return membership
*/
public Map<String, byte[]> getCurrentMembers() {
ImmutableMap.Builder<String, byte[]> builder = ImmutableMap.builder();
boolean thisIdAdded = false;
for (ChildData data : cache.getCurrentData()) {
String id = idFromPath(data.getPath());
thisIdAdded = thisIdAdded || id.equals(thisId);
builder.put(id, data.getData());
}
if (!thisIdAdded) {
// this instance is always a member
builder.put(thisId, pen.getData());
}
return builder.build();
}
use of org.apache.curator.framework.recipes.cache.ChildData in project archaius by Netflix.
the class ZooKeeperConfigurationSource method start.
/**
* Adds a listener to the pathChildrenCache, initializes the cache, then starts the cache-management background thread
*
* @throws Exception
*/
public void start() throws Exception {
// create the watcher for future configuration updatess
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
public void childEvent(CuratorFramework aClient, PathChildrenCacheEvent event) throws Exception {
Type eventType = event.getType();
ChildData data = event.getData();
String path = null;
if (data != null) {
path = data.getPath();
// scrub configRootPath out of the key name
String key = removeRootPath(path);
byte[] value = data.getData();
String stringValue = new String(value, charset);
logger.debug("received update to pathName [{}], eventType [{}]", path, eventType);
logger.debug("key [{}], and value [{}]", key, stringValue);
// fire event to all listeners
Map<String, Object> added = null;
Map<String, Object> changed = null;
Map<String, Object> deleted = null;
if (eventType == Type.CHILD_ADDED) {
added = new HashMap<String, Object>(1);
added.put(key, stringValue);
} else if (eventType == Type.CHILD_UPDATED) {
changed = new HashMap<String, Object>(1);
changed.put(key, stringValue);
} else if (eventType == Type.CHILD_REMOVED) {
deleted = new HashMap<String, Object>(1);
deleted.put(key, stringValue);
}
WatchedUpdateResult result = WatchedUpdateResult.createIncremental(added, changed, deleted);
fireEvent(result);
}
}
});
// passing true to trigger an initial rebuild upon starting. (blocking call)
pathChildrenCache.start(true);
}
use of org.apache.curator.framework.recipes.cache.ChildData in project sharding-jdbc by shardingjdbc.
the class ZookeeperRegistryCenter method watch.
@Override
public void watch(final String key, final EventListener eventListener) {
final String path = key + "/";
if (!caches.containsKey(path)) {
addCacheData(key);
}
TreeCache cache = caches.get(path);
cache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(final CuratorFramework client, final TreeCacheEvent event) throws Exception {
ChildData data = event.getData();
if (null == data || null == data.getPath()) {
return;
}
eventListener.onChange(new DataChangedEvent(getEventType(event), data.getPath(), null == data.getData() ? null : new String(data.getData(), "UTF-8")));
}
private DataChangedEvent.Type getEventType(final TreeCacheEvent event) {
switch(event.getType()) {
case NODE_UPDATED:
return DataChangedEvent.Type.UPDATED;
case NODE_REMOVED:
return DataChangedEvent.Type.DELETED;
default:
return DataChangedEvent.Type.IGNORED;
}
}
});
}
use of org.apache.curator.framework.recipes.cache.ChildData in project drill by axbaretto.
the class TestZookeeperClient method testEntriesReturnsRelativePaths.
@Test
public void testEntriesReturnsRelativePaths() throws Exception {
final ChildData child = Mockito.mock(ChildData.class);
Mockito.when(child.getPath()).thenReturn(abspath);
Mockito.when(child.getData()).thenReturn(data);
final List<ChildData> children = Lists.newArrayList(child);
Mockito.when(client.getCache().getCurrentData()).thenReturn(children);
final Iterator<Map.Entry<String, byte[]>> entries = client.entries();
// returned entry must contain the given relative path
final Map.Entry<String, byte[]> expected = new ImmutableEntry<>(path, data);
assertEquals("entries do not match", expected, entries.next());
}
use of org.apache.curator.framework.recipes.cache.ChildData in project drill by axbaretto.
the class TestZookeeperClient method testGetWithEventualConsistencyHitsCache.
@Test
public void testGetWithEventualConsistencyHitsCache() {
Mockito.when(client.getCache().getCurrentData(abspath)).thenReturn(null);
assertEquals("get should return null", null, client.get(path));
Mockito.when(client.getCache().getCurrentData(abspath)).thenReturn(new ChildData(abspath, null, data));
assertEquals("get should return data", data, client.get(path, false));
}
Aggregations