Commit 051f2d4c1acd49bdf3902f4c2c7597b8b48a2949
1 parent
bdc81be6
RedisUtils 工具类验证完成,分环境设定定时任务
Signed-off-by: TanYibin <5491541@qq.com>
Showing
6 changed files
with
112 additions
and
122 deletions
huaheng-wms-core/src/main/java/org/jeecg/modules/message/job/SendMsgJob.java
... | ... | @@ -31,7 +31,7 @@ public class SendMsgJob implements Job { |
31 | 31 | @Override |
32 | 32 | public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { |
33 | 33 | |
34 | - log.info(String.format("Jeecg-Boot 发送消息任务 SendMsgJob!时间:" + DateUtils.getTimestamp())); | |
34 | + log.info(String.format("Jeecg-Boot 发送消息任务 SendMsgJob 开始 时间:" + DateUtils.getTimestamp())); | |
35 | 35 | |
36 | 36 | // 1.读取消息中心数据,只查询未发送的和发送失败不超过次数的 |
37 | 37 | QueryWrapper<SysMessage> queryWrapper = new QueryWrapper<SysMessage>(); |
... | ... | @@ -66,7 +66,7 @@ public class SendMsgJob implements Job { |
66 | 66 | // 发送结果回写到数据库 |
67 | 67 | sysMessageService.updateById(sysMessage); |
68 | 68 | } |
69 | - log.info(String.format("Jeecg-Boot 发送消息任务 SendMsgJob 结束")); | |
69 | + log.info(String.format("Jeecg-Boot 发送消息任务 SendMsgJob 结束 时间:" + DateUtils.getTimestamp())); | |
70 | 70 | } |
71 | 71 | |
72 | 72 | } |
... | ... |
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 | 3 | import java.util.Map; |
4 | +import java.util.concurrent.ConcurrentHashMap; | |
4 | 5 | |
5 | 6 | import javax.servlet.http.HttpServletRequest; |
6 | 7 | |
7 | 8 | import org.jeecg.common.api.vo.Result; |
8 | 9 | import org.jeecg.common.aspect.annotation.AutoLog; |
10 | +import org.jeecg.modules.wms.receipt.receiptHeader.entity.ReceiptHeader; | |
9 | 11 | import org.jeecg.utils.HuahengRedisUtil; |
10 | 12 | import org.springframework.beans.factory.annotation.Autowired; |
11 | 13 | import org.springframework.web.bind.annotation.PostMapping; |
... | ... | @@ -15,6 +17,7 @@ import org.springframework.web.bind.annotation.ResponseBody; |
15 | 17 | import org.springframework.web.bind.annotation.RestController; |
16 | 18 | |
17 | 19 | import com.alibaba.fastjson.JSON; |
20 | +import com.alibaba.fastjson.TypeReference; | |
18 | 21 | |
19 | 22 | import lombok.extern.slf4j.Slf4j; |
20 | 23 | |
... | ... | @@ -31,15 +34,69 @@ public class TestController extends HuahengBaseController { |
31 | 34 | @Autowired |
32 | 35 | private HuahengRedisUtil redisUtil; |
33 | 36 | |
34 | - @AutoLog(value = "TestController-testRedis") | |
37 | +// @AutoLog(value = "TestController-testRedis") | |
35 | 38 | @ResponseBody |
36 | 39 | @PostMapping(value = "/testRedis") |
37 | - public Result<?> testRedis(@RequestBody Map<String, String> paramMap, HttpServletRequest request) { | |
40 | + public Result<?> testRedis(@RequestBody Map<String, String> paramMap, HttpServletRequest request) throws InterruptedException { | |
38 | 41 | |
39 | - log.info(JSON.toJSONString(paramMap)); | |
40 | - redisUtil.set("testKey", "testValue"); | |
41 | - String result = redisUtil.get("testKey"); | |
42 | - log.info("redisUtil.get -> " + result); | |
43 | - return Result.OK(result); | |
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("11111111111"); | |
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)); | |
51 | +// Long rxpireTime = redisUtil.getExpire(key); | |
52 | +// log.info("rxpireTime -> " + rxpireTime); | |
53 | +// redisUtil.expire(key, 100); | |
54 | +// 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 | + | |
74 | + Thread.sleep(2000l); | |
75 | + count = redisUtil.get("countKey", Long.class); | |
76 | + rxpireTime = redisUtil.getExpire("countKey"); | |
77 | + log.info("countKey sleep 2s count -> {} rxpireTime -> {}", count, rxpireTime); | |
78 | + | |
79 | + | |
80 | + count = redisUtil.incr("countKey", 120); | |
81 | + rxpireTime = redisUtil.getExpire("countKey"); | |
82 | + log.info("countKey incr count -> {} rxpireTime -> {}", count, rxpireTime); | |
83 | + count = redisUtil.incr("countKey", 140); | |
84 | + rxpireTime = redisUtil.getExpire("countKey"); | |
85 | + log.info("countKey incr count -> {} rxpireTime -> {}", count, rxpireTime); | |
86 | + | |
87 | + Thread.sleep(2000l); | |
88 | + count = redisUtil.get("countKey", Long.class); | |
89 | + rxpireTime = redisUtil.getExpire("countKey"); | |
90 | + log.info("countKey sleep 2s get -> {} rxpireTime -> {}", count, rxpireTime); | |
91 | + log.info("countKey hasKey -> {}",redisUtil.hasKey("countKey")); | |
92 | + | |
93 | + Thread.sleep(10000l); | |
94 | + count = redisUtil.get("countKey", Long.class); | |
95 | + rxpireTime = redisUtil.getExpire("countKey"); | |
96 | + log.info("countKey sleep 10s get -> {} rxpireTime -> {}", count, rxpireTime); | |
97 | + log.info("countKey hasKey -> {}",redisUtil.hasKey("countKey")); | |
98 | + | |
99 | + | |
100 | + return Result.OK(count); | |
44 | 101 | } |
45 | 102 | } |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengRedisUtil.java
... | ... | @@ -44,10 +44,6 @@ public class HuahengRedisUtil { |
44 | 44 | return JSON.parseObject(jsonString, type); |
45 | 45 | } |
46 | 46 | |
47 | - public static <T> List<T> fromJsonList(String jsonString, Class<T> cls) { | |
48 | - return JSON.parseArray(jsonString, cls); | |
49 | - } | |
50 | - | |
51 | 47 | /** |
52 | 48 | * 将key中存储值的整数值递增 1 |
53 | 49 | * @author TanYibin |
... | ... | @@ -59,7 +55,9 @@ public class HuahengRedisUtil { |
59 | 55 | public Long incr(@Nonnull String key, @Nonnull Integer seconds) { |
60 | 56 | try { |
61 | 57 | Long returnValue = redisTemplate.opsForValue().increment(key); |
62 | - redisTemplate.expire(key, seconds, TimeUnit.SECONDS); | |
58 | + if (returnValue.equals(1l)) { | |
59 | + redisTemplate.expire(key, seconds, TimeUnit.SECONDS); | |
60 | + } | |
63 | 61 | return returnValue; |
64 | 62 | } catch (Exception e) { |
65 | 63 | log.error("操作缓存异常:key:{}", key, e); |
... | ... | @@ -74,9 +72,13 @@ public class HuahengRedisUtil { |
74 | 72 | * @param key |
75 | 73 | * @return |
76 | 74 | */ |
77 | - public Long decr(@Nonnull String key) { | |
75 | + public Long decr(@Nonnull String key, @Nonnull Integer seconds) { | |
78 | 76 | try { |
79 | - return redisTemplate.opsForValue().decrement(key); | |
77 | + Long returnValue = redisTemplate.opsForValue().decrement(key); | |
78 | + if (returnValue.equals(-1l)) { | |
79 | + redisTemplate.expire(key, seconds, TimeUnit.SECONDS); | |
80 | + } | |
81 | + return returnValue; | |
80 | 82 | } catch (Exception e) { |
81 | 83 | log.error("操作缓存异常:key:{}", key, e); |
82 | 84 | } |
... | ... | @@ -137,6 +139,34 @@ public class HuahengRedisUtil { |
137 | 139 | return null; |
138 | 140 | } |
139 | 141 | |
142 | + public <T> T getAndSet(@Nonnull String key, @Nonnull T value, @Nonnull TypeReference<T> type) { | |
143 | + try { | |
144 | + String jsonString = redisTemplate.opsForValue().getAndSet(key, toJson(value)); | |
145 | + redisTemplate.expire(key, 7200, TimeUnit.SECONDS); | |
146 | + if (StringUtils.isEmpty(jsonString)) { | |
147 | + return null; | |
148 | + } | |
149 | + return fromJson(jsonString, type); | |
150 | + } catch (Exception e) { | |
151 | + log.error("操作缓存异常:key:{}", key, e); | |
152 | + } | |
153 | + return null; | |
154 | + } | |
155 | + | |
156 | + public <T> T getAndSet(@Nonnull String key, @Nonnull T value, @Nonnull TypeReference<T> type, @Nonnull Integer seconds) { | |
157 | + try { | |
158 | + String jsonString = redisTemplate.opsForValue().getAndSet(key, toJson(value)); | |
159 | + redisTemplate.expire(key, seconds, TimeUnit.SECONDS); | |
160 | + if (StringUtils.isEmpty(jsonString)) { | |
161 | + return null; | |
162 | + } | |
163 | + return fromJson(jsonString, type); | |
164 | + } catch (Exception e) { | |
165 | + log.error("操作缓存异常:key:{}", key, e); | |
166 | + } | |
167 | + return null; | |
168 | + } | |
169 | + | |
140 | 170 | /** |
141 | 171 | * 设置key的过期时间 |
142 | 172 | * @author TanYibin |
... | ... | @@ -200,13 +230,13 @@ public class HuahengRedisUtil { |
200 | 230 | * @param key |
201 | 231 | * @return |
202 | 232 | */ |
203 | - public <T> T get(@Nonnull String key) { | |
233 | + public <T> T get(@Nonnull String key, @Nonnull TypeReference<T> type) { | |
204 | 234 | try { |
205 | 235 | String result = redisTemplate.opsForValue().get(key); |
206 | 236 | if (StringUtils.isEmpty(result)) { |
207 | 237 | return null; |
208 | 238 | } |
209 | - return fromJson(result, new TypeReference<T>() {}); | |
239 | + return fromJson(result, type); | |
210 | 240 | } catch (Exception e) { |
211 | 241 | log.error("操作缓存异常:key:{}", key, e); |
212 | 242 | } |
... | ... | @@ -243,101 +273,4 @@ public class HuahengRedisUtil { |
243 | 273 | } |
244 | 274 | return false; |
245 | 275 | } |
246 | - | |
247 | - /** | |
248 | - * 设置key,List 过期时间7200(秒) | |
249 | - * @author TanYibin | |
250 | - * @createDate 2023年2月8日 | |
251 | - * @param key | |
252 | - * @param list | |
253 | - */ | |
254 | - public void setList(@Nonnull String key, @Nonnull List list) { | |
255 | - try { | |
256 | - redisTemplate.opsForValue().set(key, toJson(list), 7200, TimeUnit.SECONDS); | |
257 | - } catch (Exception e) { | |
258 | - log.error("操作缓存异常:key:{}", key, e); | |
259 | - } | |
260 | - } | |
261 | - | |
262 | - /** | |
263 | - * 设置key,List,过期时间 | |
264 | - * @author TanYibin | |
265 | - * @createDate 2023年2月8日 | |
266 | - * @param key | |
267 | - * @param list | |
268 | - */ | |
269 | - public void setList(@Nonnull String key, @Nonnull List list, @Nonnull Integer seconds) { | |
270 | - try { | |
271 | - redisTemplate.opsForValue().set(key, toJson(list), seconds, TimeUnit.SECONDS); | |
272 | - } catch (Exception e) { | |
273 | - log.error("操作缓存异常:key:{}", key, e); | |
274 | - } | |
275 | - } | |
276 | - | |
277 | - /** | |
278 | - * 获取key对应的List | |
279 | - * @author TanYibin | |
280 | - * @createDate 2023年2月8日 | |
281 | - * @param <T> | |
282 | - * @param key | |
283 | - * @param clazz | |
284 | - * @return | |
285 | - */ | |
286 | - public <T> List<T> getList(@Nonnull String key, @Nonnull Class<T> clazz) { | |
287 | - try { | |
288 | - String result = redisTemplate.opsForValue().get(key); | |
289 | - if (StringUtils.isEmpty(result)) { | |
290 | - return null; | |
291 | - } | |
292 | - return fromJsonList(result, clazz); | |
293 | - } catch (Exception e) { | |
294 | - log.error("操作缓存异常:key:{}", key, e); | |
295 | - } | |
296 | - return null; | |
297 | - } | |
298 | - | |
299 | - /** | |
300 | - * 设置key,Map 过期时间7200(秒) | |
301 | - * @author TanYibin | |
302 | - * @createDate 2023年2月8日 | |
303 | - * @param key | |
304 | - * @param Map | |
305 | - */ | |
306 | - public void setMap(@Nonnull String key, @Nonnull Map map) { | |
307 | - try { | |
308 | - redisTemplate.opsForValue().set(key, toJson(map), 7200, TimeUnit.SECONDS); | |
309 | - } catch (Exception e) { | |
310 | - log.error("操作缓存异常:key:{}", key, e); | |
311 | - } | |
312 | - } | |
313 | - | |
314 | - public void setMap(@Nonnull String key, @Nonnull Map map, @Nonnull Integer seconds) { | |
315 | - try { | |
316 | - redisTemplate.opsForValue().set(key, toJson(map), seconds, TimeUnit.SECONDS); | |
317 | - } catch (Exception e) { | |
318 | - log.error("操作缓存异常:key:{}", key, e); | |
319 | - } | |
320 | - } | |
321 | - | |
322 | - /** | |
323 | - * 获取key对应的Map | |
324 | - * @author TanYibin | |
325 | - * @createDate 2023年2月8日 | |
326 | - * @param <T> | |
327 | - * @param <S> | |
328 | - * @param key | |
329 | - * @return | |
330 | - */ | |
331 | - public <T, S> Map<T, S> getMap(@Nonnull String key) { | |
332 | - try { | |
333 | - String result = redisTemplate.opsForValue().get(key); | |
334 | - if (StringUtils.isEmpty(result)) { | |
335 | - return null; | |
336 | - } | |
337 | - return fromJson(result, new TypeReference<Map<T, S>>() {}); | |
338 | - } catch (Exception e) { | |
339 | - log.error("操作缓存异常:key:{}", key, e); | |
340 | - } | |
341 | - return null; | |
342 | - } | |
343 | 276 | } |
344 | 277 | \ No newline at end of file |
... | ... |
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
... | ... | @@ -47,7 +47,7 @@ spring: |
47 | 47 | org: |
48 | 48 | quartz: |
49 | 49 | scheduler: |
50 | - instanceName: MyScheduler | |
50 | + instanceName: Scheduler_Test | |
51 | 51 | instanceId: AUTO |
52 | 52 | jobStore: |
53 | 53 | class: org.quartz.impl.jdbcjobstore.JobStoreTX |
... | ... | @@ -65,16 +65,16 @@ spring: |
65 | 65 | jackson: |
66 | 66 | date-format: yyyy-MM-dd HH:mm:ss |
67 | 67 | time-zone: GMT+8 |
68 | - aop: | |
69 | - proxy-target-class: true | |
68 | + jpa: | |
69 | + open-in-view: false | |
70 | 70 | activiti: |
71 | 71 | check-process-definitions: false |
72 | 72 | #启用作业执行器 |
73 | 73 | async-executor-activate: false |
74 | 74 | #启用异步执行器 |
75 | 75 | job-executor-activate: false |
76 | - jpa: | |
77 | - open-in-view: false | |
76 | + aop: | |
77 | + proxy-target-class: true | |
78 | 78 | #配置freemarker |
79 | 79 | freemarker: |
80 | 80 | # 设置模板后缀名 |
... | ... |