009374ed
Hugiee.Liu
fea: create
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package com.objecteye.handle;
import com.alibaba.nacos.common.utils.ExceptionUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.objecteye.content.ViidCollectorRedisKey;
import com.objecteye.handle.viid.VIIDCommonHandle;
import com.objecteye.pojo.platform.DeviceRequestParams;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.function.Consumer;
/**
* @author: liuhaoyu
* @date: 2023/6/30
*/
@Component
@RequiredArgsConstructor
public class StartUpAndShutdown implements DisposableBean, ApplicationRunner {
private final VIIDCommonHandle viidCommonHandle;
private final StringRedisTemplate stringRedisTemplate;
private final ObjectMapper objectMapper;
private final Logger logger = LoggerFactory.getLogger(StartUpAndShutdown.class);
@Override
public void destroy() throws Exception {
allCacheHandle(viidCommonHandle::unregister);
}
@Override
public void run(ApplicationArguments args) throws Exception {
allCacheHandle(viidCommonHandle::register);
}
private void allCacheHandle(Consumer<DeviceRequestParams> func) {
try (Cursor<Map.Entry<Object, Object>> cursor = stringRedisTemplate.boundHashOps(ViidCollectorRedisKey.DEVICE_CACHE)
.scan(ScanOptions.scanOptions().build())) {
while (cursor.hasNext()) {
Map.Entry<Object, Object> entry = cursor.next();
try {
func.accept(objectMapper.readValue(entry.getValue().toString(), DeviceRequestParams.class));
} catch (Exception e) {
logger.error(ExceptionUtil.getAllExceptionMsg(e));
}
}
}
}
}
|