DataCacheScheduled.java 2.06 KB
package com.viontech.scheduled;

import com.viontech.service.TrafficEventService;
import com.viontech.vo.traffic.LaneModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
public class DataCacheScheduled implements CommandLineRunner {
    //key:apeid,value:TollgateId
    public static Map<String, String> deviceTollgateMap = new ConcurrentHashMap<>();
    @Resource
    private TrafficEventService trafficEventService;

    @Scheduled(cron = "0 0 2 * * ?")
    public void flushCache() {
        updateDeviceTollgateCache();
    }

    @Override
    public void run(String... args) throws Exception {
        updateDeviceTollgateCache();
    }

    private void updateDeviceTollgateCache() {
        log.info("开始更新设备与卡口关系缓存数据");
        //查询
        int offset = 0;
        int limit = 50;
        int selectSize = 0;
        int num = 0;
        do {
            LaneModel laneQry = new LaneModel();
            laneQry.setOffset(offset);
            laneQry.setLimit(limit);
            List<LaneModel> laneModels = trafficEventService.selectLaneDatas(laneQry);
            selectSize = laneModels.size();
            num = num + 1;
            offset = limit*num;
            if (!CollectionUtils.isEmpty(laneModels)) {
                for (LaneModel laneModel : laneModels) {
                    try {
                        deviceTollgateMap.put(laneModel.getApeID(), laneModel.getTollgateID());
                    } catch (Exception e) {
                        log.error("deviceTollgateMap.put Exception", e);
                    }
                }
            }
        } while (selectSize >= limit);
        log.info("结束更新设备与卡口关系缓存,共更新{}条", deviceTollgateMap.size());
    }
}