#
Junjie
8 天以前 cabc8e23b0b1eb4d256813e3941a4f214b32f4e4
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.zy.core.task;
 
import com.zy.common.utils.RedisUtil;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.TrafficControlDataModel;
import com.zy.core.model.param.OperateTrafficControlParam;
import com.zy.core.thread.TrafficControlThread;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
 
@Slf4j
@Component
public class TrafficApplyProcess {
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Scheduled(cron = "0/3 * * * * ? ")
    public void processApply() {
        TrafficControlThread trafficControlThread = (TrafficControlThread) SlaveConnection.get(SlaveType.TrafficControl, 1);
        if (trafficControlThread == null) {
            return;
        }
        Set<String> keys = redisUtil.searchKeys(RedisKeyType.TRAFFIC_CONTROL_APPLY.key);
 
        List<ApplyKey> sortList = new ArrayList<>();
        for (String key : keys) {
            String[] split = key.split(RedisKeyType.TRAFFIC_CONTROL_APPLY.key);
            String[] split1 = split[1].split("_");
            String shuttleNo = split1[0];
 
            int count = 0;
            String applyCountKey = RedisKeyType.TRAFFIC_CONTROL_SHUTTLE_APPLY_COUNT.key + shuttleNo;
            Object object = redisUtil.get(applyCountKey);
            if(object != null) {
                count = (Integer) object;
            }
 
            sortList.add(new ApplyKey(key, count));
        }
 
        sortList.sort(Comparator.comparing(ApplyKey::getCount).reversed());
 
        for (ApplyKey applyKey : sortList) {
            String key = applyKey.getKey();
            TrafficControlDataModel dataModel = (TrafficControlDataModel) redisUtil.get(key);
            redisUtil.del(key);
 
            boolean apply = trafficControlThread.processApply(dataModel);
 
            String applyCountKey = RedisKeyType.TRAFFIC_CONTROL_SHUTTLE_APPLY_COUNT.key + dataModel.getShuttleNo();
            if (apply) {
                redisUtil.del(applyCountKey);
            }else {
                Object object = redisUtil.get(applyCountKey);
                if (object == null) {
                    redisUtil.set(applyCountKey, 0, 60 * 60);
                }else {
                    Integer count = (Integer) object;
                    redisUtil.set(applyCountKey, count + 1, 60 * 60);
                }
            }
        }
    }
 
    @Scheduled(cron = "0/3 * * * * ? ")
    public void processReport() {
        TrafficControlThread trafficControlThread = (TrafficControlThread) SlaveConnection.get(SlaveType.TrafficControl, 1);
        if (trafficControlThread == null) {
            return;
        }
        Set<String> keys = redisUtil.searchKeys(RedisKeyType.TRAFFIC_CONTROL_REPORT_LIST.key);
 
        for (String key : keys) {
            OperateTrafficControlParam param = (OperateTrafficControlParam) redisUtil.get(key);
            redisUtil.del(key);
            boolean apply = trafficControlThread.operateTrafficControl(param);
        }
    }
 
    @Scheduled(cron = "0/3 * * * * ? ")
    public void processCancel() {
        TrafficControlThread trafficControlThread = (TrafficControlThread) SlaveConnection.get(SlaveType.TrafficControl, 1);
        if (trafficControlThread == null) {
            return;
        }
        Set<String> keys = redisUtil.searchKeys(RedisKeyType.TRAFFIC_CONTROL_CANCEL_LIST.key);
 
        for (String key : keys) {
            TrafficControlDataModel param = (TrafficControlDataModel) redisUtil.get(key);
            redisUtil.del(key);
            boolean apply = trafficControlThread.cancelTrafficControl(param.getShuttleNo(), param.getTaskNo());
        }
    }
 
    class ApplyKey{
        @Getter
        String key;
        @Getter
        Integer count;
 
        public ApplyKey(String key, Integer count) {
            this.key = key;
            this.count = count;
        }
 
    }
 
}