package com.colobu.zkrecipe.lock;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
import org.apache.curator.framework.recipes.locks.InterProcessMultiLock;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.test.TestingServer;

public class InterProcessMultiLockExample {
	private static final String PATH1 = "/examples/locks1";
	private static final String PATH2 = "/examples/locks2";
	
	public static void main(String[] args) throws Exception {
		FakeLimitedResource resource = new FakeLimitedResource();
		try (TestingServer server = new TestingServer()) {
			CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
			client.start();
			
			InterProcessLock lock1 = new InterProcessMutex(client, PATH1);
			InterProcessLock lock2 = new InterProcessSemaphoreMutex(client, PATH2);
			
			InterProcessMultiLock lock = new InterProcessMultiLock(Arrays.asList(lock1, lock2));

			if (!lock.acquire(10, TimeUnit.SECONDS)) {
				throw new IllegalStateException("could not acquire the lock");
			}
			System.out.println("has the lock");
			
			System.out.println("has the lock1: " + lock1.isAcquiredInThisProcess());
			System.out.println("has the lock2: " + lock2.isAcquiredInThisProcess());
			
			try {			
				resource.use(); //access resource exclusively
			} finally {
				System.out.println("releasing the lock");
				lock.release(); // always release the lock in a finally block
			}
			System.out.println("has the lock1: " + lock1.isAcquiredInThisProcess());
			System.out.println("has the lock2: " + lock2.isAcquiredInThisProcess());
		}
	}

}

@@ -599,16 +599,211 @@
 erver;%0A%0A
+/**%0A * %E5%A4%9A%E9%94%81%E5%AF%B9%E8%B1%A1 Multi Shared Lock%0A * Multi Shared Lock%E6%98%AF%E4%B8%80%E4%B8%AA%E9%94%81%E7%9A%84%E5%AE%B9%E5%99%A8%E3%80%82%0A * %E5%BD%93%E8%B0%83%E7%94%A8acquire%EF%BC%8C %E6%89%80%E6%9C%89%E7%9A%84%E9%94%81%E9%83%BD%E4%BC%9A%E8%A2%ABacquire%EF%BC%8C%E5%A6%82%E6%9E%9C%E8%AF%B7%E6%B1%82%E5%A4%B1%E8%B4%A5%EF%BC%8C%E6%89%80%E6%9C%89%E7%9A%84%E9%94%81%E9%83%BD%E4%BC%9A%E8%A2%ABrelease%E3%80%82%0A * %E5%90%8C%E6%A0%B7%E8%B0%83%E7%94%A8release%E6%97%B6%E6%89%80%E6%9C%89%E7%9A%84%E9%94%81%E9%83%BD%E8%A2%ABrelease(%E5%A4%B1%E8%B4%A5%E8%A2%AB%E5%BF%BD%E7%95%A5)%E3%80%82%0A * %E5%9F%BA%E6%9C%AC%E4%B8%8A%EF%BC%8C%E5%AE%83%E5%B0%B1%E6%98%AF%E7%BB%84%E9%94%81%E7%9A%84%E4%BB%A3%E8%A1%A8%EF%BC%8C%E5%9C%A8%E5%AE%83%E4%B8%8A%E9%9D%A2%E7%9A%84%E8%AF%B7%E6%B1%82%E9%87%8A%E6%94%BE%E6%93%8D%E4%BD%9C%E9%83%BD%E4%BC%9A%E4%BC%A0%E9%80%92%E7%BB%99%E5%AE%83%E5%8C%85%E5%90%AB%E7%9A%84%E6%89%80%E6%9C%89%E7%9A%84%E9%94%81%E3%80%82%0A */%0A
 public c

test MultiLock