异步任务
异步任务主要需要两个注解@EnableAsync
这个是放在springboot主入口的,用于开启异步任务,@Async
这个是放在业务层代码上,用于标注这个是一个异步任务。
业务层代码
package com.zkw.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
@Async
public void ThreadService(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在加载.......");
}
}
来一个controller
package com.zkw.controller;
import com.zkw.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
@Autowired
private AsyncService asyncService;
@GetMapping("/hello")
public String Hello(){
asyncService.ThreadService();
//这是使用传统的方式开启一条线程,来进行异步任务的
/*new Thread(()->{
asyncService.ThreadService();
}).start();*/
return "ok";
}
}
邮件任务
要先开启qq邮箱的smtp和pop3协议:传送门
首先导入一个依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
然后配置一下yaml文件
spring.mail.username=1040419584@qq.com
spring.mail.password=123456
spring.mail.host=smtp.qq.com
# qq邮件需要特别开启的
spring.mail.properties.mail.smtp.ssl.enable=true
测试如下
@Autowired
JavaMailSenderImpl mailSender;
//简单邮件发送
@Test
void contextLoads() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setSubject("学习是springboot发送邮件---->这是邮件的标题");
simpleMailMessage.setText("设置邮件发送的内容");
//邮件接收者
simpleMailMessage.setTo("xxiaowu99@163.com");
//邮件发送者
simpleMailMessage.setFrom("1040419584@qq.com");
//发送邮件
mailSender.send(simpleMailMessage);
}
//复杂邮件发送
@Test
void Test1() throws MessagingException {
//创建一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
messageHelper.setSubject("邮件标题");
// true 标志着开启识别发送文本中的html语言
messageHelper.setText("<p style='color=red'>发送的内容</p>",true);
//添加附件
messageHelper.addAttachment("1.jpg",new File("C:\\Users\\10404\\Desktop\\1.png"));
messageHelper.setTo("xxiaowu99@163.com");
messageHelper.setFrom("1040419584@qq.com");
mailSender.send(mimeMessage);
}
定时任务
cron表达式:传送门
需要两个注解,分别是@EnableScheduling
用于开启定时任务,Scheduled
用于标注这是一个定时任务
业务层代码
package com.zkw.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class SchedulingService {
@Scheduled(cron = "30 34 10 * * ?")
public void Scheduling(){
System.out.println("被执行了.......");
}
}
主入口
package com.zkw;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableAsync // 开启异步任务
@EnableScheduling //开启时任务
public class SpringbootTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTestApplication.class, args);
}
}