背景
整理下SpringBoot与任务
SpringBoot与异步任务
这种方式平时用的挺多,测试:引入web模块测试,编写service,controller,service假装有在执行某操作,需要耗时3秒
@Service
public class AsyncService {
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据中");
}
@RestController
public class AsyncController {
@Autowired
private AsyncService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();
return "成功";
}
}
这样调用的话,请求会转圈3秒才给用户返回,那么想立马返回,就用异步的方式,把给service方法加上异步注解,启动类开启异步。
//告诉Spring这是一个异步方法
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据中");
}
@EnableAsync
@SpringBootApplication
public class AsydemoApplication {
public static void main(String[] args) {
SpringApplication.run(AsydemoApplication.class, args);
}
}
这样调用,用户会立马接收到结果(调用多个service方法时也不会让用户等待),而service里的操作也会默默执行。
SpringBoot与定时任务
定时任务也是挺常用的,比如需要在每天凌晨的时候,分析一次前一天的日志信息。
上代码,方法上加上Scheduled注解,启动类加上EnableScheduling注解。
@Service
public class ScheduledService {
//second(秒) minute(分) hour(时) day of month(日) month(月) and day of week(周几)
@Scheduled(cron = "* * * * * *")
public void hello(){
System.out.println("hello");
}
}
@EnableScheduling
@SpringBootApplication
public class AsydemoApplication {
public static void main(String[] args) {
SpringApplication.run(AsydemoApplication.class, args);
}
}
这里Scheduled的主要参数可以参考文章:@Scheduled的参数
SpringBoot与邮件任务
直接点,先引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
老样子,看一下它的自动配置


我这就不一个个贴代码了,自行阅读源码。
做一个qq邮箱给163邮箱发邮件的实现
看完源码,先配置一波,要用qq邮箱发邮件,就得配置qq邮箱呗。在配置之前,先登录自己的qq邮箱,把服务开一下

yml配置:注意password填的是生成的授权码,不是qq密码
spring:
mail:
host: smtp.qq.com
username: 321208698@qq.com
password: nknumuzujsuzbhgg
单元测试一下
@Autowired
private JavaMailSender javaMailSender;
@Test
void contextLoads() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setSubject("通知-公司决策");
simpleMailMessage.setText("经公司决策,技术岗全部在家办公,请保持电话畅通");
simpleMailMessage.setTo("zo10010@163.com");
simpleMailMessage.setFrom("321208698@qq.com ");
javaMailSender.send(simpleMailMessage);
}
然后我的163邮箱就收到了消息。

关于复杂类型邮件的发送,上代码
@Test
void contextLoads1() {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setSubject("通知-公司决策");
//setText,第二个参数(html)默认是false,给改为true就识别html了
mimeMessageHelper.setText("<a style='color:red'>经公司决策,技术岗全部在家办公,请保持电话畅通</a>",true);
//setTo可以填多个地址
mimeMessageHelper.setTo("zo10010@163.com");
mimeMessageHelper.setFrom("321208698@qq.com");
//附件
mimeMessageHelper.addAttachment("1.jpg",new File("C:\\Users\\Administrator\\Pictures\\1.jpg"));
mimeMessageHelper.addAttachment("2.jpg",new File("C:\\Users\\Administrator\\Pictures\\2.jpg"));
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
效果:


本文介绍SpringBoot中的异步任务、定时任务及邮件任务实现方法。包括异步任务的使用、定时任务的配置与运行,以及如何发送普通邮件与富文本邮件。
1781

被折叠的 条评论
为什么被折叠?



