Example usage for org.apache.zookeeper WatchedEvent getType

List of usage examples for org.apache.zookeeper WatchedEvent getType

Introduction

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

Prototype

public EventType getType() 

Source Link

Usage

From source file:com.haoocai.jscheduler.core.monitor.TaskExecutionMonitor.java

License:Apache License

@Override
public void process(WatchedEvent watchedEvent) {
    watch();//from   ww w  .  j  a v  a2  s .  c o m
    if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
        byte[] doneSchedulerUnitBytes = zkAccessor.getData(watchedEvent.getPath());
        updateDoneInfoLock.lock();
        recvDoneSchedulerUnit = new SchedulerUnit(new String(doneSchedulerUnitBytes));
        recvDonePoint = System.currentTimeMillis();
        updateDoneInfoLock.unlock();
    }
}

From source file:com.jkoolcloud.tnt4j.streams.configure.zookeeper.ZKConfigManager.java

License:Apache License

/**
 * Initializes ZK ensemble node data monitoring (over {@link org.apache.zookeeper.Watcher}) and initial data
 * loading./*from w  w w .j a va2s. co m*/
 * 
 * @param zk
 *            ZooKeeper instance
 * @param path
 *            node path
 * @param zkCfgChangeListener
 *            zookeeper node data change listener instance
 */
public static void handleZKStoredConfiguration(final ZooKeeper zk, final String path,
        final ZKConfigChangeListener zkCfgChangeListener) {
    Watcher watch = new Watcher() {
        @Override
        public void process(WatchedEvent watchedEvent) {
            if (path.equals(watchedEvent.getPath())) {
                if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
                    LOGGER.log(OpLevel.DEBUG, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME,
                            "ZKConfigManager.node.changed"), path);
                    zkCfgChangeListener.reconfigure(zk, path, this);
                } else if (watchedEvent.getType() == Event.EventType.NodeDeleted) {
                    LOGGER.log(OpLevel.DEBUG, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME,
                            "ZKConfigManager.node.deleted"), path);
                    zk.exists(path, this, null, null);
                } else if (watchedEvent.getType() == Event.EventType.NodeCreated) {
                    LOGGER.log(OpLevel.DEBUG, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME,
                            "ZKConfigManager.node.created"), path);
                    zkCfgChangeListener.reconfigure(zk, path, this);
                }
            }
        }
    };

    Stat nStat = null;

    try {
        nStat = zk.exists(path, false);
    } catch (Exception exc) {
        LOGGER.log(OpLevel.WARNING, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME,
                "ZKConfigManager.node.exists.failed"), path, exc);
    }

    if (nStat == null) {
        LOGGER.log(OpLevel.DEBUG, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME,
                "ZKConfigManager.node.create.wait"), path);
        zk.exists(path, watch, null, null);
    } else {
        zkCfgChangeListener.reconfigure(zk, path, watch);
    }
}

From source file:com.jointhegrid.ironcount.manager.WorkerThread.java

License:Apache License

@Override
public void process(WatchedEvent we) {
    logger.debug(we);/*  w  w w  . ja v  a 2 s.  c  o  m*/
    if (we.getType() == EventType.NodeDataChanged) {
        if (we.getPath().equals("/ironcount/workloads/" + this.workload.name)) {
            logger.debug("change detected " + we);
            try {
                Stat s = zk.exists("/ironcount/workloads/" + this.workload.name, false);
                byte[] dat = zk.getData("/ironcount/workloads/" + this.workload.name, false, s);
                Workload w = this.m.deserializeWorkload(dat);
                if (w.active.equals(Boolean.FALSE)) {
                    this.goOn = false;
                    this.executor.shutdown();
                    logger.debug("Shutdown");
                }
            } catch (KeeperException ex) {
                logger.error(ex);
                throw new RuntimeException(ex);
            } catch (InterruptedException ex) {
                logger.error(ex);
                throw new RuntimeException(ex);
            }
        }
    }
}

From source file:com.jointhegrid.ironcount.manager.WorkloadManager.java

License:Apache License

@Override
public void process(WatchedEvent we) {
    logger.debug(we);/*from w ww.  j  a  va2s  . com*/
    if (we.getType() == we.getType().NodeCreated) {
        try {
            if (we.getPath().equals("/ironcount/workloads")) {
                List<String> children = zk.getChildren("/ironcount/workloads", this);
                considerStarting(children);
            }
        } catch (KeeperException ex) {
            throw new RuntimeException(ex);
        } catch (InterruptedException ex) {
            throw new RuntimeException(ex);
        }
    }
    if (we.getType() == we.getType().NodeDeleted) {
        if (we.getPath().startsWith("/ironcount/workloads")) {
            stopWorkerThreadIfRunning(we.getPath());
        }
    }
    if (we.getType() == we.getType().NodeChildrenChanged) {
        if (we.getPath().equals("/ironcount/workloads")) {
            try {
                //new workloads have been added NOT DELETED
                List<String> children = zk.getChildren("/ironcount/workloads", this);
                considerStarting(children);

            } catch (KeeperException ex) {
                throw new RuntimeException(ex);
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}

From source file:com.jxt.web.cluster.zookeeper.ZookeeperClusterDataManager.java

License:Apache License

@SuppressWarnings("deprecation")
@Override/*from  w  ww  . jav  a 2  s .  c o  m*/
public void process(WatchedEvent event) {
    logger.info("Handle Zookeeper Event({}) started.", event);

    KeeperState state = event.getState();
    EventType eventType = event.getType();
    String path = event.getPath();

    // when this happens, ephemeral node disappears
    // reconnects automatically, and process gets notified for all events
    boolean result = false;
    if (ZookeeperUtils.isDisconnectedEvent(event)) {
        result = handleDisconnected();
        if (state == KeeperState.Expired) {
            client.reconnectWhenSessionExpired();
        }
    } else if (state == KeeperState.SyncConnected || state == KeeperState.NoSyncConnected) {
        if (eventType == EventType.None) {
            result = handleConnected();
        } else if (eventType == EventType.NodeChildrenChanged) {
            result = handleNodeChildrenChanged(path);
        } else if (eventType == EventType.NodeDeleted) {
            result = handleNodeDeleted(path);
        } else if (eventType == EventType.NodeDataChanged) {
            result = handleNodeDataChanged(path);
        }
    }

    if (result) {
        logger.info("Handle Zookeeper Event({}) completed.", event);
    } else {
        logger.info("Handle Zookeeper Event({}) failed.", event);
    }
}

From source file:com.jxt.web.cluster.zookeeper.ZookeeperUtils.java

License:Apache License

public static boolean isConnectedEvent(WatchedEvent event) {
    KeeperState state = event.getState();
    EventType eventType = event.getType();

    return isConnectedEvent(state, eventType);
}

From source file:com.jxt.web.cluster.zookeeper.ZookeeperUtils.java

License:Apache License

public static boolean isDisconnectedEvent(WatchedEvent event) {
    KeeperState state = event.getState();
    EventType eventType = event.getType();

    return isDisconnectedEvent(state, eventType);
}

From source file:com.lin.stride.zk.election.LeaderElectionSupport.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    if (event.getType().equals(Watcher.Event.EventType.NodeDeleted)) {
        if (!event.getPath().equals(leaderOffer.getNodePath()) && state != State.STOP) {
            logger.debug("Node {} deleted. Need to run through the election process.", event.getPath());
            try {
                determineElectionStatus();
            } catch (KeeperException e) {
                becomeFailed(e);// ww  w  .  j ava  2  s .c  o  m
            } catch (InterruptedException e) {
                becomeFailed(e);
            }
        }
    }
}

From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java

License:Apache License

@Test
public void testSymlinkWithExistWatch() throws InterruptedException, ExecutionException {
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AsyncCallback.StatCallback existCallback = new AsyncCallback.StatCallback() {
        @Override// ww  w  .  j ava2 s. c o m
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            KeeperException.Code result = KeeperException.Code.get(rc);
            Assert.assertEquals(result, KeeperException.Code.OK);
            latch.countDown();
        }
    };
    Watcher existWatch = new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            Assert.assertEquals(event.getType(), Event.EventType.NodeCreated);
            _zkClient.getZooKeeper().exists(event.getPath(), null, existCallback, null);
        }
    };
    AsyncCallback.StatCallback existCallback2 = new AsyncCallback.StatCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            KeeperException.Code result = KeeperException.Code.get(rc);
            Assert.assertEquals(result, KeeperException.Code.NONODE);
            latch2.countDown();
        }
    };
    // symlink: /foo/$link/newNode -> /foo/bar/newNode
    _zkClient.getZooKeeper().exists("/foo/$link/newNode", existWatch, existCallback2, null);
    latch2.await(30, TimeUnit.SECONDS);
    _zkClient.ensurePersistentNodeExists("/foo/bar/newNode", new FutureCallback<None>());
    latch.await(30, TimeUnit.SECONDS);
    _zkClient.removeNodeUnsafe("/foo/bar/newNode", new FutureCallback<None>());
}

From source file:com.linkedin.d2.discovery.stores.zk.SymlinkAwareZooKeeperTest.java

License:Apache License

@Test
public void testSymlinkWithExistWatch2() throws InterruptedException, ExecutionException {
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AsyncCallback.StatCallback existCallback = new AsyncCallback.StatCallback() {
        @Override//from  w w w.  j  a v a 2s .  c o  m
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            KeeperException.Code result = KeeperException.Code.get(rc);
            Assert.assertEquals(result, KeeperException.Code.OK);
            latch.countDown();
        }
    };
    Watcher existWatch = new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            Assert.assertEquals(event.getType(), Event.EventType.NodeDataChanged);
            _zkClient.getZooKeeper().exists(event.getPath(), null, existCallback, null);
        }
    };
    AsyncCallback.StatCallback existCallback2 = new AsyncCallback.StatCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            KeeperException.Code result = KeeperException.Code.get(rc);
            Assert.assertEquals(result, KeeperException.Code.NONODE);
            latch2.countDown();
        }
    };
    // symlink: /foo/$link/foo -> /foo/bar/foo, which doesn't exist
    _zkClient.getZooKeeper().exists("/foo/$link/foo", existWatch, existCallback2, null);
    latch2.await(30, TimeUnit.SECONDS);
    // update symlink. now it points to /bar/foo, which does exist.
    _zkClient.setSymlinkData("/foo/$link", "/bar", new FutureCallback<None>());
    latch.await(30, TimeUnit.SECONDS);
    // restore symlink
    _zkClient.setSymlinkData("/foo/$link", "/foo/bar", new FutureCallback<None>());
}