Example usage for org.apache.zookeeper WatchedEvent getType

List of usage examples for org.apache.zookeeper WatchedEvent getType

Introduction

In this page you can find the example usage for org.apache.zookeeper WatchedEvent getType.

Prototype

public EventType getType() 

Source Link

Usage

From source file:com.fjn.helper.frameworkex.apache.zookeeper.dispatcher.Dispatcher.java

License:Apache License

void accept(WatchedEvent event) {
    boolean stateChanged = event.getPath() == null;
    boolean znodeChanged = event.getPath() != null;
    boolean dataChanged = event.getType() == EventType.NodeDataChanged
            || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated
            || event.getType() == EventType.NodeChildrenChanged;

    if (stateChanged) {
        if (event.getState() == KeeperState.Expired) {
            selector.addEvent(new Event(ServiceType.SESSION, new SourceWrapper(event)));
        }/*from   w  ww .  j  a  v a2  s.c o  m*/
    }

    if (znodeChanged) {

    }

    if (dataChanged) {

    }
}

From source file:com.flipkart.ranger.finder.ServiceRegistryUpdater.java

License:Apache License

public void start() throws Exception {
    CuratorFramework curatorFramework = serviceRegistry.getService().getCuratorFramework();
    curatorFramework.getChildren().usingWatcher(new CuratorWatcher() {
        @Override//w w  w . jav  a 2  s  . c om
        public void process(WatchedEvent event) throws Exception {
            switch (event.getType()) {

            case NodeChildrenChanged: {
                checkForUpdate();
                break;
            }
            case None:
            case NodeCreated:
            case NodeDeleted:
            case NodeDataChanged:
                break;
            }
        }
    }).forPath(PathBuilder.path(serviceRegistry.getService())); //Start watcher on service node
    serviceRegistry.nodes(checkForUpdateOnZookeeper());
    logger.info("Started polling zookeeper for changes");
}

From source file:com.github.liyp.zk.demo.ZkElector.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    logger.info("ZK path ,state , type: {}",
            event.getPath() + " " + event.getState() + " " + event.getType() + "    #" + zookeeper.hashCode());
    switch (event.getType()) {
    case NodeChildrenChanged:
    case NodeCreated:
    case NodeDataChanged:
        break;//from www  .j a  v a 2s. co  m
    case NodeDeleted:
        try {
            newLeaderElection();
        } catch (KeeperException | InterruptedException e) {
            logger.error("New leader election failed.", e);
        }
        break;
    case None:
        switch (event.getState()) {
        case Disconnected:
            logger.info("ZK path {} Disconnected.", event.getPath());
            break;
        case Expired:
            logger.info("ZK path {} Expired.", event.getPath());

            taskServer.shutdown();
            try {
                this.zookeeper = new ZooKeeper(zkAddress, zkTimeout, this);
                elected.set(true);
            } catch (IOException e) {
                logger.info("{}", this.hashCode(), e);
            }
            break;
        case SyncConnected:
            logger.info("ZK path {} SyncConnected.", event.getPath());
            try {
                if (elected.get()) {
                    leaderElection();
                    elected.set(false);
                }
            } catch (KeeperException | InterruptedException e) {
                logger.error("ZK path rebuild error.", e);
            } catch (Exception e) {
                logger.info("{}", this.hashCode(), e);
            }
            break;
        default:
            logger.info("ZK path {}, state {}", event.getPath(), event.getState());
            break;
        }
        break;
    }
}

From source file:com.github.zkclient.ZkClient.java

License:Apache License

public void process(WatchedEvent event) {
    LOG.debug("Received event: " + event);
    _zookeeperEventThread = Thread.currentThread();

    boolean stateChanged = event.getPath() == null;
    boolean znodeChanged = event.getPath() != null;
    boolean dataChanged = event.getType() == EventType.NodeDataChanged || //
            event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated || //
            event.getType() == EventType.NodeChildrenChanged;

    getEventLock().lock();//w  w  w. j  av  a 2s.co  m
    try {

        // We might have to install child change event listener if a new node was created
        if (getShutdownTrigger()) {
            LOG.debug("ignoring event '{" + event.getType() + " | " + event.getPath()
                    + "}' since shutdown triggered");
            return;
        }
        if (stateChanged) {
            processStateChanged(event);
        }
        if (dataChanged) {
            processDataOrChildChange(event);
        }
    } finally {
        if (stateChanged) {
            getEventLock().getStateChangedCondition().signalAll();

            // If the session expired we have to signal all conditions, because watches might have been removed and
            // there is no guarantee that those
            // conditions will be signaled at all after an Expired event
            if (event.getState() == KeeperState.Expired) {
                getEventLock().getZNodeEventCondition().signalAll();
                getEventLock().getDataChangedCondition().signalAll();
                // We also have to notify all listeners that something might have changed
                fireAllEvents();
            }
        }
        if (znodeChanged) {
            getEventLock().getZNodeEventCondition().signalAll();
        }
        if (dataChanged) {
            getEventLock().getDataChangedCondition().signalAll();
        }
        getEventLock().unlock();
        LOG.debug("Leaving process event");
    }
}

From source file:com.glaf.cluster.catalina.session.ZooKeeperClientImpl.java

License:Apache License

public Set<String> listNodes(final String path, final NodeListChangedListener nodeListChangedListener)
        throws InterruptedException {
    Set<String> res = new HashSet<String>();
    final Watcher watcher = (nodeListChangedListener != null) ? new Watcher() {
        @Override/*from  w w  w.ja  va 2s .  c o m*/
        public void process(WatchedEvent event) {
            if (event.getType() == Watcher.Event.EventType.NodeChildrenChanged) {
                nodeListChangedListener.onNodeListChanged();
            }
        }
    } : null;
    try {
        List<String> children = zooKeeperCall("Cannot list nodes", new Callable<List<String>>() {
            @Override
            public List<String> call() throws Exception {
                return zooKeeper.getChildren(path, watcher);
            }
        });

        if (children == null) {
            return null;
        }
        for (String childPath : children) {
            res.add(extractLastPart(childPath));
        }
        return res;
    } catch (KeeperException e) {
        throw new RuntimeException("Cannot list nodes", e);
    }
}

From source file:com.glaf.cluster.catalina.session.ZooKeeperClientImpl.java

License:Apache License

private Watcher wrapNodeListener(final NodeListener nodeListener) {
    if (nodeListener != null) {
        return new Watcher() {
            @Override/*from  www  .j a v a 2 s.  c  om*/
            public void process(WatchedEvent event) {
                switch (event.getType()) {
                case NodeCreated:
                    nodeListener.onNodeCreated(event.getPath());
                    break;
                case NodeDeleted:
                    nodeListener.onNodeDeleted(event.getPath());
                    break;
                case NodeDataChanged:
                    nodeListener.onNodeDataChanged(event.getPath());
                    break;
                default:
                    break;
                }
            }
        };
    } else {
        return null;
    }
}

From source file:com.glaf.core.test.zookeeper.ConfigWatcher.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    if (event.getType() == EventType.NodeDataChanged) {
        try {//from  w  w  w.  j a  v a2 s  .  c om
            displayConfig();
        } catch (InterruptedException e) {
            System.err.println("Interrupted. Exiting.");
            Thread.currentThread().interrupt();
        } catch (KeeperException e) {
            System.err.printf("KeeperException: %s. Exiting.\n", e);
        }
    }
}

From source file:com.griddynamics.jagger.coordinator.zookeeper.ZookeeperCoordinator.java

License:Open Source License

private <C extends Command<R>, R extends Serializable> void registerExecutor(final NodeContext nodeContext,
        final CommandExecutor<C, R> executor, ZNode node) {
    final ZNode executorNode = node.createChild(znode().withPath(nodeNameOf(executor.getQualifier())));
    final ZNode queueNode = executorNode.createChild(znode().withPath("queue"));
    executorNode.createChild(znode().withPath("result"));

    log.debug("Created znodes for executor {}", executorNode.getPath());

    queueNode.addChildrenWatcher(new Watcher() {
        @Override/*  w  w w .  j  a va  2  s  .  c om*/
        public void process(WatchedEvent event) {
            if (event.getType() != Event.EventType.NodeChildrenChanged) {
                return;
            }

            synchronized (lock) {
                if (log.isDebugEnabled()) {
                    log.debug("Children changed {} event type {}", queueNode.getPath(), event.getType());
                }

                List<QueueEntry<C, R>> entries = getEntries(queueNode, this);

                for (final QueueEntry<C, R> entry : entries) {
                    Runnable run = new Runnable() {

                        @Override
                        public void run() {
                            executeCommand(executor, executorNode, entry, nodeContext);
                        }
                    };

                    ZookeeperCoordinator.this.executor.execute(run);
                }
            }

        }
    });
}

From source file:com.gxl.shark.resources.watcher.ZookeeperWatcher.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    if (null == zk_client)
        return;// ww  w  . j a va  2s .  com
    try {
        Thread.sleep(100);
        /* ? */
        zk_client.exists(nodePath, this);
        EventType eventType = event.getType();
        final String VALUE = "zookeeper?";
        switch (eventType) {
        case NodeCreated:
            logger.info(VALUE + "[" + event.getPath() + "]");
            break;
        case NodeDataChanged:
            String nodePathValue = new String(zk_client.getData(nodePath, false, null));
            registerBean.register(nodePathValue);
            logger.info(VALUE + "[" + event.getPath() + "]???");
            break;
        case NodeChildrenChanged:
            logger.info(VALUE + "[" + event.getPath() + "]???");
            break;
        case NodeDeleted:
            logger.info(VALUE + "[" + event.getPath() + "]");
        default:
            break;
        }
    } catch (Exception e) {
        throw new ResourceException("zookeeper??[" + e.toString() + "]");
    }
}

From source file:com.haoocai.jscheduler.client.task.TaskWatcher.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    start();/*  w w  w .  ja  v a 2 s  .c o m*/

    if (event.getType() == Event.EventType.NodeDataChanged) {
        try {
            task.run();
        } catch (Exception e) {
            LOG.error("process task error:{}.", e.getMessage(), e);
        } finally {
            zkClient.setData(pathPrefix + "/" + task.name() + "/servers",
                    JvmIdentify.id().getBytes(UTF8_CHARSET));
        }
    } else {
        LOG.error("unexpected event type:{}.", event.getType());
    }
}