前言:日常工作中,我们难免会遇到许多定时任务,比如,定时发送邮件祝福用户生日快乐,某个时间定时清除某些数据
非常简单,就两个步骤就完事,
第一步:开启定时
第二步:编写任务调度的业务类
package com.itpengwei.idea.job.springbootjob.commer;
import com.itpengwei.idea.job.springbootjob.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author 彭伟
* @date 2018/8/27 16:07
*/
@Component
public class SchedulerTask {
@Autowired
private MailService mailService;
@Value(value = "${mail.username.from}")
private String username;
@Scheduled(cron = "0/10 * * * * ? ")//cron表达式
public void task() {
String msg = "SpringBoot整合定时器发送邮件-----发送时间" + new SimpleDateFormat("HH:mm:ss").format(new Date());
mailService.sendMail("发送者" + username + "----" + msg);
}
@Scheduled(cron = "0/5 * * * * ? ")
public void task1() {
String msg = "任务调度二-----发送时间" + new SimpleDateFormat("HH:mm:ss").format(new Date());
mailService.sendMail("发送者---" + username + "----" + msg);
}
}
没错,这就完事了,就是这么简单
接下来测试一下把: