List of usage examples for org.apache.zookeeper Watcher Watcher
Watcher
From source file:sleet.utils.zookeeper.ZkMiniCluster.java
License:Apache License
public void startZooKeeper(final Properties properties, boolean format, String path, final boolean randomPort) { String realPath = path + "/zk_test"; properties.setProperty("dataDir", realPath); final ServerConfig serverConfig = new ServerConfig(); QuorumPeerConfig config = new QuorumPeerConfig() { @Override/* w w w . j a va 2 s. c o m*/ public InetSocketAddress getClientPortAddress() { InetSocketAddress clientPortAddress = super.getClientPortAddress(); if (randomPort) { return randomPort(clientPortAddress); } return clientPortAddress; } private InetSocketAddress randomPort(InetSocketAddress clientPortAddress) { return new InetSocketAddress(clientPortAddress.getAddress(), 0); } }; try { config.parseProperties(properties); } catch (IOException e) { LOG.error(e.toString()); throw new RuntimeException(e); } catch (ConfigException e) { LOG.error(e.toString()); throw new RuntimeException(e); } serverConfig.readFrom(config); rm(new File(realPath)); serverThread = new Thread(new Runnable() { @Override public void run() { try { zooKeeperServerMain = new ZooKeeperServerMainEmbedded(); zooKeeperServerMain.runFromConfig(serverConfig); } catch (IOException e) { LOG.error(e.toString()); } } }); serverThread.start(); long s = System.nanoTime(); while (s + 10000000000L > System.nanoTime()) { try { Thread.sleep(50); } catch (InterruptedException e) { LOG.error(e.toString()); throw new RuntimeException(e); } try { String zkConnectionString = getZkConnectionString(); if (zkConnectionString == null) { continue; } ZooKeeper zk = new ZooKeeper(getZkConnectionString(), 30000, new Watcher() { @Override public void process(WatchedEvent event) { } }); zk.close(); break; } catch (IOException e) { LOG.error(e.toString()); throw new RuntimeException(e); } catch (InterruptedException e) { LOG.error(e.toString()); throw new RuntimeException(e); } } }
From source file:yangqi.zookeeper.example.masterworker.ChildrenCallbackMonitor.java
License:Open Source License
/** * @param args//from w w w . j a va 2 s . c o m * @throws IOException * @throws InterruptedException */ public static void main(String[] args) throws IOException, InterruptedException { final ZooKeeper zookeeper = new ZooKeeper("localhost:2181", 2000, null); final ChildrenCallback callback = new ChildrenCallback() { @Override public void processResult(int rc, String path, Object ctx, List<String> children) { System.out.println(children); } }; Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("Event is " + event); if (event.getType() == EventType.NodeChildrenChanged) { System.out.println("Changed " + event); zookeeper.getChildren("/workers", this, callback, null); } } }; zookeeper.getChildren("/workers", watcher, callback, null); System.out.println("begin finish"); Thread.sleep(200000); System.out.println("finish"); }
From source file:yangqi.zookeeper.example.masterworker.SessionWatch.java
License:Open Source License
/** * @param args// www. ja v a 2 s . c o m * @throws IOException * @throws InterruptedException */ public static void main(String[] args) throws IOException, InterruptedException { ZooKeeper zookeeper = new ZooKeeper("localhost:2181", 2000, new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("Event is " + event); } }); Thread.sleep(200000); }
From source file:zookeeper.recipes.leader.LeaderLatch.java
License:Apache License
private void checkLeadership(List<String> children) throws Exception { List<String> sortedChildren = LockInternals.getSortedChildren(LOCK_NAME, sorter, children); int ourIndex = (ourPath != null) ? sortedChildren.indexOf(ZKPaths.getNodeFromPath(ourPath)) : -1; if (ourIndex < 0) { log.error("Can't find our node. Resetting. Index: " + ourIndex); reset();//w w w. j a va 2 s. com } else if (ourIndex == 0) { setLeadership(true); } else { String watchPath = sortedChildren.get(ourIndex - 1); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { if ((state.get() == State.STARTED) && (event.getType() == Event.EventType.NodeDeleted) && (ourPath != null)) { try { getChildren(); } catch (Exception ex) { log.error("An error occurred checking the leadership.", ex); } } } }; BackgroundCallback callback = new BackgroundCallback() { @Override public void processResult(CuratorFramework client, CuratorEvent event) throws Exception { if (event.getResultCode() == KeeperException.Code.NONODE.intValue()) { // previous node is gone - reset reset(); } } }; client.checkExists().usingWatcher(watcher).inBackground(callback) .forPath(ZKPaths.makePath(latchPath, watchPath)); } }