常用任务
-
异步任务
-
邮件发送
-
定时任务
异步任务
-
Service层添加注解
package com.xz.service; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; /** * @author 许正 * @version 1.0 */ @Service public class AsyncService { @Async //该注解表示这是一个异步的方法 public void hello() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("数据正在处理..."); } }
-
编写Controller层
package com.xz.controller; import com.xz.service.AsyncService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author 许正 * @version 1.0 */ @RestController public class AsyncController { @Autowired private AsyncService asyncService; @RequestMapping("/hello") public String hello() { asyncService.hello(); return "OK!"; } }
-
执行方法上开启异步注解功能
package com.xz; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; @EnableAsync //开启异步注解功能 @SpringBootApplication public class Springboot09TaskApplication { public static void main(String[] args) { SpringApplication.run(Springboot09TaskApplication.class, args); } }
邮件发送
package com.xz;
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 Springboot09TaskApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//一个简单的邮件
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("程序员正正你好!");
mailMessage.setText("你的博客很不错!一起加油呀~~~");
mailMessage.setTo("550628942@qq.com");
mailMessage.setFrom("550628942@qq.com");
mailSender.send(mailMessage);
}
@Test
void contextLoads2() throws MessagingException {
//一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//正文
helper.setSubject("程序员正正你好plus!");
helper.setText("<p style='color=red'>你的博客很不错!一起加油呀!</p>", true);
//附件
helper.addAttachment("1.jpg", new File("E:\\1.jpg"));
helper.addAttachment("2.jpg", new File("E:\\1.jpg"));
helper.setTo("550628942@qq.com");
helper.setFrom("550628942@qq.com");
mailSender.send(mimeMessage);
}
}
定时任务
简单实现
核心接口
Taskscheduler //任务调度者
TaskExecutor //任务执行者
@EnableScheduling //开启定时功能的注解
@Scheduled //什么时候执行~
//Cron表达式
示例
package com.xz.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* @author 许正
* @version 1.0
*/
@Service
public class ScheduledService {
//在一个特定的时间执行这个方法
//秒 分 时 日 月 周几
//cron = "30 10 19 * * ?" 表示: 每天的 19:10:30 执行一次
@Scheduled(cron = "30 10 19 * * ?")
public void hello() {
System.out.println("ScheduledService 的 hello() 被执行了!");
}
}
Cron表达式
Cron表达式介绍
Cron表达式是一个具有时间含义的字符串,字符串以5个空格隔开,分为6个域,格式为X X X X X X
。其中X
是一个域的占位符。单个域有多个取值时,使用半角逗号,
隔开取值。每个域可以是确定的取值,也可以是具有逻辑意义的特殊字符。
域取值
下表为Cron表达式中六个域能够取的值以及支持的特殊字符。
特殊字符
Cron表达式中的每个域都支持一定数量的特殊字符,每个特殊字符有其特殊含义。