You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@zookeeper.apache.org by Edward Capriolo <ed...@gmail.com> on 2015/01/29 02:27:31 UTC

session establishment

http://zookeeper.apache.org/doc/r3.4.5/api/org/apache/zookeeper/ZooKeeper.html

Session establishment is asynchronous. This constructor will initiate
connection to the server and return immediately - potentially (usually)
before the session is fully established. The watcher argument specifies the
watcher that will be notified of any changes in state.


>>This notification can come at any point before or after the constructor
call has returned.

public class DummyWatcher implements Watcher {

  private CountDownLatch awaitConnection;

  public DummyWatcher(){
    awaitConnection = new CountDownLatch(1);
  }

  public boolean connectOrThrow(long timeout, TimeUnit unit) throws
InterruptedException{
    return awaitConnection.await(timeout, unit);
  }

  @Override
  public void process(WatchedEvent event) {
    if (event.getState() == KeeperState.SyncConnected){
      awaitConnection.countDown();
    }
  }

}

Does this mean that the following event might never fire or does it always
fire?

RE: session establishment

Posted by Rakesh R <ra...@huawei.com>.
Hi,

Yes, connection establishment is asynchronous.
ZooKeeper client will take the watcher reference. Watchers are notified when any state change occurs in the client.

Following are few of the watch notification cases,

Case-1) First time connection establishment. Now after successful connect establishment, will be notified with 'SyncConnected' event.

Case-2) Server is not available during first time connection establishment, watcher will not receive any events.
In that case, your 'awaitConnection.await(timeout, unit);' will be timed out.

Case-3) The session got established and then disconnected. Now, the watcher will be notified with 'Disconnected' event.


Below are the different types of connection state change notifications:
	-	SyncConnected
	-	AuthFailed
	-	ConnectedReadOnly
	-	SaslAuthenticated
	-	Expired

Regards,
Rakesh
-----Original Message-----
From: Edward Capriolo [mailto:edlinuxguru@gmail.com] 
Sent: 29 January 2015 06:58
To: user@zookeeper.apache.org
Subject: session establishment

http://zookeeper.apache.org/doc/r3.4.5/api/org/apache/zookeeper/ZooKeeper.html

Session establishment is asynchronous. This constructor will initiate connection to the server and return immediately - potentially (usually) before the session is fully established. The watcher argument specifies the watcher that will be notified of any changes in state.


>>This notification can come at any point before or after the 
>>constructor
call has returned.

public class DummyWatcher implements Watcher {

  private CountDownLatch awaitConnection;

  public DummyWatcher(){
    awaitConnection = new CountDownLatch(1);
  }

  public boolean connectOrThrow(long timeout, TimeUnit unit) throws InterruptedException{
    return awaitConnection.await(timeout, unit);
  }

  @Override
  public void process(WatchedEvent event) {
    if (event.getState() == KeeperState.SyncConnected){
      awaitConnection.countDown();
    }
  }

}

Does this mean that the following event might never fire or does it always fire?