StartCommand.java 1.64 KB
package com.bsth;


import com.bsth.thread.AverageSpeedThrad;
import com.bsth.util.DateUtils;
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.Calendar;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * 随应用启动运行
 *
 */
@Component
public class StartCommand implements CommandLineRunner {

	public static ScheduledExecutorService mainServices = Executors.newScheduledThreadPool(2);
	
	Logger log = LoggerFactory.getLogger(this.getClass());

	public static String RQ ="";
	
	private static long timeDiff;
	
	@Autowired
	AverageSpeedThrad averageSpeedThrad;

	static {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		RQ = year+"-"+(month<10?"0"+month:month);
		// 早上02:30
		timeDiff = (DateUtils.getTimestamp() + 1000 * 60 * 150) - System.currentTimeMillis();
		if (timeDiff < 0)
			timeDiff += (1000 * 60 * 60 * 24);
	}

	@Override
	public void run(String... arg0){
		
		try {
			log.info(":"+timeDiff / 1000 / 60 + "分钟后统计修正班次和公里报表");
			mainServices.scheduleAtFixedRate(averageSpeedThrad, timeDiff / 1000, 60*60*24, TimeUnit.MINUTES);//timeDiff / 1000
//			mainServices.scheduleAtFixedRate(averageSpeedThrad, 1, 60*60*24, TimeUnit.SECONDS);//timeDiff / 1000
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}