List of usage examples for org.apache.zookeeper WatchedEvent getState
public KeeperState getState()
From source file:com.edmunds.zookeeper.election.ZooKeeperElection.java
License:Apache License
/** * Called when the previous node dies, but we still need to check because the previous node may not be the master. */// w w w .ja v a 2 s. c o m void processPreviousWatcher(ZooKeeperElectionContext ctx, WatchedEvent event) { // Are we still connected? final KeeperState connectionState = event.getState(); if (connectionState == KeeperState.SyncConnected) { // Note: I am expecting a NodeDeleted event here. // But, I need to re-establish the watch in all circumstances anyway. // We are probably the master, but we need to confirm in case the previous node was not the master. listChildren(ctx); } else { error("Connection lost - Previous watch: " + connectionState, Code.CONNECTIONLOSS, "/", ctx); // Can't do anything so just withdraw. withdrawInternal(ctx, "Previous Node Connection Lost"); } }
From source file:com.edmunds.zookeeper.treewatcher.ZooKeeperTreeWatcher.java
License:Apache License
@Override public void process(WatchedEvent event) { if (event.getState() != Watcher.Event.KeeperState.SyncConnected) { unexpectedEventError(event);//from w w w.j ava 2 s .co m return; } processEvent(event, getCurrentState()); checkStateConsistent(); }
From source file:com.ery.estorm.zk.ZooKeeperWatcher.java
License:Apache License
/** * Method called from ZooKeeper for events and connection status. * <p>/*from www .ja v a 2 s . c om*/ * Valid events are passed along to listeners. Connection status changes are * dealt with locally. */ @Override public void process(WatchedEvent event) { String path = event.getPath(); if (path != null || EventType.NodeDeleted == event.getType()) { try { ZKUtil.watchAndCheckExists(this, path); } catch (KeeperException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } LOG.debug(prefix("Received ZooKeeper Event, " + "type=" + event.getType() + ", " + "state=" + event.getState() + ", " + "path=" + path)); switch (event.getType()) { // If event type is NONE, this is a connection status change case None: { connectionEvent(event); break; } // Otherwise pass along to the listeners case NodeCreated: { for (ZooKeeperListener listener : listeners) { listener.nodeCreated(path); } break; } case NodeDeleted: { for (ZooKeeperListener listener : listeners) { listener.nodeDeleted(path); } break; } case NodeDataChanged: { for (ZooKeeperListener listener : listeners) { listener.nodeDataChanged(path); } break; } case NodeChildrenChanged: { for (ZooKeeperListener listener : listeners) { listener.nodeChildrenChanged(path); } break; } } }
From source file:com.ery.estorm.zk.ZooKeeperWatcher.java
License:Apache License
/** * Called when there is a connection-related event via the Watcher callback. * <p>/*from w w w . j a v a 2 s . c o m*/ * If Disconnected or Expired, this should shutdown the cluster. But, since * we send a KeeperException.SessionExpiredException along with the abort * call, it's possible for the Abortable to catch it and try to create a new * session with ZooKeeper. This is what the client does in HCM. * <p> * * @param event */ private void connectionEvent(WatchedEvent event) { switch (event.getState()) { case SyncConnected: // Now, this callback can be invoked before the this.zookeeper is // set. // Wait a little while. long finished = System.currentTimeMillis() + this.conf.getLong("hbase.zookeeper.watcher.sync.connected.wait", 2000); while (System.currentTimeMillis() < finished) { Threads.sleep(1); if (this.recoverableZooKeeper != null) break; } if (this.recoverableZooKeeper == null) { LOG.error( "ZK is null on connection event -- see stack trace " + "for the stack trace when constructor was called on this zkw", this.constructorCaller); throw new NullPointerException("ZK is null"); } this.identifier = this.identifier + "-0x" + Long.toHexString(this.recoverableZooKeeper.getSessionId()); // Update our identifier. Otherwise ignore. LOG.debug(this.identifier + " connected"); break; // Abort the server if Disconnected or Expired case Disconnected: LOG.debug(prefix("Received Disconnected from ZooKeeper, ignoring")); break; case Expired: String msg = prefix(this.identifier + " received expired from " + "ZooKeeper, aborting"); // TODO: One thought is to add call to ZooKeeperListener so say, // ZooKeeperNodeTracker can zero out its data values. if (this.abortable != null) { this.abortable.abort(msg, new KeeperException.SessionExpiredException()); } break; case ConnectedReadOnly: case SaslAuthenticated: break; default: throw new IllegalStateException("Received event is not valid: " + event.getState()); } }
From source file:com.example.directorio.ZooKeeperConnection.java
public void connect(String host) throws IOException, InterruptedException { zoo = new ZooKeeper(host, 5000, new Watcher() { public void process(WatchedEvent we) { if (null != we.getState()) switch (we.getState()) { case SyncConnected: connectedSignal.countDown(); break; case ConnectedReadOnly: System.out.println("Estado solo lectura"); break; case AuthFailed: System.out.println("Estado Autorizacin fallida"); break; case Disconnected: System.out.println("Estado Desconectado"); break; case Expired: System.out.println("Estado expir la sesin"); break; default: break; }// w w w. jav a 2s. com } }); connectedSignal.await(); }
From source file:com.example.directorio.ZooKeeperMannager.java
public String getData(String path2) throws InterruptedException, KeeperException, UnsupportedEncodingException { final String path = path2; Stat stat = znode_exists(path);//from w w w . j a v a 2s .c om byte[] b = null; if (stat != null) { b = zk.getData(path, new Watcher() { public void process(WatchedEvent we) { if (we.getType() == Event.EventType.None) { switch (we.getState()) { case Expired: zkConnection.getConnectedSignal().countDown(); break; } } else { try { byte[] bn = zk.getData(path, false, null); String data = new String(bn, "UTF-8"); System.out.println(data); zkConnection.getConnectedSignal().countDown(); } catch (Exception ex) { ex.printStackTrace(); } } } }, null); String data = new String(b, "UTF-8"); //System.out.println(data); zkConnection.getConnectedSignal().await(); } else { System.out.println("Node does not exists"); } return new String(b); }
From source file:com.facebook.zookeeper.convenience.ZkQuickConnection.java
License:Apache License
private ZkQuickConnection(ZooKeeperFactory zooKeeperFactory, int connectionTimeoutMillis) throws IOException, InterruptedException, TimeoutException { final CountDownLatch latch = new CountDownLatch(1); zk = zooKeeperFactory.create(new Watcher() { @Override/*from w w w. j a va 2 s . com*/ public void process(WatchedEvent event) { if (event.getState() == Watcher.Event.KeeperState.SyncConnected) { latch.countDown(); } } }); if (!latch.await(connectionTimeoutMillis, TimeUnit.MILLISECONDS)) { zk.close(); throw new TimeoutException("Failed to connect to ZooKeeper"); } }
From source file:com.fjn.helper.frameworkex.apache.zookeeper.dispatcher.Dispatcher.java
License:Apache License
@Override public void process(WatchedEvent event) { event.getPath(); event.getState(); event.getType(); }
From source file:com.fjn.helper.frameworkex.apache.zookeeper.dispatcher.Dispatcher.java
License:Apache License
void accept(WatchedEvent event) { boolean stateChanged = event.getPath() == null; boolean znodeChanged = event.getPath() != null; boolean dataChanged = event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated || event.getType() == EventType.NodeChildrenChanged; if (stateChanged) { if (event.getState() == KeeperState.Expired) { selector.addEvent(new Event(ServiceType.SESSION, new SourceWrapper(event))); }/*from w w w. j ava 2s .co m*/ } if (znodeChanged) { } if (dataChanged) { } }
From source file:com.github.liyp.zk.demo.ZkElector.java
License:Apache License
@Override public void process(WatchedEvent event) { logger.info("ZK path ,state , type: {}", event.getPath() + " " + event.getState() + " " + event.getType() + " #" + zookeeper.hashCode()); switch (event.getType()) { case NodeChildrenChanged: case NodeCreated: case NodeDataChanged: break;//from ww w . ja v a 2s. c om case NodeDeleted: try { newLeaderElection(); } catch (KeeperException | InterruptedException e) { logger.error("New leader election failed.", e); } break; case None: switch (event.getState()) { case Disconnected: logger.info("ZK path {} Disconnected.", event.getPath()); break; case Expired: logger.info("ZK path {} Expired.", event.getPath()); taskServer.shutdown(); try { this.zookeeper = new ZooKeeper(zkAddress, zkTimeout, this); elected.set(true); } catch (IOException e) { logger.info("{}", this.hashCode(), e); } break; case SyncConnected: logger.info("ZK path {} SyncConnected.", event.getPath()); try { if (elected.get()) { leaderElection(); elected.set(false); } } catch (KeeperException | InterruptedException e) { logger.error("ZK path rebuild error.", e); } catch (Exception e) { logger.info("{}", this.hashCode(), e); } break; default: logger.info("ZK path {}, state {}", event.getPath(), event.getState()); break; } break; } }