org.apache.curator.framework.recipes.locks.InterProcessLock Java Examples
The following examples show how to use
org.apache.curator.framework.recipes.locks.InterProcessLock.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MultiSharedLockTest.java From BigData-In-Practice with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { CuratorFramework client = ZKUtils.getClient(); client.start(); InterProcessLock lock1 = new InterProcessMutex(client, lockPath1); // 可重入锁 InterProcessLock lock2 = new InterProcessSemaphoreMutex(client, lockPath2); // 不可重入锁 // 组锁,多锁 InterProcessMultiLock lock = new InterProcessMultiLock(Arrays.asList(lock1, lock2)); if (!lock.acquire(10, TimeUnit.SECONDS)) { throw new IllegalStateException("不能获取多锁"); } System.out.println("已获取多锁"); System.out.println("是否有第一个锁: " + lock1.isAcquiredInThisProcess()); System.out.println("是否有第二个锁: " + lock2.isAcquiredInThisProcess()); try { resource.use(); // 资源操作 } finally { System.out.println("释放多个锁"); lock.release(); // 释放多锁 } System.out.println("是否有第一个锁: " + lock1.isAcquiredInThisProcess()); System.out.println("是否有第二个锁: " + lock2.isAcquiredInThisProcess()); client.close(); System.out.println("结束!"); }
Example #2
Source File: CuratorDistributedLock.java From yuzhouwan with Apache License 2.0 | 6 votes |
public void supervene() { final CountDownLatch countDownLatch = new CountDownLatch(1); final InterProcessLock interProcessLock = new InterProcessMutex(curatorFramework, "/lock"); int count = 10; while (count > 0) { new Thread() { @Override public void run() { try { countDownLatch.await(); interProcessLock.acquire(); String now = simpleDateFormat.format(new Date()); LOG.info("Now time: ".concat(now)); interProcessLock.release(); } catch (Exception e) { throw new RuntimeException(e); } } }.start(); count--; } countDownLatch.countDown(); }
Example #3
Source File: MigrationManager.java From curator with Apache License 2.0 | 5 votes |
private CompletionStage<Void> runMigrationInLock(InterProcessLock lock, MigrationSet set) { String thisMetaDataPath = ZKPaths.makePath(metaDataPath, set.id()); return childrenWithData(client, thisMetaDataPath) .thenCompose(metaData -> applyMetaData(set, metaData, thisMetaDataPath)) .handle((v, e) -> { release(lock, true); if ( e != null ) { Throwables.propagate(e); } return v; } ); }
Example #4
Source File: AsyncWrappers.java From curator with Apache License 2.0 | 5 votes |
/** * Attempt to acquire the given lock asynchronously using the given timeout and executor. If the lock * is not acquired within the timeout stage is completedExceptionally with {@link AsyncWrappers.TimeoutException} * * @param lock a lock implementation (e.g. {@link org.apache.curator.framework.recipes.locks.InterProcessMutex}, * {@link org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2}, etc.) * @param timeout max timeout to acquire lock * @param unit time unit of timeout * @param executor executor to use to asynchronously acquire * @return stage */ public static CompletionStage<Void> lockAsync(InterProcessLock lock, long timeout, TimeUnit unit, Executor executor) { CompletableFuture<Void> future = new CompletableFuture<>(); if ( executor == null ) { CompletableFuture.runAsync(() -> lock(future, lock, timeout, unit)); } else { CompletableFuture.runAsync(() -> lock(future, lock, timeout, unit), executor); } return future; }
Example #5
Source File: AsyncWrappers.java From curator with Apache License 2.0 | 5 votes |
/** * Attempt to acquire the given lock asynchronously using the given timeout and executor. The stage * is completed with a Boolean that indicates whether or not the lock was acquired. * * @param lock a lock implementation (e.g. {@link org.apache.curator.framework.recipes.locks.InterProcessMutex}, * {@link org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2}, etc.) * @param timeout max timeout to acquire lock * @param unit time unit of timeout * @param executor executor to use to asynchronously acquire * @return stage */ public static CompletionStage<Boolean> lockAsyncIf(InterProcessLock lock, long timeout, TimeUnit unit, Executor executor) { CompletableFuture<Boolean> future = new CompletableFuture<>(); if ( executor == null ) { CompletableFuture.runAsync(() -> lockIf(future, lock, timeout, unit)); } else { CompletableFuture.runAsync(() -> lockIf(future, lock, timeout, unit), executor); } return future; }
Example #6
Source File: AsyncWrappers.java From curator with Apache License 2.0 | 5 votes |
private static void lockIf(CompletableFuture<Boolean> future, InterProcessLock lock, long timeout, TimeUnit unit) { try { future.complete(lock.acquire(timeout, unit)); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); future.completeExceptionally(e); } }
Example #7
Source File: InterProcessMultiLockExample.java From ZKRecipesByExample with Apache License 2.0 | 5 votes |
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()); } }
Example #8
Source File: MockCurator.java From vespa with Apache License 2.0 | 4 votes |
/** Create a mutex which ensures exclusive access within this single vm */ @Override public InterProcessLock createMutex(String path) { return new MockLock(path); }
Example #9
Source File: Curator.java From vespa with Apache License 2.0 | 4 votes |
/** For internal use; prefer creating a {@link com.yahoo.vespa.curator.Lock} */ public InterProcessLock createMutex(String lockPath) { return new InterProcessMutex(curatorFramework, lockPath); }
Example #10
Source File: ZookeeperPseudoLock.java From exhibitor with Apache License 2.0 | 4 votes |
public ZookeeperPseudoLock(InterProcessLock lock) { this.lock = lock; }
Example #11
Source File: MigrationManager.java From curator with Apache License 2.0 | 3 votes |
/** * Process the given migration set * * @param set the set * @return completion stage. If there is a migration-specific error, the stage will be completed * exceptionally with {@link org.apache.curator.x.async.migrations.MigrationException}. */ public CompletionStage<Void> migrate(MigrationSet set) { InterProcessLock lock = new InterProcessSemaphoreMutex(client.unwrap(), ZKPaths.makePath(lockPath, set.id())); CompletionStage<Void> lockStage = lockAsync(lock, lockMax.toMillis(), TimeUnit.MILLISECONDS, executor); return lockStage.thenCompose(__ -> runMigrationInLock(lock, set)); }
Example #12
Source File: AsyncWrappers.java From curator with Apache License 2.0 | 2 votes |
/** * Attempt to acquire the given lock asynchronously using the given executor and without a timeout. * * @param lock a lock implementation (e.g. {@link org.apache.curator.framework.recipes.locks.InterProcessMutex}, * {@link org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2}, etc.) * @param executor executor to use to asynchronously acquire * @return stage */ public static CompletionStage<Void> lockAsync(InterProcessLock lock, Executor executor) { return lockAsync(lock, 0, null, executor); }
Example #13
Source File: AsyncWrappers.java From curator with Apache License 2.0 | 2 votes |
/** * Attempt to acquire the given lock asynchronously using the given timeout using the {@link java.util.concurrent.ForkJoinPool#commonPool()}. * If the lock is not acquired within the timeout stage is completedExceptionally with {@link AsyncWrappers.TimeoutException} * * @param lock a lock implementation (e.g. {@link org.apache.curator.framework.recipes.locks.InterProcessMutex}, * {@link org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2}, etc.) * @param timeout max timeout to acquire lock * @param unit time unit of timeout * @return stage */ public static CompletionStage<Void> lockAsync(InterProcessLock lock, long timeout, TimeUnit unit) { return lockAsync(lock, timeout, unit, null); }
Example #14
Source File: AsyncWrappers.java From curator with Apache License 2.0 | 2 votes |
/** * Attempt to acquire the given lock asynchronously using the given timeout using the {@link java.util.concurrent.ForkJoinPool#commonPool()}. * The stage is completed with a Boolean that indicates whether or not the lock was acquired. * * @param lock a lock implementation (e.g. {@link org.apache.curator.framework.recipes.locks.InterProcessMutex}, * {@link org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2}, etc.) * @param timeout max timeout to acquire lock * @param unit time unit of timeout * @return stage */ public static CompletionStage<Boolean> lockAsyncIf(InterProcessLock lock, long timeout, TimeUnit unit) { return lockAsyncIf(lock, timeout, unit, null); }
Example #15
Source File: AsyncWrappers.java From curator with Apache License 2.0 | 2 votes |
/** * Attempt to acquire the given lock asynchronously without timeout using the {@link java.util.concurrent.ForkJoinPool#commonPool()}. * * @param lock a lock implementation (e.g. {@link org.apache.curator.framework.recipes.locks.InterProcessMutex}, * {@link org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2}, etc.) * @return stage */ public static CompletionStage<Void> lockAsync(InterProcessLock lock) { return lockAsync(lock, 0, null, null); }
Example #16
Source File: AsyncWrappers.java From curator with Apache License 2.0 | 2 votes |
/** * Release the lock and wrap any exception in <code>RuntimeException</code> * * @param lock lock to release */ public static void release(InterProcessLock lock) { release(lock, true); }