1.编写业务模拟延时进程
package com.cjp.service;
import org.springframework.stereotype.Service;
@Service
public class SynchService {
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据处理完毕!");
}
}
2.controller层调用业务层
package com.cjp.controller;
import com.cjp.service.SynchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SynchController {
@Autowired
SynchService synchService;
@RequestMapping("/hello")
public String hello(){
synchService.hello();
return "ok";
}
}
3.在异步任务方法上加上注解@Async
@Service
@Async
public class SynchService {
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据处理完毕!");
}
}
4.在启动类中使用注解@EnableAsync开启异步功能
package com.cjp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync
@SpringBootApplication
public class Springboot05MailApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot05MailApplication.class, args);
}
}
1、导入依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
dependency>
2.配置信息
spring.mail.username=1770364681@qq.com
spring.mail.password=
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true
3.编写业务代码
package com.cjp;
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;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@SpringBootTest
class Springboot05MailApplicationTests {
@Autowired
JavaMailSenderImpl javaMailSender;
@Test
//简单邮件
void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("你的会员已过期!");
message.setText("您的qq会员已于2022年10月3日过期!");
message.setTo("1770364681@qq.com");
message.setFrom("1770364681@qq.com");
javaMailSender.send(message);
}
@Test
//复杂邮件
void contextLoads1() throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setSubject("你好呀!你的QQ会员过期了哦");
helper.setText("您的qq会员已于2022年10月3日过期!
",true);
helper.addAttachment("简历",new File("D:\\Desktop\\简历-cjp.docx"));
helper.setTo("1770364681@qq.com");
helper.setFrom("1770364681@qq.com");
javaMailSender.send(message);
}
}
1.定时任务相关的两个接口、两个注解
TaskExecutor :任务执行者
TaskScheduler:任务调度程序
@EnableScheduling //开启定时任务支持
@Scheduled //什么时候执行
cro表达式
操作:
首先在启动类上使用注解开启定时任务
编写定时任务的业务代码:
package com.cjp.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduledService {
//秒 分 时 日 月 星期几
/*
* 30 0/5 10,8 * * ? 每天十点和八点 每隔五分钟执行一次
*
* */
@Scheduled(cron = "0 55 15 * * ?")
public void hello(){
System.out.println("程序执行了!!!");
}
}
使用cron表达式控制程序执行的时间
完成测试