StartCommand.java 1.37 KB
package com.bsth;


import com.bsth.thread.ReportCalculationThrad;
import com.bsth.util.DateUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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());
	
	private static long timeDiff;
	
	@Autowired
	ReportCalculationThrad mileageCalculationThrad;
	
	static {
		// 早上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.schedule(mileageCalculationThrad, 1, TimeUnit.SECONDS);//timeDiff / 1000
			mainServices.scheduleAtFixedRate(mileageCalculationThrad, timeDiff / 1000, 60 * 60 * 24, TimeUnit.SECONDS);//timeDiff / 1000
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}