1. 异步任务
1. 概念:异步处理还是非常常用的,比如我们在网站上发送邮件,后台会去发送邮件,此时前台会造成响应不动,直到邮件发送完毕,响应才会成功,所以我们一般会采用多线程的方式去处理这些任务
2. 使用
(1)创建一个service包,编写一个AsyncService,编写方法,假装正在处理数据,使用线程设置一些延时,模拟同步等待的情况
@Service
public class AsyncService {
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("业务进行中");
}
}
(2)创建一个controller包,编写一个AsyncController类测试
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello(){
asyncService.hello();
return "success";
}
}
(3)进行测试,3秒后出现success,这是同步等待的情况
问题:我们想让用户直接得到消息,在后台使用多线程处理即可,需要一个简单的方法
(4)给hello()方法添加@Async注解
@Service
public class AsyncService {
@Async //告诉spring这是一个异步方法
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("业务进行中");
}
}
(5)在主程序添加一个注解@EnableAsync
@EnableAsync //开启异步注解
@SpringBootApplication
public class Springboot11AddApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot11AddApplication.class, args);
}
}
(6)测试,网页瞬间响应,后台代码依旧执行
2. 邮件任务
1. 使用
(1)导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
(2)在application.properties中配置
spring.mail.username=wangzr0730@163.com
spring.mail.password=KVKTOUWHFXCSWRLL //网易给的密码
spring.mail.host=smtp.163.com
(3)Spring单元测试
@SpringBootTest
class Springboot11AddApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//测试1:一个简单的邮件
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("测试");
message.setText("我在学习邮件发送任务");
message.setTo("wangzr0730@163.com");
message.setFrom("wangzr0730@163.com");
mailSender.send(message);
}
@Test
void contextLoads2() throws MessagingException {
//测试2:一个复杂的邮件
MimeMessage message = mailSender.createMimeMessage();
//第二个参数:是否开启复杂邮件类型
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setSubject("测试2");
//第二个参数:是否识别html
helper.setText("<p style='color:red'>我在学习邮件发送任务2</p>",true);
//发送附件
helper.addAttachment("1.png",new File("C:\\Users\\wzr\\Desktop\\1.png"));
helper.setTo("wangzr0730@163.com");
helper.setFrom("wangzr0730@163.com");
mailSender.send(message);
}
}
3. 定时任务
1. cron表达式
(1)语法
(2)常用的cron表达式
(1)0/2 * * * * ? 表示每2秒 执行任务
(1)0 0/2 * * * ? 表示每2分钟 执行任务
(1)0 0 2 1 * ? 表示在每月的1日的凌晨2点调整任务
(2)0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
(3)0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作
(4)0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
(5)0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
(6)0 0 12 ? * WED 表示每个星期三中午12点
(7)0 0 12 * * ? 每天中午12点触发
(8)0 15 10 ? * * 每天上午10:15触发
(9)0 15 10 * * ? 每天上午10:15触发
(10)0 15 10 * * ? 每天上午10:15触发
(11)0 15 10 * * ? 2005 2005年的每天上午10:15触发
(12)0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发
(13)0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
(14)0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
(15)0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发
(16)0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发
(17)0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
(18)0 15 10 15 * ? 每月15日上午10:15触发
(19)0 15 10 L * ? 每月最后一日的上午10:15触发
(20)0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发
(21)0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发
(22)0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发
2. 使用
(1)创建ScheduledService,里面存放hello()方法,让其定时执行
@Service
public class ScheduledService {
//秒 分 时 日 月 周几
//0 * * * * MON-FRI
//注意cron表达式的用法;
@Scheduled(cron = "0 * * * * 0-7")
public void hello(){
System.out.println("hello.....");
}
}
(2)在主程序加上@EnableScheduling
@EnableScheduling //开启定时任务的注解
@EnableAsync //开启异步注解
@SpringBootApplication
public class Springboot11AddApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot11AddApplication.class, args);
}
}
(3)测试