duan2

导航

 

创建会话

客户端可以通过创建一个Zookeeper实例来连接服务器。4种构造方法如下

ZooKeeper(connectString, sessionTimeout, watcher);

ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly)


 ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,long sessionId, byte[] sessionPasswd);

ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,long sessionId, byte[] sessionPasswd, boolean canBeReadOnly)

 

 

注意,zookeeper客户端和服务端会话建立是一个异步的过程,也是就是说在程序中构造zookeeper方法初始化方法执行完后

会立即返回,在大多数情况下此时并没有真正的建立一个可用的会话,此时会话正处于CONnectioning状态。当真正的会话建立后

服务端会向客户端发送一个通知,客户端在获取这个通知后会话才真正的建立。

package session;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState;

public class CreateZookeeper implements Watcher {
    private static CountDownLatch connectedSemaphore = new CountDownLatch(1);
    
    @Override
    public void process(WatchedEvent event) {
        System.out.println("receive watched event:"+event);
        if (KeeperState.SyncConnected==event.getState()) {
            connectedSemaphore.countDown();
        }
        
    }
    public static void main(String[] args) throws IOException {
        ZooKeeper zooKeeper = new ZooKeeper("192.168.64.60", 5000, new CreateZookeeper());
        
        System.out.println(zooKeeper.getState());
        
        try {
            connectedSemaphore.await();
        } catch (InterruptedException e) {}
            System.out.println("zookeeper session established");
        
        
    }
    

}

程序运行结果如下:

CreateZookeeper类实现了Watcher接口重写了process方法,该方法负责处理来自zookeeper服务端的通知。在

收到来自服务端的synconncted事件后,解除了主程序中的CountDownLatch等待阻塞。至此客户端会话创建完成。

 

 创建一个复用sessionId和sessionPasswd的实例

package session;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

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

public class CreateZookeeper2 implements Watcher {
public static CountDownLatch connectedSemaphore = new CountDownLatch(1);

@Override
public void process(WatchedEvent event) {
    System.out.println("receive watched event:"+event);
    if (KeeperState.SyncConnected==event.getState()) {
        connectedSemaphore.countDown();
    }
    
}

public static void main(String[] args) throws IOException, InterruptedException {
    ZooKeeper zooKeeper = new ZooKeeper("192.168.64.60", 5000, new CreateZookeeper());
    connectedSemaphore.await();
    long sessionId = zooKeeper.getSessionId();
    byte[] sessionPasswd = zooKeeper.getSessionPasswd();
    
    //复用sessionId和sessionPasswd
     zooKeeper = new ZooKeeper("192.168.64.60", 5000, new CreateZookeeper(),1l,"test".getBytes());
     
     zooKeeper = new ZooKeeper("192.168.64.60", 5000, new CreateZookeeper(),sessionId,sessionPasswd);
     
     Thread.sleep(Integer.MAX_VALUE);

}

}

我们可以看出,第一次使用错误的id和pw服务端返回“EXpired”事件,而第二次使用正确的id和pw则重新连接上。

posted on 2018-05-20 16:27  duan2  阅读(194)  评论(0编辑  收藏  举报