Search in sources :

Example 1 with Type

use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type 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)

Aggregations

WatchedUpdateResult (com.netflix.config.WatchedUpdateResult)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)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