Java Code Examples for org.apache.curator.framework.CuratorFramework.close()

The following are Jave code examples for showing how to use close() of the org.apache.curator.framework.CuratorFramework class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: mycat-src-1.6.1-RELEASE   File: ZktoXmlMain.java   View Source Code Vote up 7 votes
private static CuratorFramework buildConnection(String url) {
    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));

    // start connection
    curatorFramework.start();
    // wait 3 second to establish connect
    try {
        curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (curatorFramework.getZookeeperClient().isConnected()) {
            return curatorFramework.usingNamespace("");
        }
    } catch (InterruptedException ignored) {
        Thread.currentThread().interrupt();
    }

    // fail situation
    curatorFramework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
 
Example 2
Project: mumu-zookeeper   File: CuratorServer.java   View Source Code Vote up 7 votes
public void testServer(){
        try {
        TestingServer server=new TestingServer(2181,new File("/"));
        server.start();

        CuratorFramework curatorFramework = CuratorFrameworkFactory.
                builder().
                connectString(server.getConnectString()).
                sessionTimeoutMs(1000).
                retryPolicy(new RetryNTimes(3, 1000)).
                build();
        curatorFramework.start();
        System.out.println(curatorFramework.getChildren().forPath("/"));
        curatorFramework.close();
        server.stop();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
 
Example 3
Project: mycat-src-1.6.1-RELEASE   File: XmltoZkMain.java   View Source Code Vote up 6 votes
private static CuratorFramework buildConnection(String url) {
    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));

    // start connection
    curatorFramework.start();
    // wait 3 second to establish connect
    try {
        curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (curatorFramework.getZookeeperClient().isConnected()) {
            return curatorFramework.usingNamespace("");
        }
    } catch (InterruptedException ignored) {
        Thread.currentThread().interrupt();
    }

    // fail situation
    curatorFramework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
 
Example 4
Project: mycat-src-1.6.1-RELEASE   File: ZKUtils.java   View Source Code Vote up 6 votes
private static CuratorFramework createConnection() {
       String url= ZkConfig.getInstance().getZkURL();

    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));

    // start connection
    curatorFramework.start();
    // wait 3 second to establish connect
    try {
        curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (curatorFramework.getZookeeperClient().isConnected()) {
            return curatorFramework;
        }
    } catch (InterruptedException ignored) {
        Thread.currentThread().interrupt();
    }

    // fail situation
    curatorFramework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
 
Example 5
Project: flume-release-1.7.0   File: StaticZooKeeperConfigurationProvider.java   View Source Code Vote up 6 votes
@Override
protected FlumeConfiguration getFlumeConfiguration() {
  try {
    CuratorFramework cf = createClient();
    cf.start();
    try {
      byte[] data = cf.getData().forPath(basePath + "/" + getAgentName());
      return configFromBytes(data);
    } finally {
      cf.close();
    }
  } catch (Exception e) {
    LOGGER.error("Error getting configuration info from Zookeeper", e);
    throw new FlumeException(e);
  }
}
 
Example 6
Project: dble   File: ZKUtils.java   View Source Code Vote up 6 votes
private static CuratorFramework createConnection() {
    String url = ZkConfig.getInstance().getZkURL();
    CuratorFramework framework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));
    // start connection
    framework.start();
    // wait 3 second to establish connect
    try {
        framework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (framework.getZookeeperClient().isConnected()) {
            LOGGER.info("CuratorFramework createConnection success");
            return framework;
        }
    } catch (InterruptedException ignored) {
        LOGGER.info("CuratorFramework createConnection error", ignored);
        Thread.currentThread().interrupt();
    }
    // fail situation
    framework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
 
Example 7
Project: leaf-snowflake   File: DistributedClusterStat.java   View Source Code Vote up 4 votes
public DistributedClusterStat(Map<Object,Object> _conf ) throws Exception
{
	this.conf = _conf;
	CuratorFramework _zk = mkZk();//创建Zookeeper连接及重试策略
	String path = String.valueOf(conf.get(Config.LEAF_ZOOKEEPER_ROOT));
	zkobj.mkdirs(_zk,path);// 创建一个永久目录
	_zk.close();

	active = new AtomicBoolean(true);



	watcher = new WatcherCallBack() {
		@Override
		public void execute(KeeperState state, EventType type, String path) {
			if ( active.get())
			{
				if(!(state.equals(KeeperState.SyncConnected)))
				{
					LOG.warn("Received event " + state + ": " + type + ": " + path + "with disconnected  from Zookeeper.");
					//System.out.println("Received event " + state + ":" + type + ":" + path + "with disconnected Zookeeper.");
				}
				else
				{
					LOG.info("Received event " + state + ":" + type + ":" + path);
					//System.out.println("Received event " + state + ":" + type + ":" + path);
					if(type.equals(EventType.NodeChildrenChanged)) //leaf 的临时node节点发生了变化(server上线或者下线)
					{
						LOG.info("Node childrens changed at path: " + path);
						//重新注册watcher事件
						try {
							List<String> children = get_childern(path,true);
							LOG.info("children list at path : " + path + " is " + children);
						} catch (Exception e)
						{
							LOG.warn("faild to get children in path: " + path,e);
						}
					}
				}

				if (!type.equals(EventType.None))
				{
					//System.out.println("Received event " + state + ":" + type + ":" + path);
					LOG.info("Received event " + state + ":" + type + ":" + path);
					for (Map.Entry<UUID,ClusterStateCallback> e: callbacks.entrySet())
					{
						ClusterStateCallback fn = e.getValue();
						fn.execute(type,path);
					}
				}

			}
		}
	};
	zk = null;
	try {
		zk = mkZk(watcher);
	}
	catch (Exception e)
	{
		LOG.error(e.getMessage(),e);
	}
}
 
Example 8
Project: stroom-stats   File: CuratorTest.java   View Source Code Vote up 3 votes
public static void main(String[] args) throws Exception {

        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);

        //use chroot so all subsequent paths are below /stroom-stats to avoid conflicts with hbase/zookeeper/kafka etc.
        CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181/stroom-stats", retryPolicy);
        client.start();

        Stat stat = client.checkExists().forPath("/");
        if (stat == null) {
            LOGGER.info("Creating root node");
            client.create().forPath("/");
        }

        Stat testNode = client.checkExists().creatingParentContainersIfNeeded().forPath("/my/test/path");

//        dumpChildren(client, "/");
//        dumpChildren(client, "/propertyService");

        checkTreeCache(client, "/propertyService");


//        client.create().forPath("/testNode", Bytes.toBytes("MyStringVal"));

        byte[] bVal = client.getData().forPath("/testNode");
        LOGGER.info(Bytes.toString(bVal));


        client.close();
    }