StartCommand.java
1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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();
}
}
}