Commit 2ab276cd authored by liuxingyu's avatar liuxingyu

分布式锁公共方法修改

parent 3810529e
......@@ -4,13 +4,20 @@ import com.mushiny.redisson.enums.LockType;
import com.mushiny.redisson.utils.RedissonLockUtil;
import com.mushiny.redisson.utils.ResultForLock;
import jodd.net.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.util.function.Tuple2;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
/**
* 描述信息
......@@ -19,6 +26,7 @@ import javax.servlet.http.HttpServletResponse;
* @since 2023/8/10 13:03
*/
@RestController
@Slf4j
public class TestController {
@Autowired
RedissonLockUtil redissonLockUtil;
......@@ -32,29 +40,26 @@ public class TestController {
@PostMapping ("api/reentrant")
public ResultForLock<String> reentrant(HttpServletResponse response) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String threadId = Thread.currentThread().getName() + Thread.currentThread().getId();
redissonLockUtil.getLockByKey("265213225444411", LockType.REENTRANT_LOCK.value());
Boolean getLock = redissonLockUtil.tryLock(50000L, "265213225444411", LockType.REENTRANT_LOCK.value());
if (getLock) {
System.out.println(threadId + "拿锁成功");
log.info("enter"+threadId+dateFormat.format(new Date()));
System.out.println(threadId+dateFormat.format(new Date()));
Tuple2<RLock, Boolean> rlocks = redissonLockUtil.tryLock(30000L, -1L, "265213225444411", LockType.REENTRANT_LOCK.value());
if (rlocks.getT2()) {
log.info(threadId + "拿锁成功"+ dateFormat.format(new Date()));
try {
count++;
jdbcTemplate.execute("UPDATE descrease a SET a.number = a.number-1");
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date startDate = new Date();
// System.out.println(threadId + "执行调度ping" + dateFormat.format(startDate));
Thread.sleep(2000);
// System.out.println("结束时间为" + dateFormat.format(new Date()));
System.out.println("执行数量为"+count);
Thread.sleep(31000);
System.out.println("成功执行" + count + "-----------" + dateFormat.format(new Date()));
} catch (Exception e) {
e.printStackTrace();
return new ResultForLock().error();
}finally {
redissonLockUtil.releaseLock("265213225444411", LockType.REENTRANT_LOCK.value());
rlocks.getT1().unlock();
}
} else {
errCount++;
System.out.println("拿锁失败"+errCount);
response.setStatus(500);
return new ResultForLock().error();
}
......@@ -63,21 +68,16 @@ public class TestController {
@PostMapping ("api/reentrantWatchDog")
public ResultForLock<String> reentrantWatchDog(HttpServletResponse response) {
@PostMapping ("api/reentrantReleaseTime")
public ResultForLock<String> reentrantReleaseTime(HttpServletResponse response) {
String threadId = Thread.currentThread().getName() + Thread.currentThread().getId();
redissonLockUtil.getLockByKey("265213225444411", LockType.REENTRANT_LOCK.value());
Boolean getLock = redissonLockUtil.tryLock(1000L,-1L, "265213225444411", LockType.REENTRANT_LOCK.value());
if (getLock) {
Tuple2<RLock, Boolean> rlocks = redissonLockUtil.tryLock(30000L, 1000L, "265213225444411", LockType.REENTRANT_LOCK.value());
if (rlocks.getT2()) {
System.out.println(threadId + "拿锁成功");
try {
count++;
jdbcTemplate.execute("UPDATE descrease a SET a.number = a.number-1");
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date startDate = new Date();
// System.out.println(threadId + "执行调度ping" + dateFormat.format(startDate));
Thread.sleep(2000);
// System.out.println("结束时间为" + dateFormat.format(new Date()));
System.out.println("执行数量为"+count);
} catch (Exception e) {
e.printStackTrace();
......@@ -101,14 +101,14 @@ public class TestController {
@PostMapping ("api/reentrantByManual")
public ResultForLock<String> reentrantByManual() {
String threadId = Thread.currentThread().getName() + Thread.currentThread().getId();
RLock rLock =redissonLockUtil.getLockByKey("265213225444411", LockType.REENTRANT_LOCK.value());
RLock rLock = redissonLockUtil.getLockByKey("265213225444411", LockType.REENTRANT_LOCK.value());
// 支持过期解锁功能,10秒钟以后自动解锁, 无需调用unlock方法手动解锁
//lock.lock(10, TimeUnit.SECONDS);
rLock.lock();// * 加锁 锁的有效期默认30秒
redissonLockUtil.lock(rLock);// * 加锁 锁的有效期默认30秒
try {
count++;
jdbcTemplate.execute("UPDATE descrease a SET a.number = a.number-1");
Thread.sleep(2000);
Thread.sleep(32000);
System.out.println("执行数量为"+count);
} catch (Exception e) {
e.printStackTrace();
......@@ -127,8 +127,8 @@ public class TestController {
public ResultForLock<String> fair() {
String threadId = Thread.currentThread().getName() + Thread.currentThread().getId();
redissonLockUtil.getLockByKey("265213225444411", LockType.FAIR_LOCK.value());
Boolean getLock = redissonLockUtil.tryLock(50000L, "265213225444411", LockType.FAIR_LOCK.value());
if (getLock) {
Tuple2<RLock, Boolean> rlocks = redissonLockUtil.tryLock(20000L, -1L, "265213225444411", LockType.REENTRANT_LOCK.value());
if (rlocks.getT2()) {
System.out.println(threadId + "拿锁成功");
try {
count++;
......
......@@ -8,6 +8,8 @@ import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;
import java.util.concurrent.TimeUnit;
......@@ -24,7 +26,7 @@ public class RedissonLockUtil {
public static final Long WAITTIME = 12000L;
public static final Long RELEASETIME = 14000L;
public static final Long RELEASETIME = -1L;
@Autowired
RedissonClient redissonClient;
......@@ -55,30 +57,19 @@ public class RedissonLockUtil {
}
/**
* 非阻塞式,在1s内尝试获取锁
* 获取成功默认锁30s,过期看门狗自动续期
*
* @param key
* @param waitTime
* @param type
* @return
* 手动加锁,锁的有效期默认30秒
* @param rLock
*/
public Boolean tryLock(Long waitTime, String key, int type) {
boolean lock = false;
if (ObjectUtils.isEmpty(waitTime)) {
waitTime = WAITTIME;
}
try {
RLock rLock = getLockByKey(key, type);
lock = rLock.tryLock(waitTime, TimeUnit.MILLISECONDS);
if (!lock) {
log.error("分布式锁加锁:{}失败,请重新尝试");
// throw new RedissonException(RedissonErrorCode.METHOD_IN_EXECUTION);
}
} catch (Exception e) {
e.printStackTrace();
}
return lock;
public void lock(RLock rLock) {
rLock.lock();
}
/**
* 手动解锁
* @param rLock
*/
public void unlock(RLock rLock) {
rLock.unlock();
}
......@@ -91,8 +82,9 @@ public class RedissonLockUtil {
* @param type
* @return
*/
public Boolean tryLock(Long waitTime, Long releaseTime, String key, int type) {
public Tuple2<RLock,Boolean> tryLock(Long waitTime, Long releaseTime, String key, int type) {
boolean lock = false;
RLock rLock =null;
if (ObjectUtils.isEmpty(waitTime)) {
waitTime = WAITTIME;
}
......@@ -100,16 +92,12 @@ public class RedissonLockUtil {
releaseTime = RELEASETIME;
}
try {
RLock rLock = getLockByKey(key, type);
rLock = getLockByKey(key, type);
lock = rLock.tryLock(waitTime, releaseTime, TimeUnit.MILLISECONDS);
if (!lock) {
log.error("分布式锁加锁:{}失败,请重新尝试");
// throw new RedissonException(RedissonErrorCode.METHOD_IN_EXECUTION);
}
} catch (Exception e) {
e.printStackTrace();
}
return lock;
return Tuples.of(rLock,lock);
}
......
......@@ -14,7 +14,7 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8
username: root
password: 123456
password: wasd11647
......
......@@ -11,29 +11,29 @@ import java.util.Date;
@SpringBootTest
class MwmsRedissonApplicationTests {
@Autowired
RedissonLockUtil redissonLockUtil;
@Test
void contextLoads() {
String threadId = Thread.currentThread().getName() + Thread.currentThread().getId();
redissonLockUtil.getLockByKey("2652132254444", LockType.READ_LOCK.value());
Boolean getLock = redissonLockUtil.tryLock(null, "2652132254444", LockType.REENTRANT_LOCK.value());
if (getLock) {
System.out.println(threadId + "拿锁成功");
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = new Date();
System.out.println(threadId + "执行调度ping" + dateFormat.format(startDate));
Thread.sleep(10000);
System.out.println("结束时间为" + dateFormat.format(new Date()));
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("拿锁失败");
}
}
//
// @Autowired
// RedissonLockUtil redissonLockUtil;
//
// @Test
// void contextLoads() {
// String threadId = Thread.currentThread().getName() + Thread.currentThread().getId();
// redissonLockUtil.getLockByKey("2652132254444", LockType.READ_LOCK.value());
// Boolean getLock = redissonLockUtil.tryLock(null,-1L, "2652132254444", LockType.REENTRANT_LOCK.value());
// if (getLock) {
// System.out.println(threadId + "拿锁成功");
// try {
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date startDate = new Date();
// System.out.println(threadId + "执行调度ping" + dateFormat.format(startDate));
// Thread.sleep(10000);
// System.out.println("结束时间为" + dateFormat.format(new Date()));
// } catch (Exception e) {
// e.printStackTrace();
// }
// } else {
// System.out.println("拿锁失败");
// }
// }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment