SpringBoot任务
一、异步任务
创建Service
@Service public class AsyncService { public void hello() { try { Thread.sleep(3000); System.out.println("hello world!"); } catch (InterruptedException e) { e.printStackTrace(); } } }
创建Controller
@RestController public class AsyncController { @Autowired private AsyncService asyncService; @GetMapping("/hello") public String hello() { asyncService.hello(); return "success"; } }
当访问http://localhost:8080/hello时会执行AsyncService中的hello方法,等到3秒之后AsyncService中的hello方法方法执行完之后,AsyncController中的hello方法返回”success”,这个过程中AsyncController需要等到AsyncService中的方法执行完之后才能返回
使用异步,不需要AsyncService中的方法执行完成,AsyncController就可以返回数据
在启动类上加上@EnableAsync注解,开启异步任务功能
@SpringBootApplication @EnableAsync // 开启异步任务功能 public class SpringBootTaskApplication { public static void main(String[] args) { SpringApplication.run(SpringBootTaskApplication.class, args); } }
在syncService中的hello方法上加上@@Async,告诉SpringBoot这个方法要执行异步
@Service public class AsyncService { // 异步任务,在该方法还没执行完之前先去执行其他的操作 @Async public void hello() { try { // 不加异步之前,controller需要等待3秒,等待hello方法执行完之后才返回;加了异步之后,controller先执行其他操作,不再等待hello方法执行完 Thread.sleep(3000); System.out.println("hello world!"); } catch (InterruptedException e) { e.printStackTrace(); } } }
二、定时任务
定时任务,即在指定的时间自动执行指定的方法。
在启动类上加上@EnableScheduling注解,开启定时任务功能
@SpringBootApplication @EnableScheduling // 开启定时任务功能 public class SpringBootTaskApplication { public static void main(String[] args) { SpringApplication.run(SpringBootTaskApplication.class, args); } }
在需要定时执行的方法上面加上@Scheduled注解,并指定执行的时机,这样,没到指定时间时方法就会被自动的执行
@Service public class SchedulingService { private static Integer count = 1; /** * cron用于指定执行的时机,规则: * 六个参数分别对应:second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几). * 【0 * * * * *】每分钟的整点(0秒)执行一次 * 【0 * * * * MON-FRI】星期一到星期五,每分钟的整点(0秒)执行一次 * 【0 0/5 14,18 * * ?】 每天14点整,和18点整,每隔5分钟执行一次 * 【0 15 10 ? * 1-6】 每个月的周一至周六10:15分执行一次 * 【0 0 2 ? * 6L】每个月的最后一个周六凌晨2点执行一次 * 【0 0 2 LW * ?】每个月的最后一个工作日凌晨2点执行一次 * 【0 0 2-4 ? * 1#1】每个月的第一个周一凌晨2点到4点期间,每个整点都执行一次; */ @Scheduled(cron = "0/5 * * * * *") // 从每分钟的整点(0秒)开始每隔5秒执行一次 public void autoExecute() { System.out.println("第 "+ (count++) + " 次执行 autoExecute 方法!"); } }
cron表达式:
字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 , - * / 星期 0-7或SUN-SAT 0,7是SUN , - * ? / L C #
特殊字符 代表含义 , 枚举 - 区间 * 任意 / 步长 ? 日/星期冲突匹配 L 最后 W 工作日 C 和calendar联系后计算过的值 # 星期,4#2,第2个星期四
三、邮件任务
SpringBoot也支持发送邮件
在pom.xml中引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
在application.properties中配置
# 邮箱地址,这里使用qq邮箱 spring.mail.username=12345678@qq.com # 注意:spring.mail.password不是邮箱登陆的密码,而是开启邮箱服务的POP3/SMTP服务时生成的授权码 spring.mail.password=wufgpyohmawcdefa # qq邮箱的host spring.mail.host=smtp.qq.com # 开启ssl,不然发邮件的时候会报530异常 spring.mail.properties.ssl.enable=true
在qq邮箱中开启POP3/SMTP服务
进入邮箱主页,在设置->账户中开启POP3/SMTP服务服务,开启服务时需要发送短信,按照提示操作即可,将生成的授权码放入application.properties配置文件中的spring.mail.password中就可以了
测试
package com.example.task; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBootTaskApplicationTests { @Autowired private JavaMailSenderImpl javaMailSender; /** * 发送简单的邮件 */ @Test public void sendMailTest01() { // 简单的邮件 SimpleMailMessage message = new SimpleMailMessage(); // 邮件主题 message.setSubject("通知"); // 邮件内容 message.setText("今晚停电!"); // 邮件发送给谁,可以指定多个 message.setTo("[email protected]"); // 邮件由谁发送的 message.setFrom("[email protected]"); // 发送邮件 javaMailSender.send(message); } /** * 发送复杂的邮件 */ @Test public void sendMailTest02() { MimeMessage message = javaMailSender.createMimeMessage(); try { // 第一个参数时要发送的邮件;第二个参数是是否带有附件 MimeMessageHelper helper = new MimeMessageHelper(message, true); // 邮件主题 helper.setSubject("通知"); // 第一个参数是邮件的内容;第二个参数是邮件内容是否是html helper.setText("<p style='color:red;'>今天下班之后大扫除!</p>", true); // 上传附件 helper.addAttachment("1.jpg", new File("C:\\Users\\Administrator\\Pictures\\1.jpg")); helper.addAttachment("2.jpg", new File("C:\\Users\\Administrator\\Pictures\\2.jpg")); // 邮件接收者,可以有多个 helper.setTo("[email protected]"); // 邮件发送者 helper.setFrom("[email protected]"); // 发送邮件 javaMailSender.send(message); } catch (MessagingException e) { e.printStackTrace(); } } }
运行结果: