import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
/**
* 分布式锁
*@Title:
*@Version:1.0
*/
@Component
public class DistributeLock {
@Autowired
private StringRedisTemplate redis;
public boolean tryLock(String key, String uuid, long expireMillis) {
BoundValueOperations<String, String> oper = redis.boundValueOps(key);
if (oper.setIfAbsent(uuid)) {
oper.expire(expireMillis, TimeUnit.MILLISECONDS);
return true;
} else if (oper.getExpire() == null || oper.getExpire() == -1 ) {
oper.expire(expireMillis, TimeUnit.MILLISECONDS);
}
return false;
}
public boolean unlock(final String key, final String uuid) {
BoundValueOperations<String, String> oper = redis.boundValueOps(key);
String value = oper.get();
if (StringUtils.equals(value, uuid)) {
redis.delete(key);
return true;
}
return false;
}
/**
*
* @param key
* @param uuid
* @param expireMillis 锁的超时时间
* @param timeout 获得锁的超时时间
* @return
*/
public boolean lock(String key, String uuid, long expireMillis,long timeout) {
BoundValueOperations<String, String> oper = redis.boundValueOps(key);
long beginTime = System.currentTimeMillis();
try {
// 在timeout的时间范围内不断轮询锁
while (System.currentTimeMillis() - beginTime < timeout) {
// 锁不存在的话,设置锁并设置锁过期时间,即加锁
if (oper.setIfAbsent(uuid)) {
oper.expire(expireMillis, TimeUnit.MILLISECONDS);
return true;
} else if (oper.getExpire() == null || oper.getExpire() == -1) {
oper.expire(expireMillis, TimeUnit.MILLISECONDS);
}
// 短暂休眠后轮询,避免可能的活锁
Thread.sleep(30);
}
} catch (Exception e) {
throw new RuntimeException("locking error", e);
}
return false;
}
}
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
/**
* 分布式锁
*@Title:
*@Version:1.0
*/
@Component
public class DistributeLock {
@Autowired
private StringRedisTemplate redis;
public boolean tryLock(String key, String uuid, long expireMillis) {
BoundValueOperations<String, String> oper = redis.boundValueOps(key);
if (oper.setIfAbsent(uuid)) {
oper.expire(expireMillis, TimeUnit.MILLISECONDS);
return true;
} else if (oper.getExpire() == null || oper.getExpire() == -1 ) {
oper.expire(expireMillis, TimeUnit.MILLISECONDS);
}
return false;
}
public boolean unlock(final String key, final String uuid) {
BoundValueOperations<String, String> oper = redis.boundValueOps(key);
String value = oper.get();
if (StringUtils.equals(value, uuid)) {
redis.delete(key);
return true;
}
return false;
}
/**
*
* @param key
* @param uuid
* @param expireMillis 锁的超时时间
* @param timeout 获得锁的超时时间
* @return
*/
public boolean lock(String key, String uuid, long expireMillis,long timeout) {
BoundValueOperations<String, String> oper = redis.boundValueOps(key);
long beginTime = System.currentTimeMillis();
try {
// 在timeout的时间范围内不断轮询锁
while (System.currentTimeMillis() - beginTime < timeout) {
// 锁不存在的话,设置锁并设置锁过期时间,即加锁
if (oper.setIfAbsent(uuid)) {
oper.expire(expireMillis, TimeUnit.MILLISECONDS);
return true;
} else if (oper.getExpire() == null || oper.getExpire() == -1) {
oper.expire(expireMillis, TimeUnit.MILLISECONDS);
}
// 短暂休眠后轮询,避免可能的活锁
Thread.sleep(30);
}
} catch (Exception e) {
throw new RuntimeException("locking error", e);
}
return false;
}
}