本文整理匯總了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);
}
}