本文整理汇总了Java中org.apache.zookeeper.Watcher.Event.KeeperState类的典型用法代码示例。如果您正苦于以下问题:Java KeeperState类的具体用法?Java KeeperState怎么用?Java KeeperState使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
KeeperState类属于org.apache.zookeeper.Watcher.Event包,在下文中一共展示了KeeperState类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processStateChanged
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的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: ZkclientZookeeperClient
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的package包/类
public ZkclientZookeeperClient(URL url) {
super(url);
client = new ZkClient(url.getBackupAddress());
client.subscribeStateChanges(new IZkStateListener() {
public void handleStateChanged(KeeperState state) throws Exception {
ZkclientZookeeperClient.this.state = state;
if (state == KeeperState.Disconnected) {
stateChanged(StateListener.DISCONNECTED);
} else if (state == KeeperState.SyncConnected) {
stateChanged(StateListener.CONNECTED);
}
}
public void handleNewSession() throws Exception {
stateChanged(StateListener.RECONNECTED);
}
});
}
示例3: init
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的package包/类
/**
* 连接zookeeper
*
* @author gaoxianglong
*/
public void init() {
try {
zkClient = new ZooKeeper(zkAddress, zkSessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
KeeperState state = event.getState();
switch (state) {
case SyncConnected:
countDownLatch.countDown();
logger.info("connection zookeeper success");
break;
case Disconnected:
logger.warn("zookeeper connection is disconnected");
break;
case Expired:
logger.error("zookeeper session expired");
break;
case AuthFailed:
logger.error("authentication failure");
break;
default:
break;
}
}
});
countDownLatch.await();
} catch (Exception e) {
logger.error("error", e);
}
}
示例4: processStateChanged
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的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);
}
}
示例5: process
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的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);
}
}
示例6: ZkclientZookeeperClient
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的package包/类
public ZkclientZookeeperClient(URL url) {
super(url);
client = new ZkClient(
url.getBackupAddress(),
url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT),
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_REGISTRY_CONNECT_TIMEOUT));
client.subscribeStateChanges(new IZkStateListener() {
public void handleStateChanged(KeeperState state) throws Exception {
ZkclientZookeeperClient.this.state = state;
if (state == KeeperState.Disconnected) {
stateChanged(StateListener.DISCONNECTED);
} else if (state == KeeperState.SyncConnected) {
stateChanged(StateListener.CONNECTED);
}
}
public void handleNewSession() throws Exception {
stateChanged(StateListener.RECONNECTED);
}
});
}
示例7: subscribeStateChanges
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的package包/类
private static void subscribeStateChanges() {
getZkClient().subscribeStateChanges(new IZkStateListener() {
public void handleStateChanged(KeeperState state) throws Exception {
if(state == null) {
ZkClientFactory.LOGGER.info("ZOOKEEPER连接状态改变");
} else {
ZkClientFactory.LOGGER.info("ZOOKEEPER连接状态改变为" + state.name());
}
}
public void handleNewSession() throws Exception {
ZkClientFactory.LOGGER.info("SESSIONTIMEOUT,ZOOKEEPER重连,产生新SESSION");
}
});
}
示例8: init
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的package包/类
@Override
public void init() {
this.zkClient = new ZkClient(this.zkAddress, this.zkSessionTimeOut, this.zkConnectionTimeOut, new SerializableSerializer());
initRootPath();
this.zkClient.subscribeStateChanges(new IZkStateListener() {
@Override
public void handleStateChanged(KeeperState state) throws Exception {
if(zkReconnectionListener != null && state.name().equals(KeeperState.SyncConnected.name())){
zkReconnectionListener.handleStateForSyncConnected();
}
}
@Override
public void handleSessionEstablishmentError(Throwable error)throws Exception {
log.error("处理会话建立错误:{}",error);
}
@Override
public void handleNewSession() throws Exception {
log.info("会话建立成功!");
}
});
}
示例9: processStateChanged
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的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);
}
}
示例10: waitForKeeperState
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的package包/类
public boolean waitForKeeperState(KeeperState keeperState, long time, TimeUnit timeUnit)
throws ZkInterruptedException {
if (_zookeeperEventThread != null && Thread.currentThread() == _zookeeperEventThread) {
throw new IllegalArgumentException("Must not be done in the zookeeper event thread.");
}
Date timeout = new Date(System.currentTimeMillis() + timeUnit.toMillis(time));
LOG.debug("Waiting for keeper state " + keeperState);
acquireEventLock();
try {
boolean stillWaiting = true;
while (_currentState != keeperState) {
if (!stillWaiting) {
return false;
}
stillWaiting = getEventLock().getStateChangedCondition().awaitUntil(timeout);
}
LOG.debug("State is " + _currentState);
return true;
} catch (InterruptedException e) {
throw new ZkInterruptedException(e);
} finally {
getEventLock().unlock();
}
}
示例11: waitForKeeperState
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的package包/类
public boolean waitForKeeperState(KeeperState keeperState, long time,
TimeUnit timeUnit) {
Date timeout = new Date(System.currentTimeMillis()
+ timeUnit.toMillis(time));
logger.debug("Waiting for keeper state " + keeperState);
acquireEventLock();
try {
boolean stillWaiting = true;
while (_currentState != keeperState) {
if (!stillWaiting) {
return false;
}
stillWaiting = getEventLock().getStateChangedCondition()
.awaitUntil(timeout);
}
logger.debug("State is " + _currentState);
return true;
} catch (InterruptedException e) {
throw new RuntimeException("error when conn");
} finally {
getEventLock().unlock();
}
}
示例12: process
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的package包/类
synchronized public void process(WatchedEvent event) {
KeeperState state = event.getState();
if (state == KeeperState.SyncConnected) {
connected = true;
syncConnected = true;
readOnlyConnected = false;
} else if (state == KeeperState.ConnectedReadOnly) {
connected = true;
syncConnected = false;
readOnlyConnected = true;
} else {
connected = false;
syncConnected = false;
readOnlyConnected = false;
}
notifyAll();
if (connected) {
clientConnected.countDown();
}
}
示例13: process
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的package包/类
@Override
public synchronized void process(WatchedEvent event) {
if (event.getState() == KeeperState.AuthFailed) {
authFailed.countDown();
}
else {
super.process(event);
}
}
示例14: fireStateChangedEvent
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的package包/类
private void fireStateChangedEvent(final KeeperState state) {
for (final IZkStateListener stateListener : _stateListener) {
_eventThread.send(new ZkEvent("State changed to " + state + " sent to " + stateListener) {
@Override
public void run() throws Exception {
stateListener.handleStateChanged(state);
}
});
}
}
示例15: sessionEvent
import org.apache.zookeeper.Watcher.Event.KeeperState; //导入依赖的package包/类
/**
* zookeeper 链接事件监听
*
* @param connectionLatch 同步锁
* @param event 事件
*/
private void sessionEvent(CountDownLatch connectionLatch, WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
log.info("收到ZK连接成功事件!");
connectionLatch.countDown();
} else if (event.getState() == KeeperState.Expired) {
log.error("会话超时,等待重新建立ZK连接...");
try {
reConnection();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
} // Disconnected:Zookeeper会自动处理Disconnected状态重连
}