Spring Boot与任务
- 异步任务
- 定时任务
- 邮件任务
在IDEA创建的WEB项目下实现。
异步任务
在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务。在Spring 3.x之后,已经内置了@Async来解决这个问题。
用到的两个注解:@EnableAysnc、@Aysnc
代码案例演示:
1.在启动函数添加注解@EnableAysnc:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @EnableAsync //开启异步注解功能 异步任务 @SpringBootApplication public class Springboot04TaskApplication { public static void main(String[] args) { SpringApplication.run(Springboot04TaskApplication.class, args); } }
2.编写测试用例
Service层-AsyncService
import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class AsyncService { @Async//告诉Spring这是一个异步任务 public void hello(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("处理数据中..."); } }
Controller层-AsyncController
import com.atguigu.task.service.AsyncService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class AsyncController { @Autowired AsyncService asyncService; @GetMapping("/hello") public String hello(){ asyncService.hello(); return "success"; } }
定时任务
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor 、TaskScheduler 接口。
两个注解:@EnableScheduling、@Scheduled
代码案例演示:
1.在启动函数添加注解@EnableScheduling:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling //开启基于注解的定时任务 @SpringBootApplication public class Springboot04TaskApplication { public static void main(String[] args) { SpringApplication.run(Springboot04TaskApplication.class, args); } }
2.编写测试用例
Service层-ScheduledService
```java package com.atguigu.task.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class ScheduledService { // @Scheduled告诉Spring这是一个定时任务 还要main上添加注解开启 /** * second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几). * 六位空格分隔,与上面一一对应。 * 0 * * * * MON-FRI * 【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点期间,每个整点都执行一次; */ // @Scheduled(cron = "0 * * * * MON-SAT") //@Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT") // @Scheduled(cron = "0-4 * * * * MON-SAT") @Scheduled(cron = "0/4 * * * * MON-SAT") //每4秒执行一次 public void hello(){ System.out.println("hello ... "); } } ```
邮件任务
1.添加pom
```properties <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ```
2.配置application.properties
#邮件任务配置 #发送邮件的邮箱 spring.mail.username=1824461232@qq.com #QQ邮箱的授权码 spring.mail.password=xxxxxxx #smtp服务器的地址 spring.mail.host=smtp.qq.com #开启安全发送 spring.mail.properties.mail.smtp.ssl.enable=true
3.编写测试用例
import org.junit.Test; import org.junit.runner.RunWith; 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.MimeMessageHelper; import org.springframework.test.context.junit4.SpringRunner; import javax.mail.internet.MimeMessage; import java.io.File; @RunWith(SpringRunner.class) @SpringBootTest public class Springboot04TaskApplicationTests { //邮件任务 @Autowired JavaMailSenderImpl mailSender; //简单文字邮件 @Test public void contextLoads() { SimpleMailMessage message = new SimpleMailMessage(); //邮件设置 message.setSubject("通知-今晚开会"); message.setText("今晚7:30开会"); message.setTo("sdliuzhi0313@163.com"); message.setFrom("1824461232@qq.com"); mailSender.send(message); } //创建一个复杂的消息邮件 @Test public void test02() throws Exception{ MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); //邮件设置 helper.setSubject("通知-今晚开会"); helper.setText("今天 7:30 开会",true); helper.setTo("sdliuzhi0313@163.com"); helper.setFrom("1824461232@qq.com"); //上传文件 helper.addAttachment("1.jpg",new File("文件本地地址")); mailSender.send(mimeMessage); } }