异步任务
步骤:
- 在要使用异步任务的方法上开启异步任务的注解,告诉Spring这是一个异步任务
- Service层
@Service
public class AsyncService {
//告诉Spring这是一个异步的方法
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在处理......");
}
}
- Controller层
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello(){
asyncService.hello();
return "OK";
}
}
- 在主方法上开启异步任务
@EnableAsync //开启异步注解的功能
@SpringBootApplication
public class SpringbootTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTestApplication.class, args);
}
}
邮件任务
步骤:
- 首先引入依赖的包
<!--javax.mail:配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 在配置文件中进行配置
spring.mail.username=xxxxxxxxx@qq.com
spring.mail.password=trarlbnxohpoggda
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true
- 自动装配相关的类,进行测试程序的书写
@SpringBootTest
class SpringbootTestApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//一个简单的邮件
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("甜瓜,你好呀!");
mailMessage.setText("我就是一个西瓜");
mailMessage.setTo("11237698@qq.com");
mailMessage.setFrom("1123698@qq.com");
mailSender.send(mailMessage);
}
@Test
void contextLoads2() throws MessagingException {
//一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//正文
helper.setSubject("melon,hello");
helper.setText("<p style='color:red'>thanks</p>",true);
//附件
helper.addAttachment("1.jpg",new File("C:\\Users\\z144\\Desktop\\1.jpg"));
helper.setTo("11237698@qq.com");
helper.setFrom("1123698@qq.com");
mailSender.send(mimeMessage);
}
//也可以对发送邮件的代码进行简单的封装,工具类
public void sendmail(Boolean html,String subject,String text) throws MessagingException {
//一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,html);
//正文
helper.setSubject(subject);
helper.setText(text,true);
//附件
helper.addAttachment("1.jpg",new File("C:\\Users\\zq144\\Desktop\\1.jpg"));
helper.setTo("1123725698@qq.com");
helper.setFrom("1123725698@qq.com");
mailSender.send(mimeMessage);
}
}
定时执行任务
- 用到的关键类和注解
TaskScheduler 任务调度者
TaskExecutor 任务执行者
@EnableScheduling //开启定时功能的注解
@Scheduled(cron="00 38 10 * * ?") //什么时候执行
- 步骤:
- 在主类main方法上开启定时功能的注解
@EnableScheduling //开启定时功能的注解
@SpringBootApplication
public class SpringbootTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTestApplication.class, args);
}
}
- 编写定时功能类,并加上注解,注意cron表达式的理解,如下。
@Service
public class ScheduledService {
//在一个特定的时间执行这个方法 Timer
//cron表达式(可以自行百度)
//秒 分 时 日 月 周几
/*
* 0 5 10,18 * * ? 每天的10点5分和18点5分执行
* 0/2 * * * * ? 每2秒执行一次任务
* */
@Scheduled(cron="00 38 10 * * ?")
public void hello(){
System.out.println("hello,你被执行了!!!");
}
}
- 结果就是在对应的时间执行相关的代码。