package com.ruoyi.demo.controller;
import com.baomidou.lock.LockInfo;
import com.baomidou.lock.LockTemplate;
import com.baomidou.lock.annotation.Lock4j;
import com.baomidou.lock.executor.RedissonLockExecutor;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalTime;
/**
* 测试分布å¼é”çš„æ ·ä¾‹
*
* @author shenxinquan
*/
@Api(value = "测试分布å¼é”çš„æ ·ä¾‹", tags = {"测试分布å¼é”çš„æ ·ä¾‹"})
@Slf4j
@RestController
@RequestMapping("/demo/redisLock")
public class RedisLockController {
@Autowired
private LockTemplate lockTemplate;
/**
* 测试lock4j 注解
*/
@ApiOperation("测试lock4j 注解")
@Lock4j(keys = {"#key"})
@GetMapping("/testLock4j")
public AjaxResult<String> testLock4j(String key, String value) {
System.out.println("start:" + key + ",time:" + LocalTime.now().toString());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end :" + key + ",time:" + LocalTime.now().toString());
return AjaxResult.success("æ“作æˆåŠŸ", value);
}
/**
* 测试lock4j 工具
*/
@ApiOperation("测试lock4j 工具")
@GetMapping("/testLock4jLockTemaplate")
public AjaxResult<String> testLock4jLockTemaplate(String key, String value) {
final LockInfo lockInfo = lockTemplate.lock(key, 30000L, 5000L, RedissonLockExecutor.class);
if (null == lockInfo) {
throw new RuntimeException("业务处ç†ä¸,请ç¨åŽå†è¯•");
}
// 获å–锿ˆåŠŸï¼Œå¤„ç†ä¸šåŠ¡
try {
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
//
}
System.out.println("æ‰§è¡Œç®€å•æ–¹æ³•1 , 当å‰çº¿ç¨‹:" + Thread.currentThread().getName());
} finally {
//释放é”
lockTemplate.releaseLock(lockInfo);
}
//结æŸ
return AjaxResult.success("æ“作æˆåŠŸ", value);
}
}