共计 2752 个字符,预计需要花费 7 分钟才能阅读完成。
一、异步任务
在 Java 应用中,绝大多数情况下都是通过同步的方式来实现交互处理的;但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在 Spring 3.x 之后,就已经内置了 @Async 来完美解决这个问题。
1. 启动器上添加 @EnableAsync 注解
@EnableScheduling // 开启注解的定时任务 | |
@SpringBootApplication | |
public class Springboot04TaskApplication {public static void main(String[] args) {SpringApplication.run(Springboot04TaskApplication.class, args); | |
} | |
} |
2.service 添加注解
@Service | |
public class AsyncService { | |
// 告诉 Spring 这是一个异步任务 | |
@Async | |
public void hello(){ | |
try {Thread.sleep(3000); | |
} catch (InterruptedException e) {e.printStackTrace(); | |
} | |
System.out.println("处理数据中..."); | |
} | |
} |
3.controller
@RestController | |
public class AsyncController { | |
@Autowired | |
AsyncService asyncService; | |
@GetMapping("/hello") | |
public String hello(){asyncService.hello(); | |
return "Success"; | |
} | |
} |
访问效果:
若果没有开启异步,访问 /hello 3 秒后,控制台打印“处理数据中...”以及返回“Success”。开启后:访问 /hello 直接返回“Success”,3 秒后控制台打印返回“Success”。
二、定时任务
1. 启动器上添加 @EnableScheduling 注解
2. 需要执行的方法添加 @Scheduled(cron=””)
@Service | |
public class ScheduledService { | |
/** | |
* second(秒) , minute(分), hour(时), day of month(日), month(月),day of week(周几). | |
* | |
* 0 * * * * MON-FRI | |
* 周一到周五,每月,每日,每时,每分,整秒启动一次(周一到周五每分钟启动 一次)* * * * * * MON-FRI 周一到周五每秒启动一次 | |
* | |
* 例子:* | |
*【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 点期间,每个整点都执行一次;*/ | |
// @Scheduled(cron = "0 * * * * SUN-SAT") | |
// @Scheduled(cron = "0,1,2,3,4 * * * * SUN-SAT") // 枚举 0,1,2,3,4 这些秒会运行 | |
// @Scheduled(cron = "0-4 * * * * SUN-SAT") // 区间 0-4 秒都会运行 | |
@Scheduled(cron = "0/4 * * * * SUN-SAT") // 步长 每隔 4 秒启动一次 | |
public void hello(){System.out.println("hello service ......"); | |
} | |
} |
3.cron 表达式
三、邮件任务
1. 配置
添加依赖
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-mail</artifactId> | |
</dependency> |
yml 配置
spring: | |
mail: | |
host: smtp.qq.com | |
username: 626532990@qq.com | |
password: qfrbjksqyqmgbdgc # 设置密码,该处的密码是 QQ 邮箱开启 SMTP 的授权码而非 QQ 密码 | |
properties: | |
mail: | |
smtp: | |
auth: true # 需要验证登录名和密码 | |
starttls: | |
enable: true # 需要 TLS 认证 保证发送邮件安全验证 | |
required: true |
2. 发送简单邮件
@Autowired | |
private JavaMailSenderImpl mailSender; | |
@Value("${spring.mail.username}") | |
public String sendFrom; | |
@Test | |
public void sendSimpleEmail(){SimpleMailMessage message = new SimpleMailMessage(); | |
// 邮件设置 | |
message.setSubject("通知:今晚通宵敲代码"); | |
message.setText("今天 19:30 加班"); | |
message.setTo("481345827@qq.com"); | |
message.setFrom(sendFrom); | |
mailSender.send(message); | |
} |
3. 发送复杂邮件(带附件)
@Test | |
public void sendComplexEmail() throws Exception{ | |
//1. 创建一个复杂的消息邮件 | |
MimeMessage mimeMessage = mailSender.createMimeMessage(); | |
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);// 打开上传文件 | |
// 邮件设置 | |
helper.setSubject("通知:今晚通宵敲代码(带附件)"); | |
helper.setText("<b style='color:red'> 今天 19:30 加班 </b>",true); // 这段内容是 html | |
helper.setTo("481345827@qq.com"); | |
helper.setFrom(sendFrom); | |
// 上传文件E:\ 图片 \Camera Roll\73378512_p0.jpg | |
helper.addAttachment("1.jpg",new File("E:\\ 图片 \\Camera Roll\\69073572_p0.jpg")); | |
helper.addAttachment("2.jpg",new File("E:\\ 图片 \\Camera Roll\\73378512_p0.jpg")); | |
mailSender.send(mimeMessage); | |
} |
正文完