准备工具:IntelliJ IDEA
Springboot异步任务:
1.SpringbootApplication 开启异步注解功能 @EnableAsync
@EnableAsync // 开启异步注解功能
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TestApplication.class, args);
}
}
- 编写AsyncService类 添加进 @Service ,方法上添加 @Async 告诉spring这是一个异步方法
注意:在类上添加@Async表示这个类下的所有方法都是异步
方法上添加@Async表示这个方法都异步
@Async所修饰的函数不要定义为static类型,这样异步调用不会生效。
本类的方法添加@Async,在本类中调用并不会异步,因为注解异步@Async是通过代理类来调用的。
若在类中直接调用,相当于this.xxx的方式,即是通过MyJobService对象直接调用的xxx方法
@Service
public class AsyncService {
@Async //告诉spring这是一个异步方法
public void helo(){
//快捷键 ctrl+alt+t
try {
Thread.sleep(3000);//等待3秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在加载......");
}
}
- AsyncController 控制器 ,注入 AsyncService 写方法调用 .helo()
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
// 异步测试
@RequestMapping("/hello")
public String hello(){
asyncService.helo();//停3秒
return "OK";
}
}
4.运行时并没有直接就返回OK,而是在三秒后才返回,期间可以看到刷新按钮一直是加载的状态,由此可以看出该异步是做到了,所有任务执行完成才返回,任务执行时间正常,主线程等待其他线程完成后结束。
异步了解了也可以看看同步的效果
首先引依赖
org.projectlombok
lombok
创建任务服务类(未做异步处理)、加入容器
package com.hh.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class MyJobService {
public void printJob() throws InterruptedException {
log.info("开始任务一。。。。");
long start = System.currentTimeMillis();
Thread.sleep(5000);
long end = System.currentTimeMillis();
log.info("完成任务一,耗时:" + (end - start) + "毫秒");
}
public void printJob2() throws InterruptedException {
log.info("开始任务二。。。。");
long start = System.currentTimeMillis();
Thread.sleep(4000);
long end = System.currentTimeMillis();
log.info("完成任务二,耗时:" + (end - start) + "毫秒");
}
public void printJob3() throws InterruptedException {
log.info("开始任务三。。。。");
long start = System.currentTimeMillis();
Thread.sleep(3000);
long end = System.currentTimeMillis();
log.info("完成任务三,耗时:" + (end - start) + "毫秒");
}
}
控制器
package com.hh.controller;
import com.hh.service.MyJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class JobController {
@Autowired
MyJobService myJobService;
@RequestMapping("demo")
public String demo() throws InterruptedException {
Long start =System.currentTimeMillis();
myJobService.printJob();
myJobService.printJob2();
myJobService.printJob3();
Long end = System.currentTimeMillis();
log.info("任务总耗时:{}",end-start);
return "success";
}
}
运行:
浏览器会一直等待。因为默认是同步任务,任务总耗时是所有任务总耗时。
Springboot邮件发送:
1.添加依赖
org.springframework.boot spring-boot-starter-mail
2.application.properties 邮件发送配置
spring.mail.username=邮箱 #配置邮件客户端 授权:mdhcdnasrvuvejad spring.mail.password=mdhcdnasrvuvejad # 发送的服务器 spring.mail.host=smtp.qq.com # 开启加密验证 qq需要配置ssl spring.mail.properties.mail.smtp.ssl.enable=true
3.编写测试方法( 真实的项目,测试的内容是在controller写的 )
@SpringBootTest
class Springboot09TestApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
//简单的邮件发送 3步
@Test
void contextLoads() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("这是发送标题吖");
mailMessage.setText("这是发送的内容,……");
mailMessage.setTo("邮箱");//接收
mailMessage.setFrom("邮箱");//发送
mailSender.send(mailMessage);
}
//复杂的邮件
@Test
void contextLoads2() throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装 true:开启支持多文件
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//正文
helper.setSubject("哈哈");
helper.setText("哈哈text
", true);//true开启HTML文本识别
//附件
helper.addAttachment("1.jpg", new File("C:\图片路径\1.jpg"));
helper.addAttachment("2.jpg", new File("C:\图片路径\2.jpg"));
helper.setTo("邮箱");
helper.setFrom("邮箱");
mailSender.send(mimeMessage);
}
//封装一个方法
public void sendMail(Boolean html, String subject, String text) throws MessagingException {
// 一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, html);
//正文
helper.setSubject(subject);
helper.setText(text, true);
//附件
helper.addAttachment("1.jpg", new File("C:\图片路径\1.jpg"));
helper.addAttachment("2.jpg", new File("C:\图片路径\2.jpg"));
helper.setTo("邮箱");
helper.setFrom("邮箱");
mailSender.send(mimeMessage);
}
}
运行:
Springboot定时任务:
定时任务的几种方式
Timer
这是 Java 自带的 java.util.Timer 类,这个类允许调度一个名为 java.util.TimerTask 任务。使用这种方
式可以让你
的程序按照某一个 频度 执行,但不能在 指定时间 运行。现在一般用的较少。
ScheduledExecutorService
JDK 自带的一个类,是基于 线程池 设计的定时任务类,每个 调度任务 都会分配到 线程池 中的一个 线
程 去执行。
也就是说,任务是 并发执行,互不影响的。
Spring Task
Spring 3.0 以后自带的 Task,支持 多线程 调度,可以将它看成一个 轻量级 的 Quartz,而且使用起来
比 Quartz
简单许多,但是适用于 单节点 的 定时任务调度。
Quartz
这是一个 功能比较强大 的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配
置起来 稍
显复杂。Quartz 功能强大,可以结合 数据库 做 持久化,进行 分布式 的 任务延时调度。
定时任务也叫计划任务,是任务在约定的时间执行已经计划好的工作,这是表面的意思。在Linux中,我们经常用到 cron 服务器来完成这项工作。cron服务器可以根据 配置文件约定的时间来执行特定的任务。
1.开启异步定时器功能的注解 @EnableScheduling
@EnableScheduling //开启异步定时器功能的注解
@SpringBootApplication
public class Springboot09TestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TestApplication.class, args);
}
}
2.编写service类 :ScheduledService ,@service注入service
写方法标注cron表达式(设置定时任务)
package com.hh.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service // 添加进 service
public class ScheduledService {
// 在一个特定的时间执行 Timer 方法 (秒 分 时 日 月 周几)
@Scheduled(cron = "0/5 * * * * ?")
public void hello(){
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dateFormat.format(new Date()) +"--------hello,ScheduledService.......被执行了~");
}
}
执行(定时设置每5秒执行一次)
用Spring Boot 内置的定时任务,可以满足大部分的定时任务场景需求
@Scheduled 注解来实现 定时任务,@Scheduled 参数可以接受两种定时的设置。
一种是我们常用的 格林时间,表达式 cron = “*/5 * * * * *”,
另一种是 fixedRate = 5 * 1000L,两种都表示每隔 5 秒执行一次目标任务。
fixedDelay:上一次 ‘执行完毕’ 时间点之后再执行
fixedDelayString:同上
//例子 上一次执行完毕时间点之后5秒再执行 @Scheduled(fixedDelay = 5000) @Scheduled(fixedDelayString = "5000")
fixedRate:上一次 ‘开始执行 ’ 时间点之后5秒再执行
fixedRateString:同上
//例子 上一次开始执行时间点之后5秒再执行 @Scheduled(fixedRate = 5000) @Scheduled(fixedRateString = "5000")
格林时间,表达式 cron 例子
@Scheduled(cron = ("0/1 * * ? * 5 "))//每周五、每秒执行一次,默认是单线程
cron表达式 在线生成表达式:
https://www.bejson.com/othertools/cron/
里面有很多cron表达式可以参考
常用表达式例子 (1)0/2 * * * * ? 表示每2秒 执行任务 (1)0 0/2 * * * ? 表示每2分钟 执行任务 (1)0 0 2 1 * ? 表示在每月的1日的凌晨2点调整任务 (2)0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业 (3)0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作 (4)0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 (5)0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时 (6)0 0 12 ? * WED 表示每个星期三中午12点 (7)0 0 12 * * ? 每天中午12点触发 (8)0 15 10 ? * * 每天上午10:15触发 (9)0 15 10 * * ? 每天上午10:15触发 (10)0 15 10 * * ? 每天上午10:15触发 (11)0 15 10 * * ? 2005 2005年的每天上午10:15触发 (12)0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发 (13)0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发 (14)0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 (15)0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发 (16)0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发 (17)0 15 10 ? * MON-FRI 周一至周五的上午10:15触发 (18)0 15 10 15 * ? 每月15日上午10:15触发 (19)0 15 10 L * ? 每月最后一日的上午10:15触发 (20)0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发 (21)0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发 (22)0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发



