异步任务:
创建一个service包:
创建一个类AsyncService:
编写方法,假装正在处理数据,使用线程设置一些延时,模拟同步等待的情况;
@Service
public class AsyncService {
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("业务进行中....");
}
}
编写controller包
编写AsyncController类
写一个Controller测试一下
@RestController
public class AsynController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello() {
asyncService.hello();//停止三秒
return"OK";
}
}
访问http://localhost:8080/hello进行测试,3秒后出现ok,这是同步等待的情况。
给hello方法添加@Async注解;
/告诉Spring这是一个异步方法
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("业务进行中....");
}
SpringBoot就会自己开一个线程池,进行调用
但是要让这个注解生效,我们还需要在主程序上添加一个注解@EnableAsync ,开启异步注解功能;
@EnableAsync //开启异步注解功能
@SpringBootApplication
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class, args);
}
}
重启测试,网页瞬间响应,后台代码依旧执行!
**
定时任务
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨的时候,分析一次前一天的日志信息,Spring为我们提供了异步执行任务调度的方式,提供了两个接口。
TaskExecutor接口
TaskScheduler接口
两个注解:
@EnableScheduling//开启定时的注解
@Scheduled//spring定时器
关于**@Scheduled**: 请点击我
创建一个ScheduledService
我们里面存在一个hello方法,
@Service
public class ScheduledService {
//在一个特定的时间执行这个方法~ Timer
//cron表达式 秒 分 时 日 月 星期
// 0 31 18 * * ? 每天的 18:31:00 执行
//0 0/5 10,18 * * ? 每天的10点和18点,每隔5分钟执行一次
//0 15 10,18 ? * 1-6 每个月的周一到周六的 10点15分执行一次
@Scheduled(cron = "0/2 * * * * ?")//spring定时器
public void hello() {
System.out.println("你被执行了~~");
}
}
这里写完定时任务之后,我们需要在主程序上增加@EnableScheduling 开启定时任务功能
@EnableAsync //开启异步注解功能
@EnableScheduling //开启基于注解的定时任务
@SpringBootApplication
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class, args);
}
}
邮件任务
邮件发送,在我们的日常开发中,也非常的多,Springboot也帮我们做了支持
邮件发送需要引入spring-boot-start-mail
SpringBoot 自动配置MailSenderAutoConfiguration
定义MailProperties内容,配置在application.yml中
自动装配JavaMailSender
测试邮件发送
引入pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置文件:
spring.mail.username=1874234461@qq.com
spring.mail.password=vijwgkwchtgjcjig
spring.mail.host=smtp.qq.com
#QQ需要开启加密验证
spring.mail.properties.maile.smtp.ssl.enable=true
获取授权码:在QQ邮箱中的设置->账户->开启pop3和smtp服务
Spring单元测试:
@SpringBootTest
class Springboot09ApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//一个简单的邮件
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("试试邮件发送~~~");//邮件主题
mailMessage.setText("JAVA是世界上最好的编程语言");
mailMessage.setFrom("1874234461@qq.com");
mailMessage.setTo("1874234461@qq.com");
mailSender.send(mailMessage);
}
@Test
void contextLoads2() throws MessagingException {
//一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setSubject("试试邮件发送~~~");//邮件主题
helper.setText("<p style='color:red'>JAVA是世界上最好的编程语言<p>",true); //开启html解析
//附件
helper.addAttachment("1.jpg",new File("C:\\Users\\PCTC\\Desktop\\图片\\DJIOH2299_TX80D0EAN3@2E.png"));
helper.setFrom("1874234461@qq.com");
helper.setTo("1874234461@qq.com");
mailSender.send(mimeMessage);
}
}