MainTimer.java 2.4 KB
package com.myproject.webapp.controller.webService;

import com.myproject.bean.qisda.InquiryShelfBean;
import com.myproject.bean.update.DataLog;
import com.myproject.dao.mongo.IDataLogDao;
import com.myproject.webapp.controller.qisda.util.OutInfoCache;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by sunke on 2020/3/5.
 * 主定时器
 */
@Service
public class MainTimer {

    protected final transient Logger log = LogManager.getLogger(getClass());

    ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(1);

    @Autowired
    private OutInfoCache outInfoCache;

    @Autowired
    private QisdaCache qisdaCache;

    @Autowired
    private IDataLogDao dataLogDao;

    @Autowired
    private ITaskService taskService;

    public void init(){

        String currentorderHSerial = qisdaCache.getCurrentOrderHSerial();
        log.info("加载未完成的任务,当前执行需求单["+currentorderHSerial+"]");
        List<DataLog> unExecuteTasks = dataLogDao.findUnFinishedTasks(currentorderHSerial);
        for (DataLog unExecuteTask : unExecuteTasks) {
            if(unExecuteTask.isExecuting() || unExecuteTask.isWait()){
                taskService.addTaskToExecute(unExecuteTask);
            }else{
                taskService.updateFinishedTask(unExecuteTask);
            }
        }

        log.info("加载DN单绑定信息和出库料架信息");
        qisdaCache.initRfidDnMap();
        InquiryShelfBean.initShelfMap();

        QisdaCache.initApiRequestMap();


        outInfoCache.loadUnEndOutInfos();
        log.info("主定时器开启,60秒后开始执行, 每10s执行一次");
        //1 分钟之后执行,每秒钟执行一次
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                timerTask();
            }
        }, 60, 10, TimeUnit.SECONDS);
    }


    private void timerTask(){
        try{
            QisdaCache.runTimer();
            outInfoCache.runTimer();
        }catch (Exception e){
            log.error("定时器执行出错",e);
        }finally {

        }
    }



}