XDApplication.java 5.74 KB
package com.bsth;

import com.bsth.data.BasicData;
import com.bsth.data.ThreadMonotor;
import com.bsth.data.car_out_info.UpdateDBThread;
import com.bsth.data.directive.DirectivesPstThread;
import com.bsth.data.gpsdata.thread.GpsDataLoaderThread;
import com.bsth.data.gpsdata.thread.OfflineMonitorThread;
import com.bsth.data.schedule.edit_logs.SeiPstThread;
import com.bsth.data.schedule.late_adjust.ScheduleLateThread;
import com.bsth.data.schedule.thread.CalcOilThread;
import com.bsth.data.schedule.thread.SchedulePstThread;
import com.bsth.data.schedule.thread.ScheduleRefreshThread;
import com.bsth.data.schedule.thread.SubmitToTrafficManage;
import com.bsth.util.DateUtils;
import com.bsth.util.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 线调大部分服务都在这里启动
 * Created by panzhao on 2017/5/14.
 */
@Component
public class XDApplication implements CommandLineRunner {

    Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    BasicData.BasicDataLoader basicDataLoader;
    @Autowired
    UpdateDBThread fcxxUpdateThread;
    @Autowired
    GpsDataLoaderThread gpsDataLoader;
    @Autowired
    OfflineMonitorThread offlineMonitorThread;
    @Autowired
    ScheduleRefreshThread scheduleRefreshThread;
    @Autowired
    SchedulePstThread schedulePstThread;
    @Autowired
    ScheduleLateThread scheduleLateThread;
    @Autowired
    SubmitToTrafficManage submitToTrafficManage;
    @Autowired
    CalcOilThread calcOilThread;
    @Autowired
    DirectivesPstThread directivesPstThread;
    @Autowired
    ThreadMonotor threadMonotor;
    @Autowired
    SeiPstThread seiPstThread;

    private static long timeDiff;
    private static long timeDiffTraffic;

    static {
        // 早上2:20
        timeDiff = (DateUtils.getTimestamp() + 1000 * 60 * 140) - System.currentTimeMillis();
        if (timeDiff < 0)
            timeDiff += (1000 * 60 * 60 * 24);
        // 早上07:00
        timeDiffTraffic = (DateUtils.getTimestamp() + 1000 * 60 * 60 * 7) - System.currentTimeMillis();
        if (timeDiffTraffic < 0)
            timeDiffTraffic += (1000 * 60 * 60 * 24);
    }

    @Override
    public void run(String... strings) throws Exception {
        try {
            Tools tools = new Tools("application.properties");
            String environment = tools.getValue("spring.profiles.active");
            //预先加载基础的对照数据
            basicDataLoader.loadAllData();
            switch (environment){
                case "dev":
                    devInit();
                    break;
                case "prod":
                    prodInit();
                    break;
            }
        }catch (Exception e){
            log.error("线调后台启动出现异常!!", e);
            System.exit(1);
        }
    }

    //@Autowired
    //DayOfSchedule dayOfSchedule;
    public void devInit(){
        log.info("devInit...");
        ScheduledExecutorService sexec = Application.mainServices;
        //抓取GPS数据
        gpsDataLoader.setFlag(-1);
        //dayOfSchedule.dataRecovery();
        //sexec.scheduleWithFixedDelay(gpsDataLoader, 30, 2, TimeUnit.SECONDS);
        //实际排班更新线程
        //sexec.scheduleWithFixedDelay(scheduleRefreshThread, 15, 240, TimeUnit.SECONDS);
        //实际排班延迟入库线程
        //sexec.scheduleWithFixedDelay(schedulePstThread, 60, 15, TimeUnit.SECONDS);
        //班次修正日志延迟入库
        //sexec.scheduleWithFixedDelay(seiPstThread, 60, 30, TimeUnit.SECONDS);
        //调度指令延迟入库
        //sexec.scheduleWithFixedDelay(directivesPstThread, 180, 180, TimeUnit.SECONDS);
        //线程监听(防止重要的线程阻塞、异常结束。以及部分主备切换工作)
        //sexec.scheduleWithFixedDelay(threadMonotor, 240, 60, TimeUnit.SECONDS);
    }

    public void prodInit(){
        log.info("prodInit...");
        ScheduledExecutorService sexec = Application.mainServices;
        //发车信息
        sexec.scheduleWithFixedDelay(fcxxUpdateThread, 60, 40, TimeUnit.SECONDS);
        //抓取GPS数据
        sexec.scheduleWithFixedDelay(gpsDataLoader, 30, 2, TimeUnit.SECONDS);
        //GPS设备掉离线
        sexec.scheduleWithFixedDelay(offlineMonitorThread, 120, 60, TimeUnit.SECONDS);
        //班次更新线程
        sexec.scheduleWithFixedDelay(scheduleRefreshThread, 15, 240, TimeUnit.SECONDS);
        //班次延迟入库线程
        sexec.scheduleWithFixedDelay(schedulePstThread, 60, 10, TimeUnit.SECONDS);
        //班次修正日志入库
        sexec.scheduleWithFixedDelay(seiPstThread, 60, 60, TimeUnit.SECONDS);
        //检查班次误点
        sexec.scheduleWithFixedDelay(scheduleLateThread, 60, 30, TimeUnit.SECONDS);
        //调度指令延迟入库
        sexec.scheduleWithFixedDelay(directivesPstThread, 180, 100, TimeUnit.SECONDS);

        //运管处静态数据提交
        log.info(timeDiff / 1000 / 60 + "分钟之后提交到运管处");
        sexec.scheduleAtFixedRate(submitToTrafficManage, timeDiffTraffic / 1000, 60 * 60 * 24, TimeUnit.SECONDS);
        //计算油、公里加注
        sexec.scheduleAtFixedRate(calcOilThread, timeDiff / 1000, 60 * 60 * 24, TimeUnit.SECONDS);

        //线程监听(防止重要的线程阻塞、异常结束。以及部分主备切换工作)
        sexec.scheduleWithFixedDelay(threadMonotor, 240, 60, TimeUnit.SECONDS);
    }
}