Search in sources :

Example 1 with WatchedUpdateResult

use of com.netflix.config.WatchedUpdateResult 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);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Type(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) HashMap(java.util.HashMap) ChildData(org.apache.curator.framework.recipes.cache.ChildData) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) WatchedUpdateResult(com.netflix.config.WatchedUpdateResult)

Example 2 with WatchedUpdateResult

use of com.netflix.config.WatchedUpdateResult in project archaius by Netflix.

the class ZooKeeperConfigurationSourceTest method setZkProperty.

private static void setZkProperty(String key, String value) throws Exception {
    // update the underlying zk property and assert that the new value is picked up
    final CountDownLatch updateLatch = new CountDownLatch(1);
    zkConfigSource.addUpdateListener(new WatchedUpdateListener() {

        public void updateConfiguration(WatchedUpdateResult result) {
            updateLatch.countDown();
        }
    });
    zkConfigSource.setZkProperty(key, value);
    updateLatch.await();
}
Also used : WatchedUpdateListener(com.netflix.config.WatchedUpdateListener) CountDownLatch(java.util.concurrent.CountDownLatch) WatchedUpdateResult(com.netflix.config.WatchedUpdateResult)

Aggregations

WatchedUpdateResult (com.netflix.config.WatchedUpdateResult)2 WatchedUpdateListener (com.netflix.config.WatchedUpdateListener)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 ChildData (org.apache.curator.framework.recipes.cache.ChildData)1 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)1 Type (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type)1 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)1 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)1 NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)1