jinglun-cloud
16 小时以前 1ef1063281497f32fcfa4f14b07d99399c0bb765
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.zy.core.task;
 
import com.zy.asrs.entity.DeviceDataLog;
import com.zy.common.utils.RedisUtil;
import com.zy.core.utils.DeviceLogRedisKeyBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
 
@Slf4j
@Component
public class DeviceAsyncLogPublisher {
 
    private static final String LANE_PREFIX = "device-log-publish-";
    private static final String TASK_NAME = "publish-device-log";
    private static final long LOG_TTL_SECONDS = 60L * 60L * 24L;
    private static final long MIN_INTERVAL_MS = 0L;
 
    private final ConcurrentHashMap<String, AtomicReference<DeviceDataLog>> pendingByLane = new ConcurrentHashMap<>();
 
    @Autowired
    private MainProcessTaskSubmitter mainProcessTaskSubmitter;
    @Autowired
    private RedisUtil redisUtil;
 
    public void publishLatest(DeviceDataLog deviceDataLog) {
        String laneKey = buildLaneKey(deviceDataLog);
        if (laneKey == null) {
            return;
        }
        pendingByLane
                .computeIfAbsent(laneKey, key -> new AtomicReference<>())
                .set(deviceDataLog);
        boolean submitted = mainProcessTaskSubmitter.submitKeyedSerialTask(
                LANE_PREFIX,
                laneKey,
                TASK_NAME,
                MIN_INTERVAL_MS,
                () -> drain(laneKey)
        );
        if (!submitted) {
            log.debug("Skip duplicate device async log publish submit, laneKey={}", laneKey);
        }
    }
 
    private void drain(String laneKey) {
        AtomicReference<DeviceDataLog> pending = pendingByLane.get(laneKey);
        if (pending == null) {
            return;
        }
        while (true) {
            DeviceDataLog deviceDataLog = pending.getAndSet(null);
            if (deviceDataLog == null) {
                return;
            }
            try {
                boolean success = redisUtil.set(DeviceLogRedisKeyBuilder.build(deviceDataLog), deviceDataLog, LOG_TTL_SECONDS);
                if (!success) {
                    pending.compareAndSet(null, deviceDataLog);
                    log.warn("Device async log publish failed, keep latest pending snapshot, type={}, deviceNo={}, stationId={}",
                            deviceDataLog.getType(), deviceDataLog.getDeviceNo(), deviceDataLog.getStationId());
                    return;
                }
            } catch (Exception e) {
                pending.compareAndSet(null, deviceDataLog);
                log.error("Device async log publish error, type={}, deviceNo={}, stationId={}",
                        deviceDataLog.getType(), deviceDataLog.getDeviceNo(), deviceDataLog.getStationId(), e);
                return;
            }
        }
    }
 
    private String buildLaneKey(DeviceDataLog deviceDataLog) {
        if (deviceDataLog == null || deviceDataLog.getType() == null || deviceDataLog.getDeviceNo() == null) {
            return null;
        }
        return deviceDataLog.getType() + ":" + deviceDataLog.getDeviceNo();
    }
}