Example usage for org.apache.zookeeper WatchedEvent getPath

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

Introduction

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

Prototype

public String getPath() 

Source Link

Usage

From source file:org.wltea.analyzer.dic.Dictionary.java

License:Apache License

public void process(WatchedEvent event) {
    // TODO Auto-generated method stub
    logger.debug("path: " + event.getPath() + "   EventType: " + event.getType());
    if (event.getType() == Watcher.Event.EventType.NodeDataChanged && event.getPath() != null) {
        String path = event.getPath();
        logger.debug("path: " + path);
        if (path.equals(ZK_DIC_EXT + ".add")) {
            Collection<String> words = this.getDataFromZkFile(path);
            this.updateWordsToDict(words, true, singleton._MainDict);
        } else if (path.equals(ZK_DIC_EXT + ".del")) {
            Collection<String> words = this.getDataFromZkFile(path);
            this.updateWordsToDict(words, false, singleton._MainDict);
        } else if (path.equals(ZK_DIC_STOP + ".add")) {
            Collection<String> words = this.getDataFromZkFile(path);
            this.updateWordsToDict(words, true, singleton._StopWordDict);
        } else if (path.equals(ZK_DIC_STOP + ".del")) {
            Collection<String> words = this.getDataFromZkFile(path);
            this.updateWordsToDict(words, true, singleton._StopWordDict);
        }/*from www.j a  v a 2 s . c o m*/
    }
}

From source file:org.wso2.carbon.coordination.core.sync.impl.ZKGroup.java

License:Open Source License

@Override
public void process(WatchedEvent event) {
    if (!this.isActive()) {
        return;//  w  ww  . ja  v  a  2s.c o  m
    }
    String path = event.getPath();

    if (!this.isCommPath(path)) {
        new Thread(new Runnable() {
            /* we are creating a new thread here because, from zookeeper, when we are
             * in the same thread as the watch event happens, another watch event cannot
             * be received later */
            public void run() {
                try {
                    processMemberNodes();
                } catch (Exception e) {
                    log.error("Error in processing WatchedEvent: " + e.getMessage(), e);
                }
            }
        }).start();
    }
}

From source file:voldemort.store.metadata.MetadataStore.java

License:Apache License

/**
 * Called from ZooKeeper when a watched event is triggered.
 *
 * @param event//from ww w.  j a  v a2 s. co m
 */

@Override
public void process(WatchedEvent event) {
    logger.info(String.format("Got event from ZooKeeper: %s", event.toString()));
    try {
        for (String key : MetadataStore.REQUIRED_KEYS) {
            if (key.equals(event.getPath()) || event.getPath().contains(key)) {
                logger.info("ZK event with path matches key: " + key + ", updating metadatacache");

                writeLock.lock();
                try {
                    // get new version of the object and re sets watch flag if appropriate for the key
                    Versioned<String> versioned = innerStore.get(key, null).get(0);

                    Versioned<Object> vObject = convertStringToObject(key, versioned);

                    metadataCache.put(key, vObject);

                    if (key.equals(CLUSTER_KEY)) {
                        updateRoutingStrategies((Cluster) vObject.getValue(), getStoreDefList());
                    } else if (key.equals(STORES_KEY)) {
                        updateRoutingStrategies(getCluster(), (List<StoreDefinition>) vObject.getValue());
                    } else if (SYSTEM_STORES_KEY.equals(key)) {
                        throw new VoldemortException("Cannot overwrite system store definitions");
                    }

                } finally {
                    writeLock.unlock();
                }
            }
        }
    } catch (VoldemortException e) {
        logger.info("failed watching/processing key: " + event.getPath());
        throw new VoldemortException("failed watching/processing event key: " + event.getPath(), e);
    }
}

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;// w  w w  .jav  a 2s .  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();/*  w w w.j av  a  2s  . co  m*/
            }
            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");
}