@ThriftMethod
public OptionalLockProjection acquireLock(CuratorProjection projection, final String path, int maxWaitMs) throws RpcException
{
try
{
CuratorEntry entry = CuratorEntry.mustGetEntry(connectionManager, projection);
final InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(entry.getClient(), path);
if ( !lock.acquire(maxWaitMs, TimeUnit.MILLISECONDS) )
{
return new OptionalLockProjection();
}
Closer closer = new Closer()
{
@Override
public void close()
{
if ( lock.isAcquiredInThisProcess() )
{
try
{
lock.release();
}
catch ( Exception e )
{
log.error("Could not release left-over lock for path: " + path, e);
}
}
}
};
String id = entry.addThing(lock, closer);
return new OptionalLockProjection(new LockProjection(id));
}
catch ( Exception e )
{
throw new RpcException(e);