一、异步任务
1.1、创建一个SpringBoot项目,导入web依赖
1.2、创建异步任务
package com.massimo.springboot09task.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
@Async //让Spring知道这是一个异步的方法
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在处理......");
}
}
1.3、Controller
package com.massimo.springboot09task.controller;
import com.massimo.springboot09task.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello(){
asyncService.hello();
return "OK";
}
}
1.4、开启异步注解功能
package com.massimo.springboot09task;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync //开启异步注解功能
@SpringBootApplication
public class Springboot09TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TaskApplication.class, args);
}
}
二、邮件任务
2.1、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.2、在application.properties进行配置
spring.mail.username=xxxxx@qq.com
spring.mail.password=授权码
spring.mail.host=smtp.qq.com
# 开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true
2.3、在测试类中编写一个简单的邮件发送任务
@SpringBootTest
class Springboot09TaskApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//一个简单的邮件
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("你好,马西莫");
mailMessage.setText("感谢一路走来你的帮助");
mailMessage.setTo("xxxxx@qq.com");
mailMessage.setFrom("xxxxx@qq.com");
mailSender.send(mailMessage);
}
}
2.4、一个复杂的邮件发送任务
@Test
void contextLoads2() throws MessagingException {
//一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//正文
helper.setSubject("你好,马西莫");
helper.setText("<p style='color:red'>感谢一路走来你的帮助</p>",true);
//附件
helper.addAttachment("1.jpg",new File("D:\\桌面\\1.jpg"));
helper.setTo("xxxxx@qq.com");
helper.setFrom("xxxxx@qq.com");
mailSender.send(mimeMessage);
}
三、定时任务
3.1、五个了解
TaskScheduler 任务调度者
TaskExecutor 任务执行者
@EnableScheduling 开启定时功能的注解
@Scheduled 什么时候执行
Cron表达式
3.2、开启定时功能
package com.massimo.springboot09task;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableAsync //开启异步注解功能
@EnableScheduling //开启定时功能
@SpringBootApplication
public class Springboot09TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TaskApplication.class, args);
}
}
3.3、编写定时任务
package com.massimo.springboot09task.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduledService {
//在一个特定的时间执行方法
//cron 表达式
//秒 分 时 日 月 周几
//每天的19点50分30秒执行一次 30 50 19 * * ?
//每两秒执行一次 0/2 * * * * ?
@Scheduled(cron = "30 50 19 * * ?")
public void hello(){
System.out.println("定时任务已被执行!");
}
}