forceUnlock, isLocked, isHeldByCurrentThread and getHoldCount methods added to RLock

pull/6/head
Nikita 11 years ago
parent 9ccafd4b5e
commit 5777e23ced

@ -34,7 +34,7 @@ import org.redisson.connection.RoundRobinLoadBalancer;
public class Config {
/**
* Сonnection load balancer to multiple Redis servers
* Сonnection load balancer to use multiple Redis servers
*/
private LoadBalancer loadBalancer = new RoundRobinLoadBalancer();

@ -230,11 +230,10 @@ public class RedissonLock extends RedissonObject implements RLock {
lock.decCounter();
connection.set(getKeyName(), lock);
} else {
connection.del(getKeyName());
connection.publish(getChannelName(), unlockMessage);
unlock(connection);
}
} else {
throw new IllegalMonitorStateException("Attempt to unlock lock, not locked by current thread id: "
throw new IllegalMonitorStateException("Attempt to unlock lock, not locked by current id: "
+ id + " thread-id: " + Thread.currentThread().getId());
}
} finally {
@ -242,6 +241,21 @@ public class RedissonLock extends RedissonObject implements RLock {
}
}
private void unlock(RedisConnection<Object, Object> connection) {
int counter = 0;
while (counter < 5) {
connection.multi();
connection.del(getKeyName());
connection.publish(getChannelName(), unlockMessage);
if (connection.exec().size() == 2) {
return;
}
counter++;
}
throw new IllegalStateException("Can't unlock lock after 5 attempts. Current id: "
+ id + " thread-id: " + Thread.currentThread().getId());
}
@Override
public Condition newCondition() {
// TODO implement
@ -253,4 +267,59 @@ public class RedissonLock extends RedissonObject implements RLock {
connectionManager.unsubscribe(pubSubEntry, getChannelName());
}
@Override
public void forceUnlock() {
RedisConnection<Object, Object> connection = connectionManager.connection();
try {
while (true) {
LockValue lock = (LockValue) connection.get(getKeyName());
if (lock != null) {
unlock(connection);
}
}
} finally {
connectionManager.release(connection);
}
}
@Override
public boolean isLocked() {
RedisConnection<Object, Object> connection = connectionManager.connection();
try {
LockValue lock = (LockValue) connection.get(getKeyName());
return lock != null;
} finally {
connectionManager.release(connection);
}
}
@Override
public boolean isHeldByCurrentThread() {
LockValue currentLock = new LockValue(id, Thread.currentThread().getId());
RedisConnection<Object, Object> connection = connectionManager.connection();
try {
LockValue lock = (LockValue) connection.get(getKeyName());
return lock != null && lock.equals(currentLock);
} finally {
connectionManager.release(connection);
}
}
@Override
public int getHoldCount() {
LockValue currentLock = new LockValue(id, Thread.currentThread().getId());
RedisConnection<Object, Object> connection = connectionManager.connection();
try {
LockValue lock = (LockValue) connection.get(getKeyName());
if (lock != null && lock.equals(currentLock)) {
return lock.getCounter();
}
return 0;
} finally {
connectionManager.release(connection);
}
}
}

@ -26,4 +26,12 @@ import java.util.concurrent.locks.Lock;
*/
public interface RLock extends Lock, RObject {
void forceUnlock();
boolean isLocked();
boolean isHeldByCurrentThread();
int getHoldCount();
}

Loading…
Cancel
Save