Commit f69e3e6dec1f4dbdac7bf3808975a10c9bde9712
1 parent
cddd4421
分布式锁完成
Signed-off-by: TanYibin <5491541@qq.com>
Showing
5 changed files
with
64 additions
and
49 deletions
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/HuahengBaseController.java
... | ... | @@ -4,14 +4,10 @@ import java.util.concurrent.TimeUnit; |
4 | 4 | |
5 | 5 | import javax.annotation.Nonnull; |
6 | 6 | |
7 | -import org.apache.poi.ss.formula.functions.T; | |
8 | 7 | import org.jeecg.common.api.vo.Result; |
9 | -import org.jeecg.common.system.base.controller.JeecgController; | |
10 | 8 | import org.jeecg.utils.RedissonDistributedLocker; |
11 | 9 | import org.springframework.beans.factory.annotation.Autowired; |
12 | 10 | |
13 | -import com.baomidou.mybatisplus.extension.service.IService; | |
14 | - | |
15 | 11 | import cn.hutool.core.date.SystemClock; |
16 | 12 | import cn.hutool.core.util.StrUtil; |
17 | 13 | import lombok.extern.slf4j.Slf4j; |
... | ... | @@ -26,7 +22,7 @@ public class HuahengBaseController { |
26 | 22 | private RedissonDistributedLocker redissonDistributedLocker; |
27 | 23 | |
28 | 24 | public interface MultiProcessListener { |
29 | - Result<?> doProcess(); | |
25 | + Result<?> doProcess() throws Exception; | |
30 | 26 | } |
31 | 27 | |
32 | 28 | /** 等待时间 */ |
... | ... | @@ -69,17 +65,20 @@ public class HuahengBaseController { |
69 | 65 | final boolean tryLock = redissonDistributedLocker.tryLock(fullLockKey, TimeUnit.SECONDS, WAIT_TIME, LEASE_TIME); |
70 | 66 | final long endTime = SystemClock.now(); |
71 | 67 | if (!tryLock) { |
72 | - log.error("[{}]获取分布式锁失败,lockKey = {},耗时 {}ms", taskName, fullLockKey, endTime - startTime); | |
73 | - throw new RuntimeException(StrUtil.format("[{}]获取分布式锁失败,lockKey = {},等待时间超出10秒", taskName, fullLockKey)); | |
68 | + log.error("[{}] 获取分布式锁失败 lockKey = {},等待锁耗时:{}ms", taskName, fullLockKey, endTime - startTime); | |
69 | + throw new RuntimeException(StrUtil.format("[{}] 获取分布式锁失败 lockKey = {},等待时间超出10秒", taskName, fullLockKey)); | |
74 | 70 | } |
75 | 71 | // 注意:一定是获取锁成功后,才进行try{}finally{释放锁} |
76 | 72 | try { |
77 | - log.info("[{}]获取分布式锁成功,lockKey = {},耗时 {}ms", taskName, fullLockKey, endTime - startTime); | |
73 | + log.info("[{}] 开始分布式事务 lockKey = {},获取锁耗时: {}ms", taskName, fullLockKey, endTime - startTime); | |
78 | 74 | result = multiProcessListener.doProcess(); |
79 | 75 | } catch (Exception e) { |
80 | - throw new RuntimeException(StrUtil.format("[{}]执行分布式事务失败,lockKey = {}", taskName, fullLockKey), e); | |
76 | + final long finishTime = SystemClock.now(); | |
77 | + throw new RuntimeException(StrUtil.format("[{}] 执行分布式事务失败 lockKey = {},事务耗时:{}ms", taskName, fullLockKey, finishTime - endTime), e); | |
81 | 78 | } finally { |
82 | 79 | redissonDistributedLocker.unlock(fullLockKey); |
80 | + final long finishTime = SystemClock.now(); | |
81 | + log.info("[{}] 结束分布式事务 lockKey = {},事务耗时:{}ms", taskName, fullLockKey, finishTime - endTime); | |
83 | 82 | } |
84 | 83 | |
85 | 84 | return result; |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/TestController.java
1 | 1 | package org.jeecg.modules.wms.framework.controller; |
2 | 2 | |
3 | +import java.security.NoSuchAlgorithmException; | |
4 | +import java.security.SecureRandom; | |
5 | +import java.util.Date; | |
3 | 6 | import java.util.Map; |
4 | -import java.util.concurrent.ConcurrentHashMap; | |
5 | 7 | |
6 | 8 | import javax.servlet.http.HttpServletRequest; |
7 | 9 | |
8 | 10 | import org.jeecg.common.api.vo.Result; |
9 | 11 | import org.jeecg.common.aspect.annotation.AutoLog; |
10 | -import org.jeecg.modules.wms.receipt.receiptHeader.entity.ReceiptHeader; | |
12 | +import org.jeecg.modules.system.entity.SysDataLog; | |
13 | +import org.jeecg.modules.system.service.ISysDataLogService; | |
11 | 14 | import org.jeecg.utils.HuahengRedisUtil; |
12 | 15 | import org.springframework.beans.factory.annotation.Autowired; |
16 | +import org.springframework.transaction.annotation.Transactional; | |
13 | 17 | import org.springframework.web.bind.annotation.PostMapping; |
14 | 18 | import org.springframework.web.bind.annotation.RequestBody; |
15 | 19 | import org.springframework.web.bind.annotation.RequestMapping; |
... | ... | @@ -17,7 +21,8 @@ import org.springframework.web.bind.annotation.ResponseBody; |
17 | 21 | import org.springframework.web.bind.annotation.RestController; |
18 | 22 | |
19 | 23 | import com.alibaba.fastjson.JSON; |
20 | -import com.alibaba.fastjson.TypeReference; | |
24 | +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | |
25 | +import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | |
21 | 26 | |
22 | 27 | import lombok.extern.slf4j.Slf4j; |
23 | 28 | |
... | ... | @@ -34,43 +39,51 @@ public class TestController extends HuahengBaseController { |
34 | 39 | @Autowired |
35 | 40 | private HuahengRedisUtil redisUtil; |
36 | 41 | |
42 | + @Autowired | |
43 | + private ISysDataLogService sysDataLogService; | |
44 | + | |
37 | 45 | @AutoLog(value = "TestController-testRedis") |
38 | 46 | @ResponseBody |
39 | 47 | @PostMapping(value = "/testRedis") |
40 | - public Result<?> testRedis(@RequestBody Map<String, String> paramMap, HttpServletRequest request) throws InterruptedException { | |
41 | - | |
42 | -// log.info(JSON.toJSONString(paramMap)); | |
43 | - String key = "testKey"; | |
44 | - Map<String, ReceiptHeader> receiptHeaderMap = new ConcurrentHashMap<String, ReceiptHeader>(); | |
45 | - ReceiptHeader value = new ReceiptHeader(); | |
46 | - value.setCode("001"); | |
47 | - receiptHeaderMap.put("receiptHeader1", value); | |
48 | - redisUtil.set(key, receiptHeaderMap); | |
49 | -// Map<String, ReceiptHeader> result = redisUtil.get(key, new TypeReference<Map<String, ReceiptHeader>>() {}); | |
50 | -// log.info("redisUtil.get -> " + JSON.toJSONString(result)); | |
48 | + public Result<?> testRedis(@RequestBody Map<String, String> paramMap, HttpServletRequest request) { | |
49 | + | |
50 | +// log.info("testRedis in paramMap:{}", JSON.toJSONString(paramMap)); | |
51 | +// String key = "testKey"; | |
52 | +// Map<String, ReceiptHeader> receiptHeaderMap = new ConcurrentHashMap<String, ReceiptHeader>(); | |
53 | +// ReceiptHeader receiptHeader = new ReceiptHeader(); | |
54 | +// receiptHeader.setCode("001"); | |
55 | +// receiptHeaderMap.put("receiptHeaderKey", receiptHeader); | |
56 | +// Map<String, ReceiptHeader> value = redisUtil.getAndSet(key, receiptHeaderMap, new TypeReference<Map<String, ReceiptHeader>>() {}, 10); | |
51 | 57 | // Long rxpireTime = redisUtil.getExpire(key); |
52 | -// log.info("rxpireTime -> " + rxpireTime); | |
53 | -// redisUtil.expire(key, 100); | |
58 | +// log.info("redisUtil.getAndSet {} -> {} rxpireTime -> {}", key, JSON.toJSONString(value), rxpireTime); | |
59 | +// | |
60 | +// Thread.sleep(2000l); | |
61 | +// receiptHeaderMap = redisUtil.get(key, new TypeReference<Map<String, ReceiptHeader>>() {}); | |
54 | 62 | // rxpireTime = redisUtil.getExpire(key); |
55 | -// log.info("rxpireTime -> " + rxpireTime); | |
56 | -// value.setCode("2222222222222222"); | |
57 | - redisUtil.delete(key); | |
58 | - Map<String, ReceiptHeader> result = redisUtil.getAndSet(key, receiptHeaderMap, new TypeReference<Map<String, ReceiptHeader>>() {}, 10); | |
59 | - Long rxpireTime = redisUtil.getExpire(key); | |
60 | - log.info("redisUtil.get -> {} rxpireTime -> {}", JSON.toJSONString(result), rxpireTime); | |
61 | - | |
62 | - Thread.sleep(2000l); | |
63 | - result = redisUtil.get(key, new TypeReference<Map<String, ReceiptHeader>>() {}); | |
64 | - rxpireTime = redisUtil.getExpire(key); | |
65 | - log.info("redisUtil.get sleep 2s -> {} rxpireTime -> {}", JSON.toJSONString(result), rxpireTime); | |
66 | - | |
67 | - Long count = redisUtil.decr("countKey", 10); | |
68 | - rxpireTime = redisUtil.getExpire("countKey"); | |
69 | - log.info("countKey decr count -> {} rxpireTime -> {}", count, rxpireTime); | |
70 | - count = redisUtil.decr("countKey", 100); | |
71 | - rxpireTime = redisUtil.getExpire("countKey"); | |
72 | - log.info("countKey decr count -> {} rxpireTime -> {}", count, rxpireTime); | |
73 | - | |
63 | +// log.info("redisUtil.get {} sleep 2s -> {} rxpireTime -> {}", key, JSON.toJSONString(value), rxpireTime); | |
64 | + | |
65 | + Result result = handleMultiProcess("testRedis", paramMap.get("username"), new MultiProcessListener() { | |
66 | + | |
67 | + @Override | |
68 | + @Transactional(timeout = 10) | |
69 | + public Result<?> doProcess() throws Exception { | |
70 | + LambdaUpdateWrapper<SysDataLog> updateWrapper = new UpdateWrapper().lambda(); | |
71 | + updateWrapper.set(SysDataLog::getUpdateTime, new Date()); | |
72 | + updateWrapper.eq(SysDataLog::getId, 10); | |
73 | + Thread.sleep(1000); | |
74 | + sysDataLogService.update(updateWrapper); | |
75 | + return new Result<>(); | |
76 | + } | |
77 | + | |
78 | + }); | |
79 | + | |
80 | +// Long count = redisUtil.decr("countKey", 10); | |
81 | +// rxpireTime = redisUtil.getExpire("countKey"); | |
82 | +// log.info("countKey decr count -> {} rxpireTime -> {}", count, rxpireTime); | |
83 | +// count = redisUtil.decr("countKey", 100); | |
84 | +// rxpireTime = redisUtil.getExpire("countKey"); | |
85 | +// log.info("countKey decr count -> {} rxpireTime -> {}", count, rxpireTime); | |
86 | + | |
74 | 87 | // Thread.sleep(2000l); |
75 | 88 | // count = redisUtil.get("countKey", Long.class); |
76 | 89 | // rxpireTime = redisUtil.getExpire("countKey"); |
... | ... | @@ -95,8 +108,11 @@ public class TestController extends HuahengBaseController { |
95 | 108 | // rxpireTime = redisUtil.getExpire("countKey"); |
96 | 109 | // log.info("countKey sleep 10s get -> {} rxpireTime -> {}", count, rxpireTime); |
97 | 110 | // log.info("countKey hasKey -> {}",redisUtil.hasKey("countKey")); |
98 | - | |
99 | 111 | |
100 | - return Result.OK(count); | |
112 | + return result; | |
113 | + } | |
114 | + | |
115 | + private int generateInt(int min, int max) throws NoSuchAlgorithmException { | |
116 | + return (int)(min + SecureRandom.getInstanceStrong().nextDouble() * (max - min + 1)); | |
101 | 117 | } |
102 | 118 | } |
... | ... |
huaheng-wms-core/src/main/resources/application-dev.yml
huaheng-wms-core/src/main/resources/application-prod.yml
huaheng-wms-core/src/main/resources/application-test.yml