基于SpringBoot的异步、邮件、定时任务
[学习视频]:b站狂神springboot篇-51|52|53节
一、异步任务
-
新建一个springboot项目,添加web功能模块
-
新建service层,在service文件夹下新建一个AsyncService类
package com.lxf.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
//告诉springboot这是一个异步方法
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在处理。。。");
}
}
- 新建一个controller层,新建一个AsncController类
package com.lxf.controller;
import com.lxf.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 AsncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello(){
asyncService.hello();//停止三秒,转圈
return "ok";
}
}
- springboot启动类开启异步功能
package com.lxf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
//开启异步注解功能
@EnableAsync
@SpringBootApplication
public class SpringMyTasksApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMyTasksApplication.class, args);
}
}
- 比较可知:如果没开启异步功能注解,访问hello网站需要等待3秒才能访问,提示信息也是三秒后打印(模拟任务)。开启之后立刻就能访问网站,提示信息也是三秒后打印。适用于不需要立刻作用于前端的业务。
二、邮件任务
2.1、163邮箱测试
- 导入依赖
<!--发送邮件依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
-
application.properties配置信息
-
163邮箱配置:
spring.mail.username=自己的邮箱账户名 spring.mail.password=授权码 spring.mail.host=smtp.163.com spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true # SSL Config spring.mail.port=465 spring.mail.protocol=smtp spring.mail.default-encoding=UTF-8 spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.smtp.socketFactory.port=465 spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
2.QQ邮箱配置
spring.mail.username=自己的邮箱账户名 spring.mail.password=授权码 spring.mail.host=smtp.qq.com #开启加密验证(QQ邮箱需要) spring.mail.properties.mail.smtp.ssl.enable=true
-
-
测试类
package com.lxf;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
@SpringBootTest
class SpringMyTasksApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//发送哦一个简单的邮件
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//邮件主题
simpleMailMessage.setSubject("例行通知");
//正文内容
simpleMailMessage.setText("明天放假!");
//邮件接收的邮箱名
simpleMailMessage.setTo("xxx@qq.com");
//邮件发送名<发送的邮箱名>
simpleMailMessage.setFrom(new String(("Boss<xxx@163.com>")
.getBytes("UTF-8")));
mailSender.send(simpleMailMessage);
}
@Test
void contextLoads2() throws UnsupportedEncodingException, MessagingException {
//一个复杂邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);//true:开启多文件发送
//标题
helper.setSubject("紧急通知");
//正文:发送一段html代码
helper.setText("<p style='color:red;font-size:32px;font-weight:bold;'>业务紧急,明天不放假了,加班!!!</p>",true);
//附件
helper.addAttachment("鼬神.jpg", new File("D:\\壁纸\\鼬神\\写轮眼.jpg"));
//邮件接收的邮箱名
helper.setTo("xxx@qq.com");
//邮件发送名<发送的邮箱名>
helper.setFrom(new String(("Boss<xxx@163.com>")
.getBytes("UTF-8")));
mailSender.send(mimeMessage);
}
}
三、定时任务
1. 在service层下新建一个SchduleService类:
package com.lxf.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduleService {
//在一个特定的时间执行这行代码
//cron表达式
//秒 分 时 日 月 周几
/*
30 15 10 * * ? :每天的10点15分30 执行一次
30 0/5 10,18 * * ? :每天10点和18点,每隔5分钟执行一次
0 15 10 ? * 1-6 :每个月的周一到周六的10点15分准时执行一次
*/
@Scheduled(cron = "0 25 16 * * ?")
public void hello(){
System.out.println("hello,你被执行了");
}
}
2.springboot启动类开启异步功能
package com.lxf;
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 SpringMyTasksApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMyTasksApplication.class, args);
}
}