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:org.eclipse.gyrex.cloud.internal.zk.ZooKeeperMonitor.java

License:Open Source License

@Override
public void process(final WatchedEvent event) {
    switch (event.getType()) {
    case NodeChildrenChanged:
        childrenChanged(event.getPath());
        break;//  w ww  .j a v  a 2  s.  c  om
    case NodeCreated:
        pathCreated(event.getPath());
        break;
    case NodeDeleted:
        pathDeleted(event.getPath());
        break;
    case NodeDataChanged:
        recordChanged(event.getPath());
        break;
    case None:
        // state of the connection changed
        // TODO: how to handle/consume this event here?
        // our prefered way of working with ZK connect/disconnect/expired events
        // is using the ZKGate which has it's own connect listener (integrated into ZKBasedService)
        //         switch (event.getState()) {
        //            case SyncConnected:
        //               // connection established, nothing to do because watches
        //               // will be automatically re-registered and delivered by ZooKeeper
        //               connected();
        //               break;
        //            case Disconnected:
        //               // session disconnected, the gate will recover
        //               disconnected();
        //               break;
        //            case Expired:
        //               // session expired, the gate will close
        //               expired();
        //               break;
        //         }
        break;
    default:
        LOG.warn("Unhandled event ({}) in ({})", new Object[] { event, this });
        break;
    }
}

From source file:org.fusesource.meshkeeper.distribution.registry.zk.ZooKeeperChildWatcher.java

License:Open Source License

public ZooKeeperChildWatcher(ZooKeeper zk, String path) {
    this.zk = zk;
    this.path = path;
    this.watcher = new Watcher() {

        public void process(WatchedEvent event) {
            switch (event.getType()) {
            case NodeChildrenChanged: {
                //System.out.println("NodeChildrenChangedEvent");
                watch();/*from w ww  .j av  a2 s  .  c o  m*/
                break;
            }
            default: {
                System.out.println("WARNING Got:" + event);
            }
            }
        }
    };

    this.callback = new ChildrenCallback() {

        public void processResult(int rc, String path, Object ctx, List<String> children) {
            handleChildUpdate(path, children);
        }
    };

}

From source file:org.hummer.config.WatcherAdapter.java

License:Apache License

public void process(WatchedEvent event) {
    WatchEvent evt = new WatchEvent();
    evt.setState(event.getState().getIntValue());
    evt.setType(event.getType().getIntValue());
    evt.setArg(event.getPath());//from w  w w  .  j  a va 2  s  . c o  m
    innerWatcher.onEvent(evt);
}

From source file:org.jc.zk.dpw.DataMonitor.java

@Override
public void process(WatchedEvent event) {
    String path = event.getPath();
    switch (event.getType()) {
    case NodeDataChanged:
        if (path.equals(this.znodeTime)) {
            //Time has been updated
            this.listener.timeZnodeChanged();
        } else if (path.equals(this.znodeMaster)) {
            this.listener.masterZnodeChanged();
        } else if (path.equals(this.processHeartBeatZnode)) {
            this.listener.processHeartBeatZnodeUpdate();
        } else if (this.znodeToCreateForUpdates == null && this.cmwUpdateZnodes.contains(event.getPath())) {
            this.listener.cmwUpdatedUpdateZnode(event.getPath());
        } else if (this.znodeToCreateForUpdates != null
                && this.znodeToCreateForUpdates.equals(event.getPath())) {
            this.bindToZnodes(this.isActiveMaster);
        } else if (path.equals(this.amwRequestKillZnode)) {
            this.listener.amwRequestKillZnodeChanged();
        }//  w w w  .  j a  v  a  2s  .c o  m
        break;
    case None:
        if (event.getState() == Event.KeeperState.Expired
                || event.getState() == Event.KeeperState.Disconnected) {
            this.listener.disconnected(KeeperException.Code.CONNECTIONLOSS.intValue());
        } else if (event.getState() == Event.KeeperState.SyncConnected) {
            this.listener.connected();
        }
        break;
    case NodeDeleted:
        if (event.getPath().equals(this.znodeTime)) {
            this.listener.timeZnodeRemoved();
        } else if (event.getPath().equals(this.znodeProcessObserved)) {
            this.listener.processObservedZnodeRemoved();
        } else if (this.cmwUpdateZnodes.contains(event.getPath())) {
            this.listener.cmwUpdateZnodeRemoved();
        }
        break;
    case NodeCreated:
        if (this.ownCMWFailoverZnode != null && this.ownCMWFailoverZnode.equals(event.getPath())) {
            this.listener.cmwFailoverZnodeCreated(event.getPath());
        } else if (this.znodeToCreateForUpdates != null
                && this.znodeToCreateForUpdates.equals(event.getPath())) {
            /**
             * To comply with the old model, where CMWs created update
             * znode, instead of waiting for AMW to create them, once CMW
             * detects that update znode was created by AMW, it will 
             * automatically read the data from znode, that is, instead
             * of invoking callback to notify about the creation of znode
             * by master, it will proceed to read the content and later
             * notify CMW about the new data.
             */
            Map<String, String> ctx = new HashMap<>();
            ctx.put(ZNODE_TYPE, CMW_UPDATE_NODE);
            ctx.put(UPDATE_NODE_EVENT, UPDATE_EVENT_IS_CREATE);
            this.zk.getData(path, this, this, ctx);
        } else if (this.znodeToCreateForUpdates == null && this.cmwUpdateZnodes.contains(event.getPath())) {
            this.bindToZnodes(this.isActiveMaster);
        } else if (event.getPath().equals(this.znodeMaster)) {
            this.listener.masterZnodeCreated();
        } else if (event.getPath().equals(this.znodeTime)) {
            this.listener.timeZnodeCreated();
        } else if (event.getPath().equals(this.znodeProcessObserved)) {
            this.listener.processObservedZnodeCreated();
        }
        break;
    }
}

From source file:org.jc.zk.dpw.DataMonitorProcesses.java

@Override
public void process(WatchedEvent event) {
    switch (event.getType()) {
    case NodeCreated:
        if (event.getPath().equals(this.znodeObserved)) {

        }/*from   ww  w .  ja v  a2s . c o m*/
        break;
    case NodeDataChanged:
        if (event.getPath().equals(this.znodeTime)) {
            this.readTimeZnode();
        }
        break;
    case None:
        if (event.getState() == Event.KeeperState.Disconnected
                || event.getState() == Event.KeeperState.Expired) {
            this.listener.closing(KeeperException.Code.CONNECTIONLOSS.intValue());
        }
        break;
    case NodeDeleted:
        if (event.getPath().equals(this.znodeObserved) || event.getPath().equals(this.znodeTime)) {
            this.listener.closing(KeeperException.Code.CONNECTIONLOSS.intValue());
        }
        break;
    }
}

From source file:org.jc.zk.dpw.TimeDataMonitor.java

@Override
public void process(WatchedEvent event) {
    System.out.print(event.toString());
    switch (event.getType()) {
    case NodeCreated:
        if (event.getPath().equals(this.znodeForTimeListeners)) {
            this.listener.timeListenersZnodeCreated();
        } else if (event.getPath().equals(this.timeZnodeRemovedFlagZnode)) {
            this.listener.notificationZnodeCreated();
        }//from w  w  w  .j a v  a  2 s . c  om
        break;
    case NodeDeleted:
        if (event.getPath().equals(this.timeZnode)) {
            this.listener.timeZnodeDeleted();
        } else if (event.getPath().equals(this.znodeForTimeListeners)) {
            this.listener.timeListenersZnodeDeleted();
        } else if (event.getPath().equals(this.timeZnodeRemovedFlagZnode)) {
            this.listener.notificationZnodeRemoved();
        }
        break;
    case NodeDataChanged:
        if (event.getPath().equals(this.timeZnode)) {
            this.listener.timeZnodeChanged();
            //this.zk.getData(this.timeZnode, null, this, this.ctx);
        } else if (event.getPath().equals(this.znodeForTimeListeners)) {
            this.listener.timeListenersZnodeChanged();
        } else if (event.getPath().equals(this.requestAMWKillZnode)) {
            this.listener.requestAMWKillZnodeChanged();
        }
        break;
    case None:
        if (event.getState() == Event.KeeperState.SyncConnected) {
            this.listener.connected();
        } else if (event.getState() == Event.KeeperState.Disconnected
                || event.getState() == Event.KeeperState.Expired) {
            this.listener.disconnected(KeeperException.Code.CONNECTIONLOSS.intValue());
        }
        break;
    }
}

From source file:org.jc.zk.process.ProcessWrapperMonitor.java

@Override
public void process(WatchedEvent event) {
    switch (event.getType()) {
    case None://from  ww  w.j a v  a 2s . co  m
        if (event.getState() == Event.KeeperState.Disconnected
                || event.getState() == Event.KeeperState.Expired) {
            this.listener.disconnected();
        } else if (event.getState() == Event.KeeperState.SyncConnected) {
            this.listener.connected();
        }
        break;
    case NodeCreated:
        if (event.getPath().equals(this.heartBeatZnode)) {
            this.listener.pHeartBeatZnodeCreated();
        }
        break;
    case NodeDeleted:
        if (event.getPath().equals(this.heartBeatZnode)) {
            this.listener.pHeartBeatZnodeRemoved();
        }
        break;
    }
}

From source file:org.lable.oss.dynamicconfig.provider.zookeeper.NodeWatcher.java

License:Apache License

@Override
public void process(WatchedEvent watchedEvent) {
    if (state != State.LIVE)
        return;/*from w w  w. j a  v a2 s.  co m*/

    Event.KeeperState state = watchedEvent.getState();
    Event.EventType type = watchedEvent.getType();

    // This switch handles all relevant states, and tries to reset the watch on the znode after it is
    // triggered.
    switch (state) {
    case SyncConnected:
    case ConnectedReadOnly:
        resetRetryCounters();
        switch (type) {
        case NodeCreated:
        case NodeDataChanged:
            // Configuration znode changed, let the callback know.
            zk.getData(path, this, callback, null);
            break;
        case None:
            registerWatcher(path);
            break;
        case NodeDeleted:
            logger.error("Our configuration znode was deleted. Waiting for it to be recreated");
            registerWatcher(path);
            break;
        }
        break;
    case Disconnected:
        logger.warn("Disconnected from Zookeeper quorum, reconnecting");
        // The Zookeeper instance will automatically attempt reconnection.
        waitBeforeRetrying();
        break;
    case Expired:
        logger.warn("Connection to Zookeeper quorum expired. Attempting to reconnect");
        // The Zookeeper instance is no longer valid. We have to reconnect ourselves.
        connect();
        break;
    case SaslAuthenticated:
    case AuthFailed:
        // Probably not relevant to us.
        break;
    }
}

From source file:org.labs.qbit.election.leader.LeaderProcedure.java

License:Open Source License

@Override
public void process(WatchedEvent event) {
    logger.info("An event occurred for [{}] for process ID [{}]", event.getPath(), processID);
    if (event.getType() == Watcher.Event.EventType.None) {
        createZNode();/*from  www  . j ava2 s. c  o m*/
    } else {
        String path = event.getPath();
        if (path != null && path.equals(zNodeBasePath)) {
            try {
                zooKeeper.getChildren(zNodeBasePath, this);
            } catch (KeeperException e) {
                logger.error("An error occurred", e);
            } catch (InterruptedException e) {
                logger.error("An error occurred", e);
            }
        }
    }
}

From source file:org.midonet.midolman.state.ZkLeaderElectionWatcher.java

License:Apache License

public void process(WatchedEvent event) {
    if (event.getType().equals(Event.EventType.NodeDeleted)) {
        try {//from  w w w. j  ava 2s .c o m
            String nextNodePath = watchNextNode(mySeqNum, executeOnBecomingLeader, leaderPath, zk);
            if (nextNodePath == null) {
                executeOnBecomingLeader.call();
            }
        } catch (StateAccessException e) {
            log.error("error trying to install watcher on " + leaderPath + ": " + e.getMessage());
        }
    }
}