现在越来越多的企业都开始使用springboot,这个框架内嵌tomecat,而且相对于spring中的哪些恶心的不行的xml配置来说,springboot的配置相当少,而且支持更加的广泛。。。
其实在平时的开发过程中,使用定时任务的场景很多,而且应用很广泛,前几天的一个项目中使用springboot,写了一个定时任务,写完才觉得springboot相比spring要简单太多,不需要引包,也不需要额外的配置,只要你在配置中有注册扫描配置就OK,在写代码时其实就和我们普通的写法是一样的,不多说,直接上实例:
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import cn.xdf.assistant.service.AttendanceStatisticsService;
import cn.xdf.assistant.service.ClockRecordService;
@Component
@Configurable
@EnableScheduling
public class ScheduledTasks{
private static Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
@Autowired
ClockRecordService clockRecordService;
@Autowired
AttendanceStatisticsService attendanceStatisticsService;//执行任务类
@Scheduled(fixedRate = 1000 * 30)
public void reportCurrentTime(){
//System.out.println ("Scheduling Tasks Examples: The time is now " + dateFormat ().format (new Date ()));
}
//接收打卡数据
@Scheduled(cron = "0 0/1 * * * ?") //在这里就可以直接设置执行的时间规则
public void synchronousPushCardData(){
Date date1 = new Date();
logger.info("接收打卡数据开始:");
clockRecordService.synchronousPushCardData();
Date date2 = new Date();
logger.info("接收打卡数据结束,用时:{}毫秒",date2.getTime()-date1.getTime());
}
//同步班级课次信息
@Scheduled(cron = "0 0/1 * * * ?")
public void synchronousClassLessonCardData(){
Date date1 = new Date();
logger.info("同步班级课次信息开始:");
clockRecordService.synchronousClassLessonCardData(null);
Date date2 = new Date();
logger.info("同步班级课次信息结束,用时:{}毫秒",date2.getTime()-date1.getTime());
}
//计算考勤信息
@Scheduled(cron = "0 0 23 * * ?")
public void calculationAttendanceDetail(){
Date date1 = new Date();
logger.info("计算考勤信息开始:");
attendanceStatisticsService.calculationAttendance();
Date date2 = new Date();
logger.info("计算考勤信息结束,用时:{}毫秒",date2.getTime()-date1.getTime());
}
}
============== 执行类 =================
因为这个类中的代码过多,我就只拿出一个方法出来作为参考
@Service
public class ClockRecordService{
private static Logger punchClocklogger = LoggerFactory.getLogger("punchClockApi");
private static Logger logger = LoggerFactory.getLogger(ClockRecordService.class);
private static SqlSessionFactory ssf = Config.getSsf();
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Value("${jwaapi.apiAssistantUsedUrl}")
private String apiJWUrl;
@Value("${jwaapi.appId}")
private String appIdJW;
@Value("${scheduledStatus}")
private String scheduledStatus;
/**
* 查询课次信息
* @param in
* @return
*/
public List<XCardLessonFromJW> getXCardLesson (UserClockIn in,String dtDate){
//这里是拿的配置文件中的url拼接成请求进行远程调用
String httpUrl = apiJWUrl+"/assistantInterface/searchAssistantArrangement";
List<XCardLessonFromJW> pclList = new ArrayList<XCardLessonFromJW>();
Map<String, String> params = new HashMap<>();
params.put("appId", appIdJW);
params.put("accessToken", "ef4e3a2b-ae21-4cad-ada8-86ec46a8a83g");
params.put("schoolId", String.valueOf(in.getSchoolId()));
params.put("classCode", in.getClassCode());
params.put("assistantCode", in.getUserCode());
params.put("dtDate", dtDate);
Date d1 = new Date();
//这里主要是看接口制定的是使用什么样的请求方式,当然get和post的请求的区别在这里就不说了
String resultStr = Util.doPost(httpUrl, params);
Date d2 = new Date();
logger.info("查询课次信息,接口地址:{}",httpUrl);
logger.info("请求参数:{}",params);
logger.info("返回结果:{}",resultStr);
logger.info("用时(毫秒):{}",d2.getTime()-d1.getTime());
pclList = Util.fromJson(resultStr, new TypeReference<List<XCardLessonFromJW>>(){});
return pclList;
}
如果说你的离线任务需要更复杂和灵活的处理的话,也可以使用xxl-job,或者其他任务调度平台