Create a session
The client can connect to the server by creating an zookeeper instance. 4 methods of construction are as follows
int Boolean int sessiontimeout, Watcher Watcher,longbyteint Sessiontimeout, Watcher Watcher, Long byte boolean canbereadonly)
Note that zookeeper client and server-side session establishment is an asynchronous process, which means that after the initialization method of the Zookeeper method is constructed in the program,
will return immediately, and in most cases there is no real time to establish an available session at which time the session is in the Connectioning state. When a real session is established
The server sends a notification to the client that the session is actually established after the client obtains the notification.
Packagesession;Importjava.io.IOException;ImportJava.util.concurrent.CountDownLatch;Importorg.apache.zookeeper.WatchedEvent;ImportOrg.apache.zookeeper.Watcher;ImportOrg.apache.zookeeper.ZooKeeper;Importorg.apache.zookeeper.Watcher.Event.KeeperState; Public classCreatezookeeperImplementsWatcher {Private StaticCountdownlatch Connectedsemaphore =NewCountdownlatch (1); @Override Public voidprocess (Watchedevent event) {System.out.println ("Receive watched event:" +event); if(keeperstate.syncconnected==event.getstate ()) {Connectedsemaphore.countdown (); } } Public Static voidMain (string[] args)throwsIOException {ZooKeeper ZooKeeper=NewZooKeeper ("192.168.64.60", 5000,NewCreatezookeeper ()); System.out.println (Zookeeper.getstate ()); Try{connectedsemaphore.await (); } Catch(Interruptedexception e) {} System.out.println ("Zookeeper Session established"); } }
The results of the program run as follows:
The Createzookeeper class implements the Watcher interface to rewrite the process method, which is responsible for processing notifications from the zookeeper server. In
After receiving the Synconncted event from the server, the Countdownlatch waiting block in the main program is unblocked. This completes the client session creation.
Create an instance of Multiplexing SessionID and SESSIONPASSWD
Packagesession;Importjava.io.IOException;ImportJava.util.concurrent.CountDownLatch;Importorg.apache.zookeeper.WatchedEvent;ImportOrg.apache.zookeeper.Watcher;ImportOrg.apache.zookeeper.ZooKeeper;Importorg.apache.zookeeper.Watcher.Event.KeeperState; Public classCreateZookeeper2ImplementsWatcher { Public StaticCountdownlatch Connectedsemaphore =NewCountdownlatch (1); @Override Public voidprocess (Watchedevent event) {System.out.println ("Receive watched event:" +event); if(keeperstate.syncconnected==event.getstate ()) {Connectedsemaphore.countdown (); } } Public Static voidMain (string[] args)throwsIOException, interruptedexception {ZooKeeper ZooKeeper=NewZooKeeper ("192.168.64.60", 5000,NewCreatezookeeper ()); Connectedsemaphore.await (); LongSessionId =Zookeeper.getsessionid (); byte[] sessionpasswd =zookeeper.getsessionpasswd (); //multiplexing SessionID and sessionpasswdZooKeeper =NewZooKeeper ("192.168.64.60", 5000,NewCreatezookeeper (), 1l, "test". GetBytes ()); ZooKeeper=NewZooKeeper ("192.168.64.60", 5000,Newcreatezookeeper (), sessionid,sessionpasswd); Thread.Sleep (Integer.max_value);}}
We can see that the first time using the wrong ID and the PW server return the "EXpired" event, while the second time using the correct ID and PW is reconnected.
Read the principle of distributed Consistency Java Client API operations