fix:续期问题

上级 d9e1368a
package com.kwan.springbootkwan.aop;
import com.kwan.springbootkwan.annotation.RedisLock;
import com.kwan.springbootkwan.schedule.RedisLockRenewal;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
......@@ -21,6 +23,9 @@ public class RedisLockAspect {
private final StringRedisTemplate redisTemplate;
@Autowired
private RedisLockRenewal redisLockRenewal;
public RedisLockAspect(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
......@@ -36,6 +41,7 @@ public class RedisLockAspect {
if (locked) {
if (StringUtils.hasText(lockKey)) {
redisTemplate.opsForValue().set(lockKey, "locked", redisLock.expire(), TimeUnit.MILLISECONDS);
redisLockRenewal.scheduleRenewal(lockKey, 20000, redisLock.expire());
}
return joinPoint.proceed();
} else {
......
......@@ -70,7 +70,7 @@ public class DepartmentController {
@GetMapping("/all")
@RedisLock(key = "myLock_queryTreeAll", expire = 5000, timeout = 3000)
@RedisLock(key = "myLock_queryTreeAll", expire = 30000, timeout = 3000)
// @RedisLock(key = "myLock_queryTreeAll", timeout = 10000)
public ResponseEntity<List<Department>> queryTreeAll() {
return ResponseEntity.ok(this.departmentService.queryTreeAll());
......
package com.kwan.springbootkwan.schedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
public class RedisLockRenewal {
private final StringRedisTemplate redisTemplate;
/**
* 定时任务线程池
*/
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
@Autowired
public RedisLockRenewal(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void scheduleRenewal(String lockKey, long renewalIntervalMillis, long lockExpireMillis) {
scheduler.scheduleAtFixedRate(() -> {
try {
// 续期操作:设置新的过期时间
redisTemplate.expire(lockKey, lockExpireMillis, TimeUnit.MILLISECONDS);
} catch (Exception e) {
// 处理续期失败的情况,可以记录日志或采取其他操作
e.printStackTrace();
}
}, renewalIntervalMillis, renewalIntervalMillis, TimeUnit.MILLISECONDS);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册