zookeeper入門code

1805 ワード

いくつかのテスト:
	String path = "/tlog2/test/fuck";
		boolean exists = zkClient.exists(path);

		CuratorFramework zkClient = CuratorFrameworkFactory.newClient(
				"10.232.31.216:3325,10.232.31.215:3325,10.232.31.219:3325", new RetryPolicy() {

					@Override
					public boolean allowRetry(int retryCount, long elapsedTimeMs) {
						return false;
					}

				});

		Watcher watcher = new Watcher() {

			@Override
			public void process(WatchedEvent event) {
				System.out.println(event.getType());
				//client.checkExists().usingWatcher(watcher).inBackground(backgroundCallback).forPath(path)
			}
		};

		BackgroundCallback callback = new BackgroundCallback() {

			@Override
			public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {

				switch (event.getType()) {
				case GET_DATA: {
					if (event.getResultCode() == KeeperException.Code.OK.intValue()) {
						//logger.trace("GET_DATA: {}", event);
						//setNewData(event.getData());
						System.out.println("GET_DATA: "+ event);
					}
					break;
				}

				case EXISTS: {
					if (event.getResultCode() == KeeperException.Code.NONODE.intValue()) {
						//logger.trace("EXISTS=>NONODE: {}", event);
						System.out.println("EXISTS=>NONODE: "+ event);
						//setNewData(null);
					} else if (event.getResultCode() == KeeperException.Code.OK.intValue()) {
						System.out.println("EXISTS=>OK: "+event);
						//client.getData().inBackground(backgroundCallback).forPath(path);
					}
					break;
				}

				default:
					System.out.println("Unknown CuratorEvent: " + event.getType());
					break;
				}
			}

		};

		zkClient.checkExists().usingWatcher(watcher).inBackground(callback).forPath(path);