org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock Java Examples

The following examples show how to use org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock. 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: BalancedMetricResolver.java    From timely with Apache License 2.0 6 votes vote down vote up
private void testIPRWLock(CuratorFramework curatorFramework, InterProcessReadWriteLock lock, String path) {
    try {
        lock.writeLock().acquire(10, TimeUnit.SECONDS);
    } catch (Exception e1) {
        try {
            curatorFramework.delete().deletingChildrenIfNeeded().forPath(path);
            curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT)
                    .forPath(path);
        } catch (Exception e2) {
            LOG.info(e2.getMessage(), e2);
        }
    } finally {
        try {
            lock.writeLock().release();
        } catch (Exception e3) {
            LOG.error(e3.getMessage(), e3);
        }
    }
}
 
Example #2
Source File: DataStoreCache.java    From timely with Apache License 2.0 6 votes vote down vote up
private void testIPRWLock(CuratorFramework curatorFramework, InterProcessReadWriteLock lock, String path) {
    try {
        lock.writeLock().acquire(10, TimeUnit.SECONDS);
    } catch (Exception e1) {
        try {
            curatorFramework.delete().deletingChildrenIfNeeded().forPath(path);
            curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT)
                    .forPath(path);
        } catch (Exception e2) {
            LOG.info(e2.getMessage(), e2);
        }
    } finally {
        try {
            lock.writeLock().release();
        } catch (Exception e3) {
            LOG.error(e3.getMessage());
        }
    }
}
 
Example #3
Source File: ZkAtom.java    From Milkomeda with MIT License 5 votes vote down vote up
private AtomLockInfo sharedReentrantReadWriteLock(String keyPath, long time, TimeUnit unit, boolean readOnly) throws Exception {
    String lockPath = ROOT_LOCK_NODE + SEPARATOR + keyPath;
    InterProcessReadWriteLock rwLock = new InterProcessReadWriteLock(client, lockPath);
    InterProcessMutex lock = readOnly ? rwLock.readLock() : rwLock.writeLock();
    boolean isLocked = lock.acquire(time, unit);
    return AtomLockInfo.builder().isLocked(isLocked).lock(lock).build();
}
 
Example #4
Source File: ExampleClientReadWriteLocks.java    From ZKRecipesByExample with Apache License 2.0 5 votes vote down vote up
public ExampleClientReadWriteLocks(CuratorFramework client, String lockPath, FakeLimitedResource resource, String clientName) {
	this.resource = resource;
	this.clientName = clientName;
	lock = new InterProcessReadWriteLock(client, lockPath);
	readLock = lock.readLock();
	writeLock = lock.writeLock();
}
 
Example #5
Source File: SharedReentrantReadWriteLockTest.java    From BigData-In-Practice with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    for (int i = 0; i < clientNums; i++) {
        final String clientName = "client#" + i;
        new Thread(new Runnable() {
            @Override
            public void run() {
                CuratorFramework client = ZKUtils.getClient();
                client.start();
                final InterProcessReadWriteLock lock = new InterProcessReadWriteLock(client, lockPath);
                final InterProcessMutex readLock = lock.readLock();
                final InterProcessMutex writeLock = lock.writeLock();

                try {
                    // 注意只能先得到写锁再得到读锁,不能反过来!!!
                    if (!writeLock.acquire(10, TimeUnit.SECONDS)) {
                        throw new IllegalStateException(clientName + " 不能得到写锁");
                    }
                    System.out.println(clientName + " 已得到写锁");
                    if (!readLock.acquire(10, TimeUnit.SECONDS)) {
                        throw new IllegalStateException(clientName + " 不能得到读锁");
                    }
                    System.out.println(clientName + " 已得到读锁");
                    try {
                        resource.use(); // 使用资源
                    } finally {
                        System.out.println(clientName + " 释放读写锁");
                        readLock.release();
                        writeLock.release();
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                } finally {
                    CloseableUtils.closeQuietly(client);
                    countDownLatch.countDown();
                }
            }
        }).start();
    }
    countDownLatch.await();
    System.out.println("结束!");
}