public class DistributedLockExample {
private static final String ZK_CONNECTION_STRING = "localhost:2181";
private static final String LOCK_PATH = "/locks";
public static void main(String[] args) throws Exception {
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(ZK_CONNECTION_STRING, new RetryNTimes(3, 1000));
curatorFramework.start();
InterProcessMutex lock = new InterProcessMutex(curatorFramework, LOCK_PATH);
if (lock.acquire(10, TimeUnit.SECONDS)) {
try {
Thread.sleep(5000);
} finally {
lock.release();
}
} else {
}
curatorFramework.close();
}
}