Read the principle of distributed Consistency Java Client API operations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.