How to implement competitive Lock in zookeeper
This article is about how zookeeper implements competitive locks. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Package com.hgs.sharelocks;import java.util.Collections;import java.util.List;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.Watcher.Event.EventType;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.ZooKeeper;public class ZKsharelocks {private ZooKeeper zk = null; private final static String connectString = "192.168.6.131purl 2181192.168.6.132purl 2181192.168.6.133purl 2181" Private final static int sessionTimeout = 2000; private final static String parentNode= "/ servers"; private final static String subNode= "/ sub"; private volatile String myPath = null; private boolean lockflag = false;public static void main (String [] args) throws Exception {ZKsharelocks sl = new ZKsharelocks (); sl.getConnect (); Thread.sleep (Long.MAX_VALUE) } public void getConnect () throws Exception {/ / create instance zk = new ZooKeeper (connectString, sessionTimeout, new Watcher () {@ Overridepublic void process (WatchedEvent event) {/ / determine whether the event type and path is the business path if (event.getType () = = EventType.NodeChildrenChanged&&event.getPath (). Equals (parentNode)) {List inchild = null Try {/ / get the child node and continue listening to inchild = zk.getChildren (parentNode, true); / / sort Collections.sort (inchild) / / determine that the returned time path is not null and the smallest subpath is the current day path if (myPath! = null&&inchild.size () > 0&&inchild.indexOf (myPath.substring (parentNode.length () + 1)) = = 0) {doSomthing (); Thread.sleep (2000) MyPath=zk.create (parentNode+subNode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);}} catch (Exception e) {e.printStackTrace ();}); / / first create a temporary sequence entry myPath=zk.create (parentNode+subNode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL) / / get the number of subdirectories under the parent node and listen to List childs = zk.getChildren (parentNode, true); / / determine whether you are the only one when entering. If you are the only one, you can do the business logic if (childs.size () = = 1) {/ / the business method has the operation doSomthing () to delete the myPath entry in the business method; / / continue to myPath=zk.create (parentNode+subNode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL) after deletion }} public void doSomthing () throws Exception {System.out.println ("get the lock..." + Thread.currentThread (). GetName ()); / / delete the lock held after the lock is completed, that is, delete the entry zk.delete (myPath,-1);}} Thank you for reading! This is the end of the article on "how to achieve competitive locks in zookeeper". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!