首页

关于java定义ZookeepClient和BaseZookeeper客户端基础类连接zookeeper服务端进行数据存储使用代码示例

标签:ZookeepClient,BaseZookeeper,zookeeper代码示例     发布时间:2018-11-29   

一、前言

关于java的ZookeepClient客户端连接类和BaseZookeeper基类,连接zookeeper服务端进行数据节点存取使用,详情下面代码示例。

二、代码示例

1. ZookeepClient客户端类

import java.io.IOException;@b@import java.util.Arrays;@b@import java.util.List;@b@@b@import org.apache.zookeeper.KeeperException;@b@import org.apache.zookeeper.data.Stat;@b@@b@public class ZookeepClient {@b@	@b@	public static void main(String[] args) throws IOException,@b@			InterruptedException, KeeperException {@b@		BaseZookeeper baseZookeeper = new BaseZookeeper();@b@@b@		String host = "192.168.1.199:2181";@b@@b@		baseZookeeper.connectZookeeper(host);@b@		System.out.println("--------connect zookeeper ok-----------");@b@@b@		byte[] data = { 1, 2, 3, 4, 5 };@b@@b@		System.out.println("--------create node ok-----------");@b@@b@		List<String> children = baseZookeeper.getChildren("/");@b@		for (String child : children) {@b@			System.out.println(child);@b@			System.out.println("--------get children ok-----------");@b@@b@			byte[] nodeData = baseZookeeper.getData("/master");@b@			System.out.println(new String(nodeData));@b@			System.out.println("--------get node data ok-----------");@b@@b@			data = "test".getBytes();@b@			Stat stat = baseZookeeper.exists("/master", false);@b@@b@			baseZookeeper.setData("/master", data, stat.getVersion());@b@			System.out.println("--------set node data ok-----------");@b@@b@			nodeData = baseZookeeper.getData("/master");@b@			System.out.println(Arrays.toString(nodeData));@b@			System.out.println("--------get node new data ok-----------");@b@			Thread.currentThread();@b@			Thread.sleep(60000L);@b@			baseZookeeper.closeConnect();@b@			System.out.println("--------close zookeeper ok-----------");@b@		}@b@	}@b@}

2.  BaseZookeeper客户端基类

import java.io.IOException;@b@import java.io.PrintStream;@b@import java.util.List;@b@import java.util.concurrent.CountDownLatch;@b@import org.apache.zookeeper.CreateMode;@b@import org.apache.zookeeper.KeeperException;@b@import org.apache.zookeeper.WatchedEvent;@b@import org.apache.zookeeper.Watcher;@b@import org.apache.zookeeper.Watcher.Event.KeeperState;@b@import org.apache.zookeeper.ZooDefs.Ids;@b@import org.apache.zookeeper.ZooKeeper;@b@import org.apache.zookeeper.data.Stat;@b@@b@public class BaseZookeeper implements Watcher {@b@	@b@	public ZooKeeper zooKeeper;@b@	private static final int SESSION_TIME_OUT = 2000;@b@	private CountDownLatch countDownLatch;@b@@b@	public BaseZookeeper() {@b@		this.countDownLatch = new CountDownLatch(1);@b@	}@b@@b@	public void connectZookeeper(String host) throws IOException,@b@			InterruptedException {@b@		this.zooKeeper = new ZooKeeper(host, 2000, this);@b@		this.countDownLatch.await();@b@		System.out.println("zookeeper connect ok");@b@	}@b@@b@	public void process(WatchedEvent event) {@b@		if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {@b@			System.out.println("watcher received event");@b@			this.countDownLatch.countDown();@b@		}@b@	}@b@@b@	public String createNode(String path, byte[] data) throws KeeperException,@b@			InterruptedException {@b@		return this.zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE,@b@				CreateMode.PERSISTENT);@b@	}@b@@b@	public List<String> getChildren(String path) throws KeeperException,@b@			InterruptedException {@b@		return this.zooKeeper.getChildren(path, false);@b@	}@b@@b@	public Stat setData(String path, byte[] data, int version)@b@			throws KeeperException, InterruptedException {@b@		return this.zooKeeper.setData(path, data, version);@b@	}@b@@b@	public byte[] getData(String path) throws KeeperException,@b@			InterruptedException {@b@		return this.zooKeeper.getData(path, false, null);@b@	}@b@@b@	public void deletNode(String path, int version)@b@			throws InterruptedException, KeeperException {@b@		this.zooKeeper.delete(path, version);@b@	}@b@@b@	public void closeConnect() throws InterruptedException {@b@		if (null != this.zooKeeper) {@b@			this.zooKeeper.close();@b@		}@b@	}@b@@b@	public Stat exists(String path, boolean tag) {@b@		Stat st = null;@b@		try {@b@			st = this.zooKeeper.exists(path, tag);@b@		} catch (KeeperException e) {@b@			e.printStackTrace();@b@		} catch (InterruptedException e) {@b@			e.printStackTrace();@b@		}@b@		return st;@b@	}@b@}
  • ◆ 相关内容