1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| import com.google.common.base.Strings; import com.google.common.util.concurrent.ThreadFactoryBuilder; import org.apache.zookeeper.*;
import java.io.IOException; import java.util.concurrent.*;
public class DistributedLockBasedOnZookeeper { private String hostPort = "host:port"; private String lockNameSpace = "/myLock"; private String nodeString = lockNameSpace + "/test1"; private ZooKeeper zk;
public DistributedLockBasedOnZookeeper(){ try { zk = new ZooKeeper(hostPort, 6000, event -> { System.out.println("Receive event " + event); if (Watcher.Event.KeeperState.SyncConnected == event.getState()){ System.out.println("Connection is established..."); } }); } catch (IOException e) { e.printStackTrace(); } } private void ensureRootPath() throws InterruptedException { try { if (zk.exists(lockNameSpace, true) == null){ zk.create(lockNameSpace, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } catch (KeeperException e) { e.printStackTrace(); } }
private void watchNode(String nodeString, final Thread thread){ try { zk.exists(nodeString, event -> { System.out.println("==" + event.toString()); if (event.getType() == Watcher.Event.EventType.NodeDeleted){ System.out.println("There is a Thread released lock....."); thread.interrupt(); } try { zk.exists(nodeString, true); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
public boolean getLock() throws InterruptedException { String path = null; ensureRootPath(); watchNode(nodeString, Thread.currentThread()); while (true){ try { path = zk.create(nodeString, "".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); } catch (KeeperException e) { System.out.println(Thread.currentThread().getName() + "getting Lock but can not get"); Thread.sleep(5000); } if (!Strings.nullToEmpty(path).trim().isEmpty()){ System.out.println(Thread.currentThread().getName() + " get Lock..."); return true; } } }
public void unlock(){ try { zk.delete(nodeString, -1); System.out.println(Thread.currentThread().getName() + " release Lock..."); } catch (InterruptedException e) { e.printStackTrace(); } catch (KeeperException e) { e.printStackTrace(); } }
public static void main(String[] args) { ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build(); ExecutorService service = new ThreadPoolExecutor(10, 10, 1000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1024), threadFactory, new ThreadPoolExecutor.AbortPolicy());
for (int i = 0; i < 4; i++){ service.execute(() -> { DistributedLockBasedOnZookeeper lockBasedOnZookeeper = new DistributedLockBasedOnZookeeper(); try { lockBasedOnZookeeper.getLock(); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } lockBasedOnZookeeper.unlock(); }); } service.shutdown(); } }
|