jedis实现分布式锁setnx

引用锁
String lockName = LockConstant.Company_ZsCash + login.getCompany().getId();
boolean bool = RedisLock.tryLock(lockName, 5000);
if (!bool) {
throw Ex.build(LockConstant.Message);
}
try {
companyService.zsCash(login.getCompany().getId(), login.getCustomer().getId(), zh, pwd, money, dxyzm,yzmxh);
return AjaxJson.OK();
} catch (Exception e) {
throw e;
} finally {
RedisLock.releaseLock(lockName);
}
加锁redis工具类
public class RedisLock {

private static String lockNamePrefix = "distribute_lock:";

private static Logger log = LoggerFactory.getLogger(RedisLock.class);

public static boolean tryLock(String lockName, long lockTimeout) {
JedisPool pool = RedisUtils.getInstance().getPool();
Jedis jedis = null;
lockName = lockNamePrefix + lockName;
try {
jedis = pool.getResource();
long expires = System.currentTimeMillis() + lockTimeout + 1;
String expiresStr = String.valueOf(expires); //锁到期时间
if (jedis.setnx(lockName, expiresStr) == 1) {
return true;
}

String currentValueStr = jedis.get(lockName); //redis里的时间
//判断是否为空,不为空的情况下,如果被其他线程设置了值,则第二个条件判断是过不去的
if (currentValueStr != null && Long.parseLong(currentValueStr) < System.currentTimeMillis()) {
String oldValueStr = jedis.getSet(lockName, expiresStr);
//获取上一个锁到期时间,并设置现在的锁到期时间,
//只有一个线程才能获取上一个线上的设置时间,因为jedis.getSet是同步的
if (oldValueStr != null && oldValueStr.equals(currentValueStr)) {
//如过这个时候,多个线程恰好都到了这里,但是只有一个线程的设置值和当前值相同,他才有权利获取锁
return true;
}
}
} catch (Exception e) {
log.error("", e);
} finally {
if (jedis != null) {
jedis.close();
}
}
return false;
}

public synchronized static boolean tryLock(String lockName) {
JedisPool pool = RedisUtils.getInstance().getPool();
Jedis jedis = null;
lockName = lockNamePrefix + lockName;
try {
jedis = pool.getResource();
if (jedis.setnx(lockName, "1") == 1) {
return true;
}
} catch (Exception e) {
log.error("", e);
} finally {
if (jedis != null) {
jedis.close();
}
}
return false;
}


/**
* Lock
*
* @param lockName
* @param list
* @return
*/
public static boolean lock(String lockName, List<String> list) {
JedisPool pool = RedisUtils.getInstance().getPool();
Jedis jedis = null;
lockName = lockNamePrefix + lockName;
try {
jedis = pool.getResource();
String group = lockName + "g";
if (jedis.setnx(group, "1") == 1) {
for (int i = 0; i < list.size(); i++) {
String key = lockName + list.get(i);
if (jedis.setnx(key, "1") == 0) {
if (i > 0) {
release(jedis, lockName, list, i);
}
jedis.del(group);
return false;
}
}
jedis.del(group);
return true;
}
} catch (Exception e) {
log.error("lock Arr", e);
} finally {
if (jedis != null) {
jedis.close();
}
}
return false;
}

private static void release(Jedis jedis, String lockName, List<String> list, int len) {
for (int i = 0; i < len; i++) {
jedis.del(lockName + list.get(i));
}
}


public static void releaseLock(String lockName) {
lockName = lockNamePrefix + lockName;
JedisPool pool = RedisUtils.getInstance().getPool();
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.del(lockName);
} finally {
if (jedis != null) {
jedis.close();
}
}
}

public static void releaseLock(String lockName, List<String> list) {
lockName = lockNamePrefix + lockName;
JedisPool pool = RedisUtils.getInstance().getPool();
Jedis jedis = null;
try {
jedis = pool.getResource();
for (int i = 0; i < list.size(); i++) {
jedis.del(lockName + list.get(i));
}
} finally {
if (jedis != null) {
jedis.close();
}
}
}

}
 

 

 
posted @ 2022-05-06 10:07  一个追求未知的人  阅读(859)  评论(0)    收藏  举报