springboot常见的任务
异步任务,定时任务,发送邮件任务
1.异步任务 @Async
service类
package com.lh.springboot.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @program: springbootweb04
* @description:
* @author: li hui
* @create: 2020-12-22 18:08
*/
@Service
public class AsyncService {
//告诉spring这是一个异步方法
@Async
public void hello(){
try {
Thread.sleep(6000);
System.out.println("数据处理中!!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
controller 类
package com.lh.springboot.controller; import com.lh.springboot.service.AsyncService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @program: springbootweb04 * @description: * @author: li hui * @create: 2020-12-22 18:10 */ @RestController public class AsyncController { private final AsyncService asyncService; @Autowired public AsyncController(AsyncService asyncService) { this.asyncService = asyncService; } @RequestMapping("/hello") public String hello(){ asyncService.hello(); return "success"; } }
启动类
package com.lh.springboot;
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 Springbootweb04Application {
public static void main(String[] args) {
SpringApplication.run(Springbootweb04Application.class, args);
}
}
没有加 @Async 的注解 和 加了注解有很大的区别,可以感受一下 如果感觉不明显可以在service的 @Async 下的注解 的执行方法内 让程序多睡几秒
2.定时任务 @Scheduled
首先开启定时任务
service类
package com.lh.springboot.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.Date; /** * @program: springbootweb04 * @description: * @author: li hui * @create: 2020-12-22 18:17 */ @Service public class ScheduledService { /* Cron表达式参数分别表示: 秒(0~59) 例如0/5表示每5秒 分(0~59) 时(0~23) 日(0~31)的某天,需计算 月(0~11) 周几( 可填1-7 或 SUN/MON/TUE/WED/THU/FRI/SAT) */ @Scheduled(cron = "0/5 * * * * ? ") //每5秒执行一次 public void hello(){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置日期格式 System.out.println("hello: " + df.format(new Date())); } }
这个定时任务是每隔5秒执行一次
3.发送邮件任务
1)引入maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2)配置application.properties
3)测试类
package com.lh.springboot; 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.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootTest class Springbootweb04ApplicationTests { @Autowired JavaMailSenderImpl javaMailSender; @Test void contextLoads() { //简单的发送邮箱 SimpleMailMessage mailMessage = new SimpleMailMessage(); //邮件设置 mailMessage.setSubject("通知-面试"); //邮件的标题 mailMessage.setText("面试时间早9点到晚5点"); //邮箱的内容 mailMessage.setTo(""); // 发送到那个邮箱 mailMessage.setFrom(""); // 谁发的 javaMailSender.send(mailMessage); } @Test void test02() throws MessagingException { /* 创建一个复杂消息的邮件 */ MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); //邮件设置 messageHelper.setSubject("通知-面试"); //邮件的标题 messageHelper.setText("<h1 style='color:red'>面试时间早9点到晚5点</h1>",true); //邮箱的内容 messageHelper.setTo(""); // 发送到那个邮箱 messageHelper.setFrom(""); // 谁发的 //上传文件 messageHelper.addAttachment("1.jpg", new File("D:\\pic\\demo1.png")); // 参数1 文件吗 参数2 文件地址 messageHelper.addAttachment("2.jpg", new File("D:\\pic\\2.jpg")); javaMailSender.send(mimeMessage); } }