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 keys = redisUtil.searchKeys(RedisKeyType.TRAFFIC_CONTROL_APPLY.key); List 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 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 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; } } }