任务调度简介
会员生日提醒
信用卡账单通知
花呗还款通知
每月月底考勤统计
活动开始通知
间隔多久同步数据
有一定的规律,重复执行,使用任务调度框架(定时任务)
主流的技术:
1.Spring Task
2.Quartz
3.Elastic-Job
Spring Task
Spring提供的一种定时任务,简单快捷!
2个注解(@EnableScheduling :修饰在开关类,@Scheduled:修饰要重复执行的方法,并且设置cron属性)+
1个表达式(CRON表达式)
CRON表达式
用来定义定时任务的时间格式的表达式
格式组成:秒 分 时 日 月 周 年
其中年可以省略,其余必须存在
在线生成CRON表达式
开关类开启定时任务
@SpringBootApplication
@MapperScan(basePackages ="com.qf.day0616.dao" )
@EnableScheduling//开启定时任务
public class Day0616Application {
public static void main(String[] args) {
SpringApplication.run(Day0616Application.class, args);
}
}
任务类任务书写
/**
* @Author LXM
* @Date 2020/6/16 0016
*/
@Component
public class TStudentTask {
@Resource
private ItStudentDao studentDao;
//会员生日提醒
//每天早上8点开始执行任务
@Scheduled(cron="0 0 8 * * ?")
public void brithday(){
List<TStudent> list=studentDao.getBrithday(new SimpleDateFormat("MM-dd").format(new Date()));
//打印信息到控制台(正常操作是获取手机号或邮箱给会员发短信或者邮件)
list.stream().forEach(System.out::println);
}
}
Dao层sql语句书写
public interface ItStudentDao extends BaseMapper<TStudent> {
//查询表中生日为今天的所有人员信息
@Select("select * from t_student where date_format(birthday,'%m-%d')=#{currDay}")
List<TStudent> getBrithday(String currDay);
}