Example usage for org.apache.zookeeper ZooKeeper ZooKeeper

List of usage examples for org.apache.zookeeper ZooKeeper ZooKeeper

Introduction

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

Prototype

public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly)
        throws IOException 

Source Link

Document

To create a ZooKeeper client object, the application needs to pass a connection string containing a comma separated list of host:port pairs, each corresponding to a ZooKeeper server.

Usage

From source file:com.brainlounge.zooterrain.zkclient.ZkStateObserver.java

License:Apache License

public void start() {
    try {//from w w w  . jav  a 2 s .c om
        zk = new ZooKeeper(zkConnection, 30 * 1000, null, false);
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        throw new RuntimeException("zk connection failed");
    }
    zk.register(this);
    try {
        zk.getChildren("/", this);
    } catch (KeeperException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (InterruptedException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
}

From source file:com.yahoo.pulsar.zookeeper.ZookeeperClientFactoryImpl.java

License:Apache License

@Override
public CompletableFuture<ZooKeeper> create(String serverList, SessionType sessionType,
        int zkSessionTimeoutMillis) {
    // Create a normal ZK client
    boolean canBeReadOnly = sessionType == SessionType.AllowReadOnly;

    CompletableFuture<ZooKeeper> future = new CompletableFuture<>();
    try {//from ww  w. ja  v a 2s . c om
        CompletableFuture<Void> internalFuture = new CompletableFuture<>();

        ZooKeeper zk = new ZooKeeper(serverList, zkSessionTimeoutMillis, event -> {
            if (event.getType() == Event.EventType.None) {
                switch (event.getState()) {

                case ConnectedReadOnly:
                    checkArgument(canBeReadOnly);
                    // Fall through
                case SyncConnected:
                    // ZK session is ready to use
                    internalFuture.complete(null);
                    break;

                case Expired:
                    internalFuture
                            .completeExceptionally(KeeperException.create(KeeperException.Code.SESSIONEXPIRED));
                    break;

                default:
                    log.warn("Unexpected ZK event received: {}", event);
                    break;
                }
            }
        }, canBeReadOnly);

        internalFuture.thenRun(() -> {
            log.info("ZooKeeper session established: {}", zk);
            future.complete(zk);
        }).exceptionally((exception) -> {
            log.error("Failed to establish ZooKeeper session: {}", exception.getMessage());
            future.completeExceptionally(exception);
            return null;
        });

    } catch (IOException e) {
        future.completeExceptionally(e);
    }

    return future;
}

From source file:io.kodokojo.commons.utils.properties.provider.kv.ZookeeperJsonObjectValueProvider.java

License:Open Source License

public ZookeeperJsonObjectValueProvider(String zookeeperUrl,
        KeyToZookeeperPathConverter keyToZookeeperPathConverter) {
    if (isBlank(zookeeperUrl)) {
        throw new IllegalArgumentException("zookeeperUrl must be defined.");
    }/*from  w  w  w.  jav  a  2s .c  o m*/
    this.zookeeperUrl = zookeeperUrl;
    try {
        client = new ZooKeeper(zookeeperUrl, 1000, (event -> {
        }), true);
    } catch (IOException e) {
        throw new IllegalStateException("Unable to create a Zookeeper client with url " + zookeeperUrl, e);
    }
    this.keyToZookeeperPathConverter = keyToZookeeperPathConverter;
}

From source file:io.kodokojo.commons.utils.properties.provider.kv.ZookeeperPropertyValueProvider.java

License:Open Source License

public ZookeeperPropertyValueProvider(String zookeeperUrl,
        KeyToZookeeperPathConverter keyToZookeeperPathConverter) {
    if (isBlank(zookeeperUrl)) {
        throw new IllegalArgumentException("zookeeperUrl must be defined.");
    }/*from   w w  w.  jav a  2 s  .com*/
    this.zookeeperUrl = zookeeperUrl;
    try {
        client = new ZooKeeper(zookeeperUrl, 1000, (event -> {
        }), true);
    } catch (IOException e) {
        throw new IllegalStateException("Unable to create a Zookeeper client with url " + zookeeperUrl, e);
    }
    this.keyToZookeeperPathConverter = keyToZookeeperPathConverter;
}

From source file:io.teknek.daemon.BadPlanNameTest.java

License:Apache License

@Test
public void hangAround() throws IOException, WorkerDaoException {
    p = new Plan()
            .withFeedDesc(new FeedDesc().withFeedClass(FixedFeed.class.getName()).withProperties(
                    MapBuilder.makeMap(FixedFeed.NUMBER_OF_PARTITIONS, 2, FixedFeed.NUMBER_OF_ROWS, 10)))
            .withRootOperator(new OperatorDesc(new BeLoudOperator()));
    p.setName("yell");
    p.setMaxWorkers(1);/*from  w w w. j a v  a 2  s  .com*/

    DummyWatcher dw = new DummyWatcher();
    ZooKeeper zk = new ZooKeeper(zookeeperTestServer.getConnectString(), 200, dw, false);
    createOrUpdateBadPlan(p, zk);
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.apache.bookkeeper.zookeeper.BkZooKeeperClient.java

License:Apache License

protected ZooKeeper createZooKeeper() throws IOException {
    return new ZooKeeper(connectString, sessionTimeoutMs, watcherManager, allowReadOnlyMode);
}

From source file:org.apache.curator.utils.DefaultZookeeperFactory.java

License:Apache License

@Override
public ZooKeeper newZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly)
        throws Exception {
    return new ZooKeeper(connectString, sessionTimeout, watcher, canBeReadOnly);
}

From source file:org.apache.hoya.providers.accumulo.AccumuloProviderService.java

License:Apache License

private void verifyZookeeperLive(String zkQuorum, int timeout)
        throws IOException, KeeperException, InterruptedException {

    BlockingZKWatcher watcher = new BlockingZKWatcher();
    ZooKeeper zookeeper = new ZooKeeper(zkQuorum, 10000, watcher, true);
    zookeeper.getChildren("/", watcher);

    watcher.waitForZKConnection(timeout);

}

From source file:org.apache.hoya.tools.ZKIntegration.java

License:Apache License

public void init() throws IOException {
    assert zookeeper == null;
    log.debug("Binding ZK client to {}", zkConnection);
    zookeeper = new ZooKeeper(zkConnection, sessionTimeout, this, canBeReadOnly);
}

From source file:org.apache.phoenix.util.PhoenixMRJobUtil.java

License:Apache License

public static String getActiveResourceManagerHost(Configuration config, String zkQuorum)
        throws IOException, InterruptedException, JSONException, KeeperException,
        InvalidProtocolBufferException, ZooKeeperConnectionException {
    ZooKeeperWatcher zkw = null;/*from  w  w  w  .j  av  a  2s  .c o m*/
    ZooKeeper zk = null;
    String activeRMHost = null;
    try {
        zkw = new ZooKeeperWatcher(config, "get-active-yarnmanager", null);
        zk = new ZooKeeper(zkQuorum, 30000, zkw, false);

        List<String> children = zk.getChildren(YARN_LEADER_ELECTION, zkw);
        for (String subEntry : children) {
            List<String> subChildern = zk.getChildren(YARN_LEADER_ELECTION + "/" + subEntry, zkw);
            for (String eachEntry : subChildern) {
                if (eachEntry.contains(ACTIVE_STANDBY_ELECTOR_LOCK)) {
                    String path = YARN_LEADER_ELECTION + "/" + subEntry + "/" + ACTIVE_STANDBY_ELECTOR_LOCK;
                    byte[] data = zk.getData(path, zkw, new Stat());
                    ActiveRMInfoProto proto = ActiveRMInfoProto.parseFrom(data);
                    proto.getRmId();
                    LOG.info("Active RmId : " + proto.getRmId());

                    activeRMHost = config.get(YarnConfiguration.RM_HOSTNAME + "." + proto.getRmId());
                    LOG.info("activeResourceManagerHostname = " + activeRMHost);

                }
            }
        }
    } finally {
        if (zkw != null)
            zkw.close();
        if (zk != null)
            zk.close();
    }

    return activeRMHost;
}