一、异步任务

两个注解: @EnableAysns、@Aysnc

代码示例

1
2
3
4
5
6
7
8
9
@EnableAsync // 开启异步注解功能
@SpringBootApplication
public class SpringBoot04TaskApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBoot04TaskApplication.class, args);
}

}

【AsyncService.java】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Service
public class AsyncService {

// 告诉spring这是一个异步方法
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据中....");
}
}

【AsyncController.java】

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
public class AsyncController {

@Autowired
private AsyncService asyncService;

@GetMapping(value = "/hello")
public String hello(){
asyncService.hello();
return "success";
}
}

二、定时任务

两个注解: @EnableScheduling、 @Scheduled

cron 表达式:

image-20201003104138889

image-20201003104149841

开启基于注解的定任务

1
2
3
4
5
6
7
8
9
@EnableScheduling // 开启基于注解的定时任务
@SpringBootApplication
public class SpringBoot04TaskApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBoot04TaskApplication.class, args);
}

}

【ScheduledService.java】

second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几)

对应:0 * * * * MON-FR

【0 0/5 14,18 * * ?】 每天 14 点整,和 18 点整,每隔 5 分钟执行一次

【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 点期间,每个整点都执行一次;

1
2
3
4
5
6
7
8
9
10
11
@Service
public class ScheduledService {

//@Scheduled(cron = "0 * * * * MON-SAT") // 整点执行一次
// @Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT") // 0,1,2,3,4 各执行一次
// @Scheduled(cron = "0-4 * * * * MON-SAT") //0-4执行
@Scheduled(cron = "0/4 * * * * MON-SAT") // 每4秒执行一次
public void hello(){
System.out.println("hello...");
}
}

三、邮件任务

① 导入 pom 依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

② 配置 application.properties

1
2
3
4
spring.mail.username=// 自己的邮箱
spring.mail.password=
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true

spring.mail.password 怎么获取:(以 QQ 邮箱为例)

登入 QQ 邮箱

image-20201003111219977

③ 测试

基本格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Autowired
JavaMailSenderImpl javaMailSender;

@Test
void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage();

// 邮件设置
message.setSubject("通知-今晚开会");
message.setText("今晚7:30开会");

// 邮件接收
message.setTo("2640379231@qq.com");
// 邮件发送
message.setFrom("2097291754@qq.com");

javaMailSender.send(message);
}

image-20201003112703389

复杂格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Autowired
JavaMailSenderImpl javaMailSender;

@Test
public void test02() throws MessagingException {
// 1.创建一个复杂的消息邮件
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

// 邮件设置
helper.setSubject("通知-今晚开会");
// 开启html 渲染,默认是false
helper.setText("<b style='color:red'>今天 7:30 开会</b>", true);

helper.setTo("2640379231@qq.com");
helper.setFrom("2097291754@qq.com");

// 上传文件
helper.addAttachment("1.jpg",new File("C:\\Users\\hp\\Desktop\\OY\\图片\\1.jpg"));
helper.addAttachment("2.jpg",new File("C:\\Users\\hp\\Desktop\\OY\\图片\\2.jpg"));

javaMailSender.send(mimeMessage);

}

image-20201003113437850