记录一下定时任务
public class TestScheduled implements ApplicationRunner {
@Autowired
CommonService commonService;
@Value("${time.czxx.point}")
private String point;
long oneDay = 24 * 60 * 60 * 1000;
ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1,new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
static List<FCpMemberJ> czxxList = new ArrayList<>();
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("加载##获取税务咨询信息##定时任务.......");
long initDelay = getTimeMillis(point) - System.currentTimeMillis();//08:10:00
initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;
service.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("ttt");
}
}, initDelay, oneDay, TimeUnit.MILLISECONDS);
log.info("##获取税务咨询信息##定时任务加载成功.......");
}
/**
* 获取指定时间对应的毫秒数
*
* @param time "HH:mm:ss"
* @return
*/
private static long getTimeMillis(String time) {
try {
DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");
Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);
return curDate.getTime();
} catch (ParseException e) {
log.error("getTimeMillis catch exception.");
e.printStackTrace();
}
return 0;
}
}