本文共 4166 字,大约阅读时间需要 13 分钟。
@EnableAsync + @Async // 开启异步任务注解功能 @EnableAsync 标识在启动类上面@Async 标识在方法上面,标识异步方法
定时任务:
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。
Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor、TaskScheduler接口。
@EnableScheduling 标识在启动类上面
@Scheduled 标识在方法上面
package cn.mesmile.task.service;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;import java.text.SimpleDateFormat;import java.util.Date;/** * @author zb * @date 2019/12/3 21:05 * @Description: */@Servicepublic class ScheduledService { /** * cron: * second(秒), minute(分), hour(时), day of month (日), month(月), and day of week(周几) * * 0 * * * * MON-FRI * 周一到周五 每分钟整数秒 * hello world......2019-12-03 09:41:00 * hello world......2019-12-03 09:42:00 * hello world......2019-12-03 09:43:00 * * 1,2,3 * * * * MON-FRI * 1-3 * * * * MON-FRI * hello world......2019-12-03 09:52:01 * hello world......2019-12-03 09:52:02 * hello world......2019-12-03 09:52:03 * * 0/3 * * * * MON-FRI * hello world......2019-12-03 09:54:33 * hello world......2019-12-03 09:54:36 * hello world......2019-12-03 09:54:39 */ @Scheduled(cron = "0/3 * * * * MON-FRI") public void hello() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println("hello world......"+simpleDateFormat.format(new Date())); } /** * 特殊字符 代表含义 * , 枚举 * - 区间 * * 任意 * / 步长 * ? 日/星期冲突匹配L最后 * W 工作日 * C 和calendar联系后计算过的值 * # 星期,4#2,第2个星期三 * * 字段 允许值 允许的特殊字符 * 秒 0-59 ,* / * 分 0-59 ,* / * 小时 0-23 ,* / * 日期 1-31 ,*?/LWC * 月份 1-12 ,* / * 星期 0-7或SUN-SAT0,7是SUN ,-*?/LC# */}
导入spring支持的邮件jar包:
org.springframework.boot spring-boot-starter-mail
package cn.mesmile.task;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.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import java.io.File;import java.nio.charset.MalformedInputException;@SpringBootTestclass TaskApplicationTests { @Autowired JavaMailSender javaMailSender; @Test void contextLoads() { System.out.println("hello world"); } @Test void testSendMail(){ // 发送简单消息 SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); // 接收方邮箱 simpleMailMessage.setTo("s@aliyun.com"); simpleMailMessage.setSubject("这是文章的标题。。。。"); simpleMailMessage.setText("这是文本内容哦.........."); // 发送方邮箱 simpleMailMessage.setFrom("10@qq.com"); javaMailSender.send(simpleMailMessage); } @Test void testSendMails() throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); mimeMessageHelper.setSubject("通知--今晚开会"); mimeMessageHelper.setText("今天 7:30 开会",true); // 接收方邮箱 mimeMessageHelper.setTo("s@aliyun.com"); // 发送方邮箱 mimeMessageHelper.setFrom("1047@qq.com"); // 上传文件 mimeMessageHelper.addAttachment("docker容器.txt",new File("C:\\Users\\SuperZheng\\Desktop\\demo\\docker容器.txt")); javaMailSender.send(mimeMessage); }}
# password 为 授权码# ssl:enable 允许 sslspring: mail: password: lntcjmgj username: 10@qq.com host: smtp.qq.com properties: mail: smtp: ssl: enable: true
转载地址:http://hqxxi.baihongyu.com/