疯狂的狮子li
2022-07-07 d9e54388e7fde2462e8b7e0165c5e7da3f202593
ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisLockController.java
@@ -4,9 +4,8 @@
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 com.ruoyi.common.core.domain.R;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
@@ -21,7 +20,7 @@
 *
 * @author shenxinquan
 */
@Api(value = "测试分布式锁的样例", tags = {"测试分布式锁的样例"})
@Tag(name ="测试分布式锁的样例", description = "测试分布式锁的样例")
@Slf4j
@RestController
@RequestMapping("/demo/redisLock")
@@ -33,10 +32,9 @@
    /**
     * 测试lock4j 注解
     */
    @ApiOperation("测试lock4j 注解")
    @Lock4j(keys = {"#key"})
    @GetMapping("/testLock4j")
    public AjaxResult<String> testLock4j(String key, String value) {
    public R<String> testLock4j(String key, String value) {
        System.out.println("start:" + key + ",time:" + LocalTime.now().toString());
        try {
            Thread.sleep(10000);
@@ -44,15 +42,14 @@
            e.printStackTrace();
        }
        System.out.println("end :" + key + ",time:" + LocalTime.now().toString());
        return AjaxResult.success("操作成功", value);
        return R.ok("操作成功", value);
    }
    /**
     * 测试lock4j 工具
     */
    @ApiOperation("测试lock4j 工具")
    @GetMapping("/testLock4jLockTemaplate")
    public AjaxResult<String> testLock4jLockTemaplate(String key, String value) {
    @GetMapping("/testLock4jLockTemplate")
    public R<String> testLock4jLockTemplate(String key, String value) {
        final LockInfo lockInfo = lockTemplate.lock(key, 30000L, 5000L, RedissonLockExecutor.class);
        if (null == lockInfo) {
            throw new RuntimeException("业务处理中,请稍后再试");
@@ -70,7 +67,7 @@
            lockTemplate.releaseLock(lockInfo);
        }
        //结束
        return AjaxResult.success("操作成功", value);
        return R.ok("操作成功", value);
    }
}