Example usage for org.apache.zookeeper Watcher process

List of usage examples for org.apache.zookeeper Watcher process

Introduction

In this page you can find the example usage for org.apache.zookeeper Watcher process.

Prototype

void process(WatchedEvent event);

Source Link

Usage

From source file:co.cask.cdap.common.zookeeper.coordination.ResourceCoordinator.java

License:Apache License

/**
 * Wraps a given Watcher so that it only get triggered if {@link #shouldProcess()} returns true.
 *//* w ww  .  ja  va2  s .  co m*/
private Watcher wrapWatcher(final Watcher watcher) {
    return new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (!shouldProcess()) {
                return;
            }
            watcher.process(event);
        }
    };
}

From source file:co.cask.cdap.common.zookeeper.coordination.ResourceCoordinatorClient.java

License:Apache License

/**
 * Wraps a ZK watcher so that it only get triggered if this service is running.
 *
 * @param watcher The Watcher to wrap./*from  w  w  w .  j  av a 2  s  . c  om*/
 * @return A wrapped Watcher.
 */
private Watcher wrapWatcher(final Watcher watcher) {
    return new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (isRunning()) {
                watcher.process(event);
            }
        }
    };
}

From source file:co.cask.tephra.zookeeper.TephraZKClientService.java

License:Apache License

/**
 * Wraps the given watcher to be called from the event executor.
 * @param watcher Watcher to be wrapped//from  w w w.  j  a  v  a2 s . c  o m
 * @return The wrapped Watcher
 */
private Watcher wrapWatcher(final Watcher watcher) {
    if (watcher == null) {
        return null;
    }
    return new Watcher() {
        @Override
        public void process(final WatchedEvent event) {
            if (eventExecutor.isShutdown()) {
                LOG.debug("Already shutdown. Discarding event: {}", event);
                return;
            }
            eventExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        watcher.process(event);
                    } catch (Throwable t) {
                        LOG.error("Watcher throws exception.", t);
                    }
                }
            });
        }
    };
}

From source file:co.cask.tephra.zookeeper.TephraZKClientService.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    State state = state();/*from  ww w  .j a v  a  2 s. c  o m*/
    if (state == State.TERMINATED || state == State.FAILED) {
        return;
    }

    try {
        if (event.getState() == Event.KeeperState.SyncConnected && state == State.STARTING) {
            LOG.debug("Connected to ZooKeeper: {}", zkStr);
            notifyStarted();
            return;
        }
        if (event.getState() == Event.KeeperState.Expired) {
            LOG.info("ZooKeeper session expired: {}", zkStr);

            // When connection expired, simply reconnect again
            if (state != State.RUNNING) {
                return;
            }
            eventExecutor.submit(new Runnable() {
                @Override
                public void run() {
                    // Only reconnect if the current state is running
                    if (state() != State.RUNNING) {
                        return;
                    }
                    try {
                        LOG.info("Reconnect to ZooKeeper due to expiration: {}", zkStr);
                        closeZooKeeper(zooKeeper.getAndSet(createZooKeeper()));
                    } catch (IOException e) {
                        notifyFailed(e);
                    }
                }
            });
        }
    } finally {
        if (event.getType() == Event.EventType.None) {
            for (Watcher connectionWatcher : connectionWatchers) {
                connectionWatcher.process(event);
            }
        }
    }
}

From source file:com.continuuity.loom.common.zookeeper.LeaderElection.java

License:Apache License

private Watcher wrapWatcher(final Watcher watcher) {
    return new Watcher() {
        @Override//from ww  w  . jav a2 s . c  o m
        public void process(final WatchedEvent event) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    watcher.process(event);
                }
            });
        }
    };
}

From source file:com.continuuity.weave.internal.zookeeper.DefaultZKClientService.java

License:Open Source License

/**
 * Wraps the given watcher to be called from the event executor.
 * @param watcher Watcher to be wrapped//from   ww  w. j  a v a 2s  .  c om
 * @return The wrapped Watcher
 */
private Watcher wrapWatcher(final Watcher watcher) {
    if (watcher == null) {
        return null;
    }
    return new Watcher() {
        @Override
        public void process(final WatchedEvent event) {
            eventExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        watcher.process(event);
                    } catch (Throwable t) {
                        LOG.error("Watcher throws exception.", t);
                    }
                }
            });
        }
    };
}

From source file:com.facebook.zookeeper.mock.MockZkConnectionManager.java

License:Apache License

public void refreshClient() {
    try {/*from   ww w  . j  a v a2  s .  c o  m*/
        if (zk != null) {
            zk.close();
        }
        zk = zooKeeperFactory.create(new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                for (Watcher watcher : watchers) {
                    watcher.process(event);
                }
            }
        });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.facebook.zookeeper.mock.MockZooKeeperDataStore.java

License:Apache License

public synchronized String create(long sessionId, String path, byte[] data, List<ACL> acl,
        CreateMode createMode) throws KeeperException {
    if (isRootPath(path)) {
        throw new KeeperException.NodeExistsException(path);
    }/*from w  ww . jav  a2 s . co  m*/
    String relativePath = stripRootFromPath(path);
    String relativeChildPath = root.createDescendant(sessionId, relativePath, data, acl, createMode);
    String absChildPath = addRootToPath(relativeChildPath);

    // Trigger any creation watches that may exist
    if (creationWatchers.containsKey(absChildPath)) {
        WatchedEvent watchedEvent = new WatchedEvent(EventType.NodeCreated, KeeperState.SyncConnected,
                absChildPath);
        for (Watcher watcher : creationWatchers.get(absChildPath)) {
            watcher.process(watchedEvent);
        }
        creationWatchers.remove(absChildPath);
    }
    return absChildPath;
}

From source file:com.netflix.curator.ConnectionState.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    if (LOG_EVENTS) {
        log.debug("ConnectState watcher: " + event);
    }//from w  w  w  .  j a  va2s.co m

    for (Watcher parentWatcher : parentWatchers) {
        TimeTrace timeTrace = new TimeTrace("connection-state-parent-process", tracer.get());
        parentWatcher.process(event);
        timeTrace.commit();
    }

    boolean wasConnected = isConnected.get();
    boolean newIsConnected = wasConnected;
    if (event.getType() == Watcher.Event.EventType.None) {
        newIsConnected = checkState(event.getState(), wasConnected);
    }

    if (newIsConnected != wasConnected) {
        isConnected.set(newIsConnected);
        connectionStartMs = System.currentTimeMillis();
    }
}

From source file:com.netflix.curator.x.zkclientbridge.CuratorZKClientBridge.java

License:Apache License

@Override
public void connect(final Watcher watcher) {
    if (watcher != null) {
        CuratorListener localListener = new CuratorListener() {
            @Override// w w  w .  j  a v a 2  s  .co  m
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getWatchedEvent() != null) {
                    watcher.process(event.getWatchedEvent());
                }
            }
        };
        curator.getCuratorListenable().addListener(localListener);
        listener.set(localListener);

        try {
            BackgroundCallback callback = new BackgroundCallback() {
                @Override
                public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                    WatchedEvent fakeEvent = new WatchedEvent(Watcher.Event.EventType.None,
                            curator.getZookeeperClient().isConnected() ? Watcher.Event.KeeperState.SyncConnected
                                    : Watcher.Event.KeeperState.Disconnected,
                            null);
                    watcher.process(fakeEvent);
                }
            };
            curator.checkExists().inBackground(callback).forPath("/foo");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}