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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| public class TestLock {
ZooKeeper zk; ZKConf zkConf; DefaultWatch defaultWatch;
@Before public void conn(){ zkConf = new ZKConf(); zkConf.setAddress("192.168.163.128:2181,192.168.163.129:2181,192.168.163.130:2181,192.168.163.131:2181/testLock"); zkConf.setSessionTime(1000); defaultWatch = new DefaultWatch(); ZKUtils.setConf(zkConf); ZKUtils.setWatch(defaultWatch); zk = ZKUtils.getZK(); }
@After public void close(){ ZKUtils.closeZK(); }
@Test public void testlock(){ for (int i = 0; i < 10; i++) { new Thread(){ @Override public void run() { WatchCallBack watchCallBack = new WatchCallBack(); watchCallBack.setZk(zk); String name = Thread.currentThread().getName(); watchCallBack.setThreadName(name);
try { watchCallBack.tryLock(); System.out.println(name + " at work"); watchCallBack.getRootData(); watchCallBack.unLock(); } catch (Exception e) { e.printStackTrace(); }
} }.start(); } while(true){
} } } public class WatchCallBack implements Watcher, AsyncCallback.StringCallback, AsyncCallback.Children2Callback,AsyncCallback.StatCallback,AsyncCallback.DataCallback {
ZooKeeper zk; CountDownLatch cc = new CountDownLatch(1); String lockName ; String threadName;
public String getThreadName() { return threadName; }
public void setThreadName(String threadName) { this.threadName = threadName; }
public ZooKeeper getZk() { return zk; }
public void setZk(ZooKeeper zk) { this.zk = zk; }
public void tryLock() { try { zk.create("/lock", threadName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, this, threadName ); cc.await(); } catch (InterruptedException e) { e.printStackTrace(); } }
public void getRootData() throws KeeperException, InterruptedException { byte[] data = zk.getData("/", false, new Stat()); System.out.println(new String(data)); }
public void unLock(){ try { zk.delete("/"+lockName,-1); } catch (InterruptedException e) { e.printStackTrace(); } catch (KeeperException e) { e.printStackTrace(); } }
@Override public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
if(children == null){ System.out.println(ctx.toString() + "list null"); }else{ try { Collections.sort(children); int i = children.indexOf(lockName); if(i<1){ System.out.println(threadName+" i am first..."); zk.setData("/",threadName.getBytes(),-1); cc.countDown(); }else{ System.out.println(threadName+" watch "+children.get(i-1)); zk.exists("/"+children.get(i-1),this); } } catch (Exception e) { e.printStackTrace(); } }
}
@Override public void processResult(int rc, String path, Object ctx, String name) {
System.out.println(ctx.toString()+" create path: "+ name); lockName = name.substring(1); zk.getChildren("/", false, this, ctx ); }
@Override public void process(WatchedEvent event) {
Event.EventType type = event.getType(); switch (type) {
case NodeDeleted: zk.getChildren("/", false, this, ""); break;
case NodeChildrenChanged: break; }
}
@Override public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { } @Override public void processResult(int rc, String path, Object ctx, Stat stat) {
} }
|