异步、定时、邮件发送任务
异步任务
- 同步:当我们执行某个功能时,在没有得到结果之前,这个调用就不能返回!简单点就是说必须 等前一件事做完才能做下一件事;
- 异步:和同步则是相对的,当我们执行某个功能后,我们并不需要立即得到结果,我们额可以正常地 做其他操作,这个功能可以在完成后通知或者回调来告诉我们;例子,后台下载, 我们执行下载功能后,我们就无需去关心它的下载过程,当下载完毕后通知我们就可以了~
1、创建一个service包, 类AsyncService
package com.allen.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @author Allen
* @date 2020/12/30 12:57
*/
@Service
public class AsyncService {
public void hello() throws InterruptedException {
Thread.sleep(3000);
System.out.println("data is processing...");
}
}
2、编写controller包, 编写AsyncController类
package com.allen.controller;
import com.allen.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Allen
* @date 2020/12/30 13:00
*/
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello() throws InterruptedException {
asyncService.hello();
return "OK!";
}
}
3、访问http://localhost:8080/hello进行测试,3秒后出现 OK! ,这是同步等待的情况。
4、要实现异步,需实现以下操作
- 在异步服务方法上加上 @Async 注解
package com.allen.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @author Allen
* @date 2020/12/30 12:57
*/
@Service
public class AsyncService {
@Async
public void hello() throws InterruptedException {
Thread.sleep(3000);
System.out.println("data is processing...");
}
}
- 在启动类上 @EnableAsync 开启异步注解功能
package com.allen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync //开启异步注解功能
@SpringBootApplication
public class Springboot10TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot10TaskApplication.class, args);
}
}
5、重启测试,网页瞬间响应,后台代码依旧执行,实现了异步!
定时任务
现代的应用程序早已不是以前的那些由简单的增删改查拼凑而成的程序了,高复杂性早已是标配,而任务的定时调度与执行也是对程序的基本要求了。
很多业务需求的实现都离不开定时任务,例如,每月一号,移动将清空你上月未用完流量,重置套餐流量,以及备忘录提醒、闹钟等功能
Java 系统中主要有三种方式来实现定时任务:
- Timer和TimerTask
- ScheduledExecutorService
- 三方框架 Quartz
TaskScheduler //任务调度者
TaskExecutor //任务执行者
@EnableScheduling //开启定时功能注解
@Scheduled //什么时候执行
Cron表达式
- 在核心启动类开启@EnableScheduling定时功能注解
package com.allen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
@EnableAsync //开启异步注解功能
@EnableScheduling //开启定时功能注解
@SpringBootApplication
public class Springboot10TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot10TaskApplication.class, args);
}
}
- 编写ScheduledService类, 开启@Scheduled注解,注意 cron 表达式
package com.allen.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* @author Allen
* @date 2020/12/30 13:23
*/
@Service
public class ScheduledService {
//cron 秒 分 时 日 月 周几
/**
* 0 31 13 * * 0-7 每天的13点31分0秒执行此任务
* 0 0/5 13,14 * * 0-7 每天的13点到14点 每隔5分钟执行一次此任务
* 0-2 * * * * 0-7 每分钟的最初0-2秒,每隔1秒执行一次
*/
@Scheduled(cron = "0-2 * * * * 0-7")
public void hello(){
System.out.println("hello, you are executed!");
}
}
邮件发送任务
1、导入依赖
<!--javax.mail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、编写properties配置
spring.mail.username=1330695688@qq.com
# password 是 qq邮箱 生成的授权码
spring.mail.password=szyivczdzstvihfa
spring.mail.host=smtp.qq.com
# 开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true
3、简单邮件测试
package com.allen;
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;
import org.springframework.mail.javamail.MimeMailMessage;
@SpringBootTest
class Springboot10TaskApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//一个简单的邮件
SimpleMailMessage mailMessage = new SimpleMailMessage();
//主题
mailMessage.setSubject("mail test!");
//正文
mailMessage.setText("hello, this is mail test!");
//发件人
mailMessage.setFrom("1330695688@qq.com");
//收件人
mailMessage.setTo("1330695688@qq.com");
mailSender.send(mailMessage);
}
}
4、测试
5、发送复杂邮件
@Test
void contextLoads2() throws MessagingException {
//一个复杂的邮件
MimeMessage message = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setSubject("!!!test 2!!!!");
helper.setText("<p style='color: red'>hello, this is mail test!</p>",true);
//附件
helper.addAttachment("1.png",new File("C:\\Users\\Allen\\Desktop\\1.png"));
helper.setFrom("1330695688@qq.com");
helper.setTo("1330695688@qq.com");
mailSender.send(message);
}
6、测试