First, asynchronous tasks
Asynchronous: When they find a method is asynchronous time, this will suspend the task, the code after the execution, when the asynchronous task that can be performed in a callback method
springboot asynchronous:
-
@EnableAsync
-
@Async
在主程序添加注解
@EnableAsync //开启异步注解
@SpringBootApplication
public class Springboot04TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot04TaskApplication.class, args);
}
}
在相应的方法加上 @Async注解
Second, regular tasks
The main task is timed two notes:
-
@EnableScheduling // open the regular tasks based annotation
-
@Scheduled
Timing core expression:
Field | allowance | Allow special characters |
---|---|---|
second | 0-59 | , - * / |
Minute | 0-59 | , - * / |
hour | 0-23 | , - * / |
date | 1-31 | , - * ? / L W C |
month | 1-12 | , - * / |
week | 0-7 or SUN-SAT 0,7 is SUN | , - * ? / L C # |
Special characters | On behalf of the meaning of |
---|---|
, | enumerate |
- | Interval |
* | Arbitrarily |
/ | Steps |
? | Day / week match conflict |
L | At last |
W | Working day |
C | The calculated value of the contact and calendar |
# | Week, 4 # 2, 2nd Thursday |
/**
* econd(秒), minute(分), hour(时), day of month(日), month(月), and day of week(周).
* 【0 0/15 14,18 * * ?】每天14和18点整,每隔15分钟执行一次
* 【0 15 10 ? * 1-6】 每月的周一至周六10:15分执行一次
* 【0 0 2 ? * 6L】 每个月的最后一个周六凌晨2点执行一次
* 【0 0 2 LW * ?】每个月的最后一个工作日凌晨2点执行一次
* 【0 0 2-4 ? * 1#1】每个月的第一个周一凌晨2点到4点期间,每个整点执行一次
*/
@Scheduled(cron = "0 * * * * MON-TUE")
public void hello(){
System.out.println("hello.......");
}
Third, Mail task
1, Simple Mail sent
void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage();
//邮件设置
message.setSubject("通知-今晚开会");//标题
message.setText("今晚7:00开会");//内容
message.setFrom("[email protected]");//发送的邮箱
message.setTo("[email protected]");//接收的邮箱
mailSender.send(message);
}
配置文件
[email protected]
spring.mail.password=tuhutzkdbcxodefj
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true//开启ssl安全连接
2, send e-mail attachments
void contextLoads() {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setSubject("通知-今晚开会");
helper.setText("<h1>今晚7:00开会</h1>",true);
helper.setFrom("[email protected]");
helper.setTo("[email protected]");
helper.addAttachment("1.jpg", new File("C:\\Users\\17933\\Pictures\\Saved Pictures\\1.jpg"));
helper.addAttachment("2.jpg", new File("C:\\Users\\17933\\Pictures\\Saved Pictures\\2.jpg"));
mailSender.send(mimeMessage);
}