异步任务
定时任务
邮件任务
一、异步任务
1、新建项目
package com.kuang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync //开启异步注解功能
@SpringBootApplication
public class Springboot09TestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TestApplication.class, args);
}
}
package com.kuang.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
//告诉Spring这是一个异步的方法
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在处理中...");
}
}
package com.kuang.controller;
import com.kuang.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello() {
asyncService.hello();//停止3秒
return "OK";
}
}
二、邮件任务
pom.xml导入Java.email
<!--java.mail:配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置邮件参数
spring.mail.username=565036413@qq.com
spring.mail.password=ihfbrpwgcuarbefh
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true
启动测试
package com.kuang;
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;
@SpringBootTest
class Springboot09TestApplicationTests {
@Autowired //自动注入
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//一个简单的邮件
SimpleMailMessage mailMessage = new SimpleMailMessage();
//邮件主题
mailMessage.setSubject("小狂神你好呀");
//邮件内容
mailMessage.setText("谢谢你的狂神说Java系列课程");
//发送给谁
mailMessage.setTo("565036413@qq.com");
//接收过来
mailMessage.setFrom("565036413@qq.com");
mailSender.send(mailMessage);
}
}
启动测试-发送一个复杂的邮件(添加附件,内容包含Html语言)
@Test
void contextLoads2() throws MessagingException {
//一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//主题
helper.setSubject("这是一个复杂的邮件-plus");
helper.setText("<p style='color:red'>谢谢你的狂神说Java系列课程~</p>",true);//true 开启解析html
//附件
helper.addAttachment("1.jpg",new File("D:\\CIIN-2020\\CIIN_Java\\_0Tools\\学习截图\\1.jpg"));
helper.addAttachment("2.jpg",new File("D:\\CIIN-2020\\CIIN_Java\\_0Tools\\学习截图\\1.jpg"));
helper.setTo("565036413@qq.com");
helper.setFrom("565036413@qq.com");
mailSender.send(mimeMessage);
}
三、定时任务
- TaskScheduler 任务调度者
- TaskExecutor 任务执行者
- @EnableScheduling //开启定时功能的注解
- @Scheduled //什么时候执行~
- Cron表达式
1、开启定时功能的注解
package com.kuang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableAsync //开启异步注解功能
@EnableScheduling //开启定时功能的注解
@SpringBootApplication
public class Springboot09TestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TestApplication.class, args);
}
}
2、增加定时任务的Service
package com.kuang.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service
public class ScheduledService {
//在一个特定的时间执行这个方法~ Timer
//cron 表达式
//秒 分 时 日 月 周几~
//0 * * * * 0-7 //周一到周日 每一天的第0秒执行这个任务
//30 15 10 * * ? //?不确定周几 每天的10:15分30秒执行一次任务
//30 0/5 10,18 * * ? //?不确定周几 每天的10点和18点,每隔五分钟执行一次任务
//0 15 10 ? * 1-6 //每个月的周一到周六 10:15分执行一次任务
//@Scheduled(cron = "0 13 23 * * ?") //?不确定周几 每天的23:13分0秒执行一次任务
@Scheduled(cron = "0/2 * * * * ?") //每2秒执行一次
public void hello(){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("hello,你被执行了~"+sdf.format(date));
}
}
3、了解定时任务的cron表达式
//cron 表达式
//秒 分 时 日 月 周几~
//0 * * * * 0-7 //周一到周日 每一天的第0秒执行这个任务
//30 15 10 * * ? //?不确定周几 每天的10:15分30秒执行一次任务
//30 0/5 10,18 * * ? //?不确定周几 每天的10点和18点,每隔五分钟执行一次任务
//0 15 10 ? * 1-6 //每个月的周一到周六 10:15分执行一次任务
//@Scheduled(cron = "0 13 23 * * ?") //?不确定周几 每天的23:13分0秒执行一次任务
@Scheduled(cron = "0/2 * * * * ?") //每2秒执行一次