Example usage for org.apache.zookeeper WatchedEvent toString

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

Introduction

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

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:cn.lhfei.zookeeper.ch01.Master.java

License:Apache License

@Override
public void process(WatchedEvent e) {
    log.info(e.toString());
}

From source file:com.bigdata.zookeeper.AbstractZNodeConditionWatcher.java

License:Open Source License

/**
 * Notify a {@link Thread} synchronized on itself when the znode that it is
 * watching generates an {@link WatchedEvent}. If the event is a
 * disconnect, then we instead set the {@link #disconnected} flag and return
 * immediately./*from   w w  w .j a  v  a2  s . c  om*/
 */
public void process(final WatchedEvent event) {

    if (log.isInfoEnabled())
        log.info(event.toString());

    synchronized (this) {

        switch (event.getState()) {
        case Disconnected:
            // nothing to do until we are reconnected.
            disconnected = true;
            return;
        default:
            if (disconnected) {
                _resumeWatch();
            }
            // fall through
            break;
        }

        boolean satisifed;
        try {
            satisifed = isConditionSatisfied(event);
        } catch (KeeperException e) {
            log.warn(this.toString(), e);
            return;
        } catch (InterruptedException e) {
            log.warn(this.toString(), e);
            return;
        }

        if (satisifed) {

            success(event.getType().toString());

            return;

        } else {

            _resumeWatch();

        }

    }

}

From source file:com.bigdata.zookeeper.HierarchicalZNodeWatcher.java

License:Open Source License

/**
 * Note: Synchronized for mutex with {@link #cancel()}.
 *///  w  w w .  j  a  v a  2 s . com
synchronized public void process(final WatchedEvent event) {

    if (cancelled) {

        // ignore events once cancelled.

        return;

    }

    if (log.isInfoEnabled())
        log.info(event.toString());

    /*
     * Put ALL events in the queue.
     * 
     * Note: This does NOT mean that the application will see all state
     * changes for watched znodes. Zookeeper DOES NOT provide that
     * guarantee. The application CAN miss events between the time that a
     * state change triggers a WatchedEvent and the time that the
     * application handles the event and resets the watch.
     */

    queue.add(event);

    switch (event.getState()) {
    case Disconnected:
        return;
    default:
        break;
    }

    final String path = event.getPath();

    switch (event.getType()) {

    case NodeCreated:

        /*
         * We always reset the watch when we see a NodeCreated event for a
         * znode that we are already watching.
         * 
         * This event type can occur for the root of the watched hierarchy
         * since we set the watch regardless of the existence of the zroot.
         * 
         * If the event occurs for a different znode then it may have been
         * deleted and re-created and we may have missed the delete event.
         */

        try {
            zookeeper.exists(path, this);
        } catch (KeeperException e1) {
            log.error("path=" + path, e1);
        } catch (InterruptedException e1) {
            if (log.isInfoEnabled())
                log.info("path=" + path);
        }

        return;

    case NodeDeleted:

        /*
         * A watched znode was deleted. Unless this is the zroot, we will
         * remove our watch on the znode (the expectation is that the watch
         * will be re-established if the child is re-created since we will
         * notice the NodeChildrenChanged event).
         */

        if (zroot.equals(path)) {

            try {
                zookeeper.exists(path, this);
            } catch (KeeperException e1) {
                log.error("path=" + path, e1);
            } catch (InterruptedException e1) {
                if (log.isInfoEnabled())
                    log.info("path=" + path);
            }

        } else {

            watched.remove(path);

            removedWatch(path);

        }

        return;

    case NodeDataChanged:

        try {
            zookeeper.getData(path, this, new Stat());
        } catch (KeeperException e1) {
            log.error("path=" + path, e1);
        } catch (InterruptedException e1) {
            if (log.isInfoEnabled())
                log.info("path=" + path);
        }

        return;

    case NodeChildrenChanged:
        /*
         * Get the children (and reset our watch on the path).
         */
        try {
            acceptChildren(path, zookeeper.getChildren(event.getPath(), this));
        } catch (KeeperException e) {
            log.error(this, e);
        } catch (InterruptedException e1) {
            if (log.isInfoEnabled())
                log.info("path=" + path);
        }
        return;
    default:
        return;
    }

}

From source file:com.consol.citrus.zookeeper.client.ZooClient.java

License:Apache License

private Watcher getConnectionWatcher() {
    return new Watcher() {
        @Override/*w  w w. j a v a 2  s. c  o  m*/
        public void process(WatchedEvent event) {
            LOG.debug(String.format("Connection Event: %s", event.toString()));
        }
    };
}

From source file:com.dangdang.config.service.easyzk.ConfigNodeEventListener.java

License:Apache License

@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(event.toString());/*  w  w  w. ja  v  a 2s . com*/
    }

    final WatchedEvent watchedEvent = event.getWatchedEvent();
    if (watchedEvent != null) {

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(watchedEvent.toString());
        }

        if (watchedEvent.getState() == KeeperState.SyncConnected) {
            boolean someChange = false;
            switch (watchedEvent.getType()) {
            case NodeChildrenChanged:
                configNode.loadNode(false);
                someChange = true;
                break;
            case NodeDataChanged:
                configNode.loadKey(watchedEvent.getPath());
                someChange = true;
                break;
            default:
                break;
            }

            if (someChange && configNode.getConfigLocalCache() != null) {
                configNode.getConfigLocalCache().saveLocalCache(configNode, configNode.getNode());
            }
        }
    }
}

From source file:com.datis.zookafka.client.Creator.java

@Override
public void process(WatchedEvent e) {
    LOG.info("Processing event: " + e.toString());
    if (e.getType() == Event.EventType.None) {
        switch (e.getState()) {
        case SyncConnected:
            System.out.println("Event SynConnected");
            connected = true;//from   w ww . j  a va 2 s  .c o m
            break;
        case Disconnected:
            System.out.println("Event Disconnected");
            connected = false;
            break;
        case Expired:
            System.out.println("Event Expired");
            expired = true;
            connected = false;
            LOG.error("Session expiration");
        default:
            break;
        }
    }
}

From source file:com.digitgroup.fullstackroad.j2ee.zookeeper.Master.java

License:Apache License

/**
 * This method implements the process method of the
 * Watcher interface. We use it to deal with the
 * different states of a session. /* ww  w . java  2  s .  c om*/
 * 
 * @param e new session event to be processed
 */
public void process(WatchedEvent e) {
    LOG.info("Processing event: " + e.toString());
    if (e.getType() == EventType.None) {
        switch (e.getState()) {
        case SyncConnected:
            connected = true;
            break;
        case Disconnected:
            connected = false;
            break;
        case Expired:
            expired = true;
            connected = false;
            LOG.error("Session expiration");
        default:
            break;
        }
    }
}

From source file:com.digitgroup.fullstackroad.j2ee.zookeeper.Worker.java

License:Apache License

/**
 * Deals with session events like connecting
 * and disconnecting./*from  w  ww  .j av  a2 s . c  om*/
 * 
 * @param e new event generated
 */
public void process(WatchedEvent e) {
    LOG.info(e.toString() + ", " + hostPort);
    if (e.getType() == EventType.None) {
        switch (e.getState()) {
        case SyncConnected:
            /*
             * Registered with ZooKeeper
             */
            connected = true;
            break;
        case Disconnected:
            connected = false;
            break;
        case Expired:
            expired = true;
            connected = false;
            LOG.error("Session expired");
        default:
            break;
        }
    }
}

From source file:com.dinstone.zkclient.core.ZkConnection.java

License:Apache License

public synchronized ZooKeeper getZooKeeper() throws InterruptedException {
    if (this.zooKeeper == null || !zooKeeper.getState().isAlive()) {
        final CountDownLatch connectSingal = new CountDownLatch(1);
        try {//from   w w w .  ja v  a  2s .  c  o m
            this.zooKeeper = new ZooKeeper(zkServers, timeout, new Watcher() {

                public void process(WatchedEvent event) {
                    LOG.debug("Received zookeeper event, {}", event.toString());
                    if (event.getPath() == null) {
                        if (KeeperState.SyncConnected == event.getState()) {
                            connectSingal.countDown();
                            fireStateChangedEvent(event.getState());
                        } else if (KeeperState.Disconnected == event.getState()) {
                            fireStateChangedEvent(event.getState());
                        } else if (KeeperState.Expired == event.getState()) {
                            LOG.debug("Session is expired, need to clear process");
                            fireStateChangedEvent(event.getState());
                        }
                    }
                }
            });
        } catch (IOException e) {
            throw new ZkException("connect zookeeper error", e);
        }
        connectSingal.await();
    }

    return this.zooKeeper;
}

From source file:com.ling.zookeeper.ConfigCenter.java

License:Open Source License

@Override
public void process(WatchedEvent event) {
    System.out.println(event.toString());
    try {/* ww w.  ja va2 s .c o m*/
        this.zk.exists(this.znode, true);
    } catch (KeeperException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}