zookeeper的sessiontimeout 的测试

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.AsyncCallback.StatCallback;
import org.apache.zookeeper.Watcher.Event.KeeperState;


public class testZ implements Watcher{
???
???? protected CountDownLatch countDownLatch=new CountDownLatch(1);
???? ZooKeeper zooKeeper;
???? public void connect(String hosts) throws IOException, InterruptedException{?
??????????? zooKeeper = new ZooKeeper(hosts,4000,this);?
??????????? countDownLatch.await();?
????? }
????
???? public void process(WatchedEvent event) {
????????? // TODO Auto-generated method stub
???????? System.out.println("????????????");
????????? if(event.getState()==KeeperState.SyncConnected){
??????? System.out.print("链接上了!");
?????????? //countDownLatch.countDown();
????????? } else if (event.getState() == KeeperState.Disconnected) {
??????????????? System.out.print("失去链接了!");

??????????????? //System.out.println("[SUC-CORE] session expired. now rebuilding");

??????????????? //session expired, may be never happending.
??????????????? //close old client and rebuild new client
?????????????? /// close();

??????????????? //getZooKeeper();
??????????? } else if (event.getState() == KeeperState.Expired) {
??????????????? System.out.print("session exception了!");

??????????????? System.out.println("[SUC-CORE] session expired. now rebuilding");

??????????????? //session expired, may be never happending.
??????????????? //close old client and rebuild new client
??????????????? close();

??????????????? //getZooKeeper();
??????????? }
???????? }
????
???? /**
????? * 关闭zookeeper连接,释放资源
????? */
???? public? void close() {
???????? System.out.println("[SUC-CORE] close");
???????? if (zooKeeper != null) {
???????????? try {
???????????????? zooKeeper.close();
???????????????? zooKeeper = null;
???????????? } catch (InterruptedException e) {
???????????????? //ignore exception
???????????? }
???????? }
???? }
????
??? public static void main(String args[]){
???????
??????? testZ a = new testZ();
??????? try {
??????????? a.connect("192.168.105.157:2181");
??????? } catch (Exception e) {
??????????? // TODO Auto-generated catch block
??????????? e.printStackTrace();
??????? }?
???????
??? }


}

拔开网线,当有链接异常发生超过4秒时,再插网线链接上就会报session exception了!,4秒内插入网线链接上没有问题不会报session exception了!

如下:

CONNECTIONLOSS和SESSIONEXPIRED

因为网络环境等原因,这两个异常还是会时不时出现一下,需要对这些情况有所考虑。

ConnectionLoss表示认为该连接不可用,zk的java client会自动拿着当前的sessionId去其他的zkServer建立连接。

如果在SessionTimeout的时间之内重新建立连接成功,则一切恢复正常,临时节点和watcher不会发生丢失。

如果在SessionTimeout时间之后才连接上zkServer,则zkServer会返回Session Expired,这个时候前一个session不可用,临时节点和watcher丢失,需要手动去冲新进行初始化。

ps:Zk实例化的时候会传入一个SessionTimeOut的参数,zk服务端在接收到这个参数后会和本地配置minSessionTimeout(默认2*tickTime)和maxSessionTimeout(默认20*tickTime)进行比较并进行截取。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值