本文整理汇总了Java中org.apache.zookeeper.WatchedEvent类的典型用法代码示例。如果您正苦于以下问题:Java WatchedEvent类的具体用法?Java WatchedEvent怎么用?Java WatchedEvent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WatchedEvent类属于org.apache.zookeeper包,在下文中一共展示了WatchedEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processStateChanged
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
private void processStateChanged(WatchedEvent event) {
logger.info("zookeeper state changed (" + event.getState() + ")");
setCurrentState(event.getState());
if (getShutdownTrigger()) {
return;
}
try {
fireStateChangedEvent(event.getState());
if (event.getState() == KeeperState.Expired) {
reconnect();
fireNewSessionEvents();
}
} catch (final Exception e) {
throw new RuntimeException("Exception while restarting zk client", e);
}
}
示例2: watchNode
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
private void watchNode(final ZooKeeper zk) {
try {
List<String> nodeList = zk.getChildren(Constant.ZK_REGISTRY_PATH, new Watcher() {
public void process(WatchedEvent event) {
if (event.getType() == Event.EventType.NodeChildrenChanged) {
watchNode(zk);
}
}
});
List<String> dataList = new ArrayList<>();
for (String node : nodeList) {
byte[] bytes = zk.getData(Constant.ZK_REGISTRY_PATH + "/" + node, false, null);
dataList.add(new String(bytes));
}
LOGGER.debug("node data: {}", dataList);
this.dataList = dataList;
LOGGER.debug("Service discovery triggered updating connected server node.");
updateConnectedServer();
} catch (Exception e) {
LOGGER.error("", e);
}
}
示例3: process
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
@Override
public void process(WatchedEvent event) {
ReplyHeader h = new ReplyHeader(-1, -1L, 0);
if (LOG.isTraceEnabled()) {
ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK,
"Deliver event " + event + " to 0x"
+ Long.toHexString(this.sessionId)
+ " through " + this);
}
// Convert WatchedEvent to a type that can be sent over the wire
WatcherEvent e = event.getWrapper();
try {
sendResponse(h, e, "notification");
} catch (IOException e1) {
if (LOG.isDebugEnabled()) {
LOG.debug("Problem sending to " + getRemoteSocketAddress(), e1);
}
close();
}
}
示例4: connectZooKeeper
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
private static ZooKeeper connectZooKeeper(String ensemble)
throws IOException, KeeperException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
ZooKeeper zkc = new ZooKeeper(HOSTPORT, ZK_SESSION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
latch.countDown();
}
}
});
if (!latch.await(ZK_SESSION_TIMEOUT, TimeUnit.MILLISECONDS)) {
throw new IOException("Zookeeper took too long to connect");
}
return zkc;
}
示例5: getZooKeeperConnection
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
/**
* 获取Zookeeper连接
* @return
*/
public static ZooKeeper getZooKeeperConnection() {
try {
// Zookeeper连接闭锁cdl
final CountDownLatch cdl = new CountDownLatch(1);
final ZooKeeper zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
// 当连接成功时放开cdl
cdl.countDown();
}
}
});
// cdl阻塞
cdl.await();
return zooKeeper;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例6: processDataOrChildChange
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
private void processDataOrChildChange(WatchedEvent event) {
final String path = event.getPath();
if (event.getType() == EventType.NodeChildrenChanged || event.getType() == EventType.NodeCreated || event.getType() == EventType.NodeDeleted) {
Set<IZkChildListener> childListeners = _childListener.get(path);
if (childListeners != null && !childListeners.isEmpty()) {
fireChildChangedEvents(path, childListeners);
}
}
if (event.getType() == EventType.NodeDataChanged || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated) {
Set<IZkDataListener> listeners = _dataListener.get(path);
if (listeners != null && !listeners.isEmpty()) {
fireDataChangedEvents(event.getPath(), listeners);
}
}
}
示例7: process
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
@Override
public void process(WatchedEvent event) {
try {
/* 重新注册节点 */
zkClient.exists(nodePath, this);
EventType eventType = event.getType();
switch (eventType) {
case NodeDeleted:
createNode();
break;
default:
break;
}
} catch (Exception e) {
log.error("error", e);
}
}
示例8: process
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
@Override
public void process(WatchedEvent event) {
ZooKeeper zkClient = zookeeperConnManager.getZkClient();
try {
/* 重新注册节点 */
zkClient.exists(nodePath, this);
EventType eventType = event.getType();
switch (eventType) {
case NodeDeleted:
election();
break;
default:
break;
}
} catch (Exception e) {
log.error("error", e);
}
}
示例9: process
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
@Override
public void process(WatchedEvent event) {
if (isDestroyed()) {
return;
}
if (!getZooKeeperConnection().isConnected()) {
return;
}
String eventPath = event.getPath();
EventType eventType = event.getType();
Znode znode = getData();
String path = znode.getPath();
if (path.equals(eventPath) && eventType.equals(EventType.NodeDeleted)) {
destroy();
return;
}
processUpdate(event);
fireDataModelDataChanged();
}
示例10: processWatchEvent
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
@Override
public synchronized void processWatchEvent(ZooKeeper zk,
WatchedEvent event) throws Exception {
if (forExpire) {
// a hack... couldn't find a way to trigger expired event.
WatchedEvent expriredEvent = new WatchedEvent(
Watcher.Event.EventType.None,
Watcher.Event.KeeperState.Expired, null);
super.processWatchEvent(zk, expriredEvent);
forExpire = false;
syncBarrier.await();
} else {
super.processWatchEvent(zk, event);
}
}
示例11: process
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
public void process(WatchedEvent event) {
if (!closed) {
try {
if (event.getType() != EventType.NodeDeleted) {
Stat s = zooKeeper.exists(nodePath, this);
if (s != null) {
zookeeper.getChildren(nodePath, this);
}
}
} catch (Exception e) {
LoggerFactory.getLogger().error(
"Error occured re-adding node watcherfor node "
+ nodePath, e);
}
nodeListener.processEvent(event.getPath(), event.getType()
.name(), null);
}
}
示例12: testRootWatchTriggered
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
@Test(timeout = 60000)
public void testRootWatchTriggered() throws Exception {
class MyWatcher implements Watcher{
boolean fired=false;
public void process(WatchedEvent event) {
if(event.getPath().equals("/"))
fired=true;
}
}
MyWatcher watcher=new MyWatcher();
// set a watch on the root node
dt.getChildren("/", new Stat(), watcher);
// add a new node, should trigger a watch
dt.createNode("/xyz", new byte[0], null, 0, dt.getNode("/").stat.getCversion()+1, 1, 1);
Assert.assertFalse("Root node watch not triggered",!watcher.fired);
}
示例13: processStateChanged
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
private void processStateChanged(WatchedEvent event) {
LOG.info("zookeeper state changed (" + event.getState() + ")");
setCurrentState(event.getState());
if (getShutdownTrigger()) {
return;
}
try {
fireStateChangedEvent(event.getState());
if (event.getState() == KeeperState.Expired) {
reconnect();
fireNewSessionEvents();
}
} catch (final Exception e) {
throw new RuntimeException("Exception while restarting zk client", e);
}
}
示例14: process
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
public void process(WatchedEvent event) {
if (!closed) {
try {
if (event.getType() != EventType.NodeDeleted) {
Stat s = zooKeeper.exists(nodePath, this);
if (s != null) {
zookeeper.getChildren(nodePath, this);
}
}
} catch (Exception e) {
LoggerFactory.getLogger().error(
"Error occurred re-adding node watcherfor node "
+ nodePath, e);
}
nodeListener.processEvent(event.getPath(), event.getType()
.name(), null);
}
}
示例15: process
import org.apache.zookeeper.WatchedEvent; //导入依赖的package包/类
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
if (latch != null) {
latch.countDown();
}
}
if (event.getType() == EventType.None) {
return;
}
try {
events.put(event);
} catch (InterruptedException e) {
Assert.assertTrue("interruption unexpected", false);
}
}