异步任务
1.异步任务
1.创建一个service包
2.创建一个类AsyncService
异步处理还是非常常用的,比如我们在网站上发送邮件,后台回去发送邮件,此时前台会造成响应不动,知道邮件发送完毕,响应才会成功,所以我们一般采用多线程的方式去处理这些任务。
编写方法,假装正在处理数据,使用线程设置一些延时,模拟同步等待的情况
1 @Service 2 public class AsyncService { 3 public void hello(){ 4 try { 5 Thread.sleep(3000); 6 }catch (InterruptedException e){ 7 e.printStackTrace(); 8 } 9 System.out.println("数据处理中..."); 10 } 11 }
3.编写controller
4.编写AsyncController类
编写一个Controller测试一下
1 @RestController 2 public class AsyncController { 3 @Autowired 4 private AsyncService asyncService; 5 @GetMapping("/hello") 6 public String hello(){ 7 asyncService.hello(); 8 return "success"; 9 } 10 }
5.访问http://localhost:8080/hello进行测试,3秒后出现success,这是同步等待的情况
问题:我们如果想让用户直接得到消息,就在后台使用多线程的方式进行处理即可,但是每次都需要自己手动去编写多线程的实现的话,太麻烦了,我们只需要用一个简单的办法,在我们的方法上添加一个简单的注解即可,如下:
6.给hello方法添加@Async注解
1 @Service 2 @Async 3 public class AsyncService { 4 public void hello(){ 5 try { 6 Thread.sleep(3000); 7 }catch (InterruptedException e){ 8 e.printStackTrace(); 9 } 10 System.out.println("数据处理中..."); 11 } 12 }
SpringBoot就会自己开一个线程池,进行调用,但是要让这个注解生效,我们还需要在主程序上添加一个注解@EnableAsync,开启异步注解功能
1 @EnableAsync 2 @SpringBootApplication 3 public class SwaggerstudyApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(SwaggerstudyApplication.class, args); 7 } 8 9 }
7.重启测试,网页瞬间响应,后台代码依旧执行
2.定时任务
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨的时候,分析一次前一天的日志信息,Spring为我们提供了异步执行任务调度的方式,提供了两个接口
- TaskExecutor接口
- TaskScheduler接口
两个注解:
- @EnableScheduling
- @Scheduled
cron表达式:http://www.bejson.com/othertools/cron/ 通过此网站了解cron表达式
1.创建一个ScheduledService
我们里面存在一个hello方法,他需要定时执行,怎么处理?
1 @Service 2 public class ScheduledService { 3 //秒 分 时 日 月 周几 4 //0 * * * * MON-FRI 5 //注意cron表达式的用法 6 @Scheduled(cron = "0 0/1 * * * ?") 7 public void hello(){ 8 System.out.println("hello....."); 9 } 10 }
2.这里写完定时任务之后,我们需要在主程序上增加@EnableScheduling开启定时任务功能
1 @EnableAsync //开启异步注解功能 2 @EnableScheduling //开启基于注解的定时任务 3 @SpringBootApplication 4 public class SwaggerstudyApplication { 5 6 public static void main(String[] args) { 7 SpringApplication.run(SwaggerstudyApplication.class, args); 8 } 9 10 }
corn练习
秒 分 时 日 月 周几
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点期间,每个整点都执行一次
3.邮件任务
邮件发送,在我们的日常开发中,也非常多,springboot也帮我们做了支持
- 邮件发送需要引入spring-boot-start-mail
- SpringBoot自动配置MailSenderAutoConfiguration
- 定义MailProperties内容,配置在application.yml中
- 自动装配JavaMailSender
- 测试邮件发送
1.引入pom依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-mail</artifactId> 4 </dependency>
看它引入的依赖,可以看到jakarta.mail
1 <dependency> 2 <groupId>com.sun.mail</groupId> 3 <artifactId>jakarta.mail</artifactId> 4 <version>1.6.5</version> 5 <scope>compile</scope> 6 </dependency>
2.查看自动配置类:
MailSenderAutoConfiguration
这个类中没有注册bean,看一下它导入的其他类:MailSenderJndiConfiguration.class
1 @Import({ MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class }) 2 public class MailSenderAutoConfiguration { 3 ...... 4 }
这个类中存在bean,JavaMailSenderImpl
然后看一下配置文件
配置文件:
待补充。。。
2121