先把前端页面显示出来,再去处理数据
1.service【@Async//告诉spring它是一个异步方法】
@Service
public class AsyncService {
@Async//告诉spring它是一个异步方法
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在处理中。。。。。。");
}
}
2.main【@EnableAsync//开启异步注解功能】
@EnableAsync//开启异步注解功能
@SpringBootApplication
public class Springboot09TestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TestApplication.class, args);
}
}
邮件任务
1.放入依赖
org.springframework.boot spring-boot-starter-mail
2.写配置文件
spring.mail.username=@qq.com spring.mail.password= spring.mail.host=smtp.qq.com #开启加密验证 spring.mail.properties.mail.smtp.ssl.enable=true
在此处得到密码加密字符串
3.建一个简单的邮件
@SpringBootTest
class Springboot09TestApplicationTests {
//先注入接口
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("欢迎来到cc邮件世界");
mailMessage.setText("来看看有什么内容");
mailMessage.setTo("@qq.com");
mailMessage.setFrom("@qq.com");
//一个简单的邮件
mailSender.send(mailMessage);
}
}
4.建一个复杂文件
@Test
void contextLoads2() throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//正文
helper.setSubject("欢迎来到这");
helper.setText("哈哈哈哈哈
",true);
//附件
helper.addAttachment("1.png",new File("C:\Users\79362\Desktop\1.png"));
helper.setTo("7936@qq.com");
helper.setFrom("7936@qq.com");
//一个复杂的文件
mailSender.send(mimeMessage);
}
}
封装为一个方法
public void sendMail(String subject, String text,Boolean html) throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,html);
//正文
helper.setSubject(subject);
helper.setText(text,true);
//附件
helper.addAttachment("1.png",new File("C:\Users\79362\Desktop\1.png"));
helper.setTo("793628929@qq.com");
helper.setFrom("793628929@qq.com");
//一个复杂的文件
mailSender.send(mimeMessage);
}
定时执行任务
TaskScheduler 任务调度者
TaskExecutor 任务执行者
@EnableScheduling //开启定时功能的注解
@Scheduled(cron表达式) //什么时候执行
main
@EnableAsync//开启异步注解功能
@EnableScheduling//开启定时功能的注解
@SpringBootApplication
public class Springboot09TestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TestApplication.class, args);
}
}
service
@Service
public class ScheduledService {
@Scheduled(cron = "0/2 * * * * ?")
public void hello(){
System.out.println("hello0000000");
}
}
cron表达式
秒,分,时,日,月,星期1~7 (SUN-SAT),年
? : 表示不指定值。简单理解就是忽略该字段的值,直接根据另一个字段的值触发执行。
*: 表示匹配该域的任意值。比如Minutes域使用*,就表示每分钟都会触发。
- : 表示范围
/ : 表示间隔时间触发(开始时间/时间间隔)
–部分内容来自狂神说JAVA



