Java定时任务
程序员文章站
2022-07-14 18:54:04
...
一:启动线程,开启定时任务
import lombok.AllArgsConstructor;
import org.apache.log4j.Logger;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
public class PersonnelAttendanceThread implements ApplicationRunner {
private static Logger log = Logger.getLogger(PersonnelAttendanceThread.class);
private final DealData dealData;
public String start() {
dealData.uploadPersonnelAttendance();
return "启动人员考勤上传市/区平台线程成功";
}
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("启动人员考勤上传市/区平台线程");
start();
}
}
二:定时任务
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.future.platform.client.TrainingHttpClient;
import com.future.platform.config.TimeUtils;
import com.future.platform.mapper.setting.AttendanceMapper;
import com.future.platform.pojo.setting.person.Attendance;
import lombok.AllArgsConstructor;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
@AllArgsConstructor
public class DealData {
private static Logger log = Logger.getLogger(DealData.class);
private final AttendanceMapper attendanceMapper;
private final TrainingHttpClient trainingHttpClient;
//时间间隔(一天)
private static final long PERIOD_DAY = 24 * 60 * 60 * 1000;
public void uploadPersonnelAttendance() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 1); //凌晨1点开始执行任务
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date date = calendar.getTime(); //第一次执行定时任务的时间
//如果第一次执行定时任务的时间 小于当前的时间
//此时要在 第一次执行定时任务的时间加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。
if (date.before(new Date())) {
date = this.addDay(date, 1);
}
Timer timer = new Timer();
//安排指定的任务在指定的时间开始进行重复的固定延迟执行。
timer.schedule(new TimerTask() {
@Override
public void run() {
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
if (day == 4) {
//每月1号执行
List<Map<String, Object>> resultMapList = new ArrayList<>();
QueryWrapper wrapper = new QueryWrapper();
List<Attendance> attendanceList = attendanceMapper.selectList(wrapper);
if (attendanceList != null && attendanceList.size() > 0) {
for (Attendance attendance : attendanceList) {
Map<String, Object> map = new HashMap<>();
map.put("name", attendance.getName());
map.put("attendance", attendance.getAttendance());
map.put("projectCode", attendance.getProject_code());
map.put("post", attendance.getPost());
map.put("areaId", attendance.getArea_id());
String timeType = "yyyy-MM";
String chinaTime = TimeUtils.getChinaTime(timeType);
map.put("time", chinaTime);
resultMapList.add(map);
}
}
Map<String, Object> map = trainingHttpClient.uploadPersonnelAttendance(resultMapList);
System.out.println(map);
}
}
}, date, PERIOD_DAY);
}
// 增加或减少天数
public Date addDay(Date date, int num) {
Calendar startDT = Calendar.getInstance();
startDT.setTime(date);
startDT.add(Calendar.DAY_OF_MONTH, num);
return startDT.getTime();
}
}
上一篇: 数据库_day4_索引