Example usage for org.apache.zookeeper WatchedEvent getState

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

Introduction

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

Prototype

public KeeperState getState() 

Source Link

Usage

From source file:org.phunt.zookeeper.removewatchexample.ExerciseRemoveWatchTest.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    if (event.getState() == KeeperState.SyncConnected) {
        zkAvail.countDown();
    }
}

From source file:org.prot.util.zookeeper.ZooHelper.java

License:Open Source License

@Override
public void process(WatchedEvent event) {
    logger.info("ZooKeeper event");
    switch (event.getState()) {
    case Expired:
        logger.info("ZooKeeper session expired");
        break;//ww  w.  ja  v  a  2  s.  c  o  m
    case Disconnected:
        logger.info("ZooKeeper disconnected");
        break;
    default:
        logger.debug("Unhandled ZooKeeper event");
        return;
    }

    reconnect();
}

From source file:org.reborndb.reborn.TestRoundRobinJedisPool.java

License:Open Source License

public ZooKeeper connect(String host, int timeout) throws Exception {
    final CountDownLatch connectedLatch = new CountDownLatch(1);

    try {/*w w w.  j  a  va2  s .  co  m*/
        ZooKeeper zk = new ZooKeeper(host, timeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == Event.KeeperState.SyncConnected) {
                    connectedLatch.countDown();
                }
            }
        });

        if (ZooKeeper.States.CONNECTING == zk.getState()) {
            connectedLatch.await();
        }

        return zk;
    } catch (Exception e) {
        return null;
    }
}

From source file:org.rioproject.zookeeper.watcher.ZooKeeperServiceWatcher.java

License:Apache License

public void process(WatchedEvent event) {
    String path = event.getPath();
    if (logger.isDebugEnabled())
        logger.debug("{}", event);
    if (path == null)
        return;//w w w  .  j  a v a  2s  .  com
    if (event.getType() == Event.EventType.None && event.getState().equals(Event.KeeperState.Expired)) {
        services.remove(path).serviceFailure(null, path);
    } else {
        if (logger.isDebugEnabled())
            logger.debug("Path: {}", path);
        if (services.get(path) != null) {
            /* Something has changed on the node, let's find out if it still exists */
            zooKeeper.exists(path, false, new AsyncCallback.StatCallback() {
                public void processResult(int rc, String path, Object ctx, Stat stat) {
                    if (KeeperException.Code.NONODE.equals(KeeperException.Code.get(rc))) {
                        services.remove(path).serviceFailure(null, path);
                    }
                }
            }, null);
        }
    }
}

From source file:org.trafodion.rest.zookeeper.ZkClient.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
        connectedSignal.countDown();//w ww .  ja va2  s.c  o  m
    }
}

From source file:org.usergrid.locking.zookeeper.ZooPut.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    // nocommit: consider how we want to accomplish this
    if (event.getState() == KeeperState.SyncConnected) {
        synchronized (this) {
            connected = true;/*from ww  w .  jav a 2  s  . c  o m*/
            this.notify();
        }
    }

}

From source file:perLucene.ZooTiger.java

License:Open Source License

ZooTiger() {
    String[] line = Config.readLocalConfig();

    super.initZookeeper(line[0], Integer.parseInt(line[1]), new Watcher() {
        @Override/*  w  w  w .j a  v a 2s . co  m*/
        public void process(WatchedEvent e) {
            if (!(e.getState().equals(Watcher.Event.KeeperState.SyncConnected))) {
                System.out.println("Disconnected.. exiting");
                System.exit(0);
            }
        }
    });

    interval = Integer.parseInt(line[2]);
    replica = Integer.parseInt(line[3]);

    broker = new Broker();

}

From source file:yangqi.code.DataMonitor.java

License:Open Source License

public void process(WatchedEvent event) {
    String path = event.getPath();
    System.out.println("GOT EVENT " + event + " @" + new Date() + ",type is " + event.getType());
    if (event.getType() == Event.EventType.None) {
        // We are are being told that the state of the
        // connection has changed
        switch (event.getState()) {
        case SyncConnected:
            // In this particular example we don't need to do anything
            // here - watches are automatically re-registered with
            // server and any watches triggered while the client was
            // disconnected will be delivered (in order of course)
            break;
        case Expired:
            // It's all over
            dead = true;//from  w ww.  j  av a  2  s.  co m
            listener.closing(KeeperException.Code.SessionExpired);
            break;
        case AuthFailed:
            break;
        case ConnectedReadOnly:
            break;
        case Disconnected:
            break;
        case NoSyncConnected:
            break;
        case SaslAuthenticated:
            break;
        case Unknown:
            break;
        default:
            break;
        }
    } else {
        if (path != null && path.equals(znode)) {
            // Something has changed on the node, let's find out
            zk.exists(znode, true, this, null);
        }
    }
    if (chainedWatcher != null) {
        chainedWatcher.process(event);
    }
}

From source file:zk.ha.ActiveStandbyElector.java

License:Apache License

synchronized void processWatchEvent(ZooKeeper zk, WatchedEvent event) {
    Event.EventType eventType = event.getType();

    if (eventType == Event.EventType.None) {
        // the connection state has changed
        switch (event.getState()) {
        case SyncConnected:
            LOG.info("Session connected.");

            // if the listener was asked to move to safe state then it needs to
            // be undone
            ConnectionState prevConnectionState = zkConnectionState;
            zkConnectionState = ConnectionState.CONNECTED;
            if (prevConnectionState == ConnectionState.DISCONNECTED && wantToBeInElection) {
                monitorActiveStatus();/*from w w  w .  j a  va  2  s .  com*/
            }
            break;
        case Disconnected:
            LOG.info("Session disconnected. Entering neutral mode...");

            // ask the app to move to safe state because zookeeper connection
            // is not active and we dont know our state
            zkConnectionState = ConnectionState.DISCONNECTED;
            //            enterNeutralMode();
            break;
        case Expired:
            // the connection got terminated because of session timeout
            // call listener to reconnect
            //            LOG.info("Session expired. Entering neutral mode and rejoining...");
            //            enterNeutralMode();
            //            reJoinElection(0);
            break;
        case SaslAuthenticated:
            LOG.info("Successfully authenticated to ZooKeeper using SASL.");
            break;
        default:
            fatalError("Unexpected Zookeeper watch event state: " + event.getState());
            break;
        }

        return;
    }

    // a watch on lock path in zookeeper has fired. so something has changed on
    // the lock. ideally we should check that the path is the same as the lock
    // path but trusting zookeeper for now
    String path = event.getPath();
    if (path != null) {
        switch (eventType) {
        case NodeDeleted:
            if (state == State.ACTIVE) {
                enterNeutralMode();
            }
            joinElectionInternal();
            break;
        case NodeDataChanged:
            //            monitorActiveStatus();
            break;
        default:
            if (LOG.isDebugEnabled()) {
                LOG.debug("Unexpected node event: " + eventType + " for path: " + path);
            }
            //            monitorActiveStatus();
        }

        return;
    }

    // some unexpected error has occurred
    fatalError("Unexpected watch error from Zookeeper");
}

From source file:zookeeper.CountdownWatcher.java

License:Apache License

public synchronized void process(WatchedEvent event) {
    System.out.println("Watcher " + name + " got event " + event);

    state = event.getState();
    if (state == KeeperState.SyncConnected) {
        connected = true;/*from   w  ww.j  a  va2s .c  om*/
        clientConnected.countDown();
    } else {
        connected = false;
    }
    notifyAll();
}