Spring Boot从0开始学的个人笔记10 --任务
一、异步处理
@Async
:写在service的方法前,表示这个方法要用到异步处理@EnableAsync
:写在spring boot启动类前,开启异步处理的功能
AsynService:
@Service
public class AsynService {@Asyncpublic void hello(){try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("数据处理中");}
}
AsynController:
@RestController
public class AsynController {@AutowiredAsynService asynService;@GetMapping("/hello")public String hello(){asynService.hello();return "success";}
}
SpringBootTaskApplication:
@EnableAsync
@SpringBootApplication
public class SpringBootTaskApplication {public static void main(String[] args) {SpringApplication.run(SpringBootTaskApplication.class, args);}}
原本,没+异步处理,每次访问http://localhost:8080/hello
,都要等3秒才出结果。加了这个,秒数结果
二、定时任务
@EnableScheduling
:写在spring boot启动类中前,开启定时任务功能@Scheduled
:用在service类中的方法前,用来设置定时任务@Scheduled(cron = "xxx")
这个xxx是有语法规则的- cron的属性值,规定为字符串,这个字符串,规定有6个时间,分别是秒、分、时、day of month、月、day of week,每个时间段之间用空格分开。比如
"0 * * * * 1-7"
,表示周一到周日,任何时间段的时候,秒为0的时候,执行一次函数
2、一些例子
@Scheduled(cron = "0,1,2,3,4 * * * * 1-5")
:周一到周五,秒钟为0,1,2,3,4的时候,执行一次代码@Scheduled(cron = "0-5 * * * * 1,2,3,4,5")
:同上@Scheduled(cron = "0/4 * * * * 1,2,3,4,5")
:从周一到周五,秒钟为0的时候开始,每隔4秒执行一次0 0/5 14,18 ? * ?
:每天14点,18点,每隔5分钟执行一次0 0 2 ? * 6L
:每个月的最后一个星期六凌晨2点执行一次
三、发邮箱
spring boot自带发邮箱功能,没想到吧
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId>
</dependency>
1、简单的邮件
application.yml:
spring:mail:username: 742891270@qq.compassword: xxxxxxxxxxxxxxx(这个密码,下面有说是什么)host: smtp.qq.comprofiles:mail:smtp:ssl:enable: true
- 密码不是你的QQ密码,而是
是这个授权码
测试类中测试:
@SpringBootTest
class SpringBootTaskApplicationTests {@AutowiredJavaMailSenderImpl mailSender;//简单的发送邮件@Testvoid contextLoads() {SimpleMailMessage message = new SimpleMailMessage();message.setSubject("今晚打老虎");//发送邮件标题message.setText("多谢乌蝇哥");//正文message.setTo("742891270@qq.com");//发给谁message.setFrom("742891270@qq.com");//发送者this.mailSender.send(message);}}
2、复杂的邮件
//复杂的邮件@Testpublic void test02() throws Exception {MimeMessage mimeMessage = this.mailSender.createMimeMessage();//要用MimeMessage类型MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);//然后用helper操作helper.setSubject("今晚打老虎");//标题helper.setText("<b style='color:red'>多谢乌蝇哥</b>", true);//内容,第二个参数为true就开启html格式helper.setTo("742891270@qq.com");//发给谁helper.setFrom("742891270@qq.com");//发送者helper.addAttachment("1.jpg", new File("D:\\壁纸\\59e06a3543101.png"));helper.addAttachment("2.jpg", new File("D:\\壁纸\\336-160G2110S6.png"));this.mailSender.send(mimeMessage);}
Spring Boot从0开始学的个人笔记10 --任务相关推荐
- Spring Boot从0开始学的个人笔记 2 -- 配置文件
1.YAML文件配置属性 这个YAML呢,是一个配置的东西,刚开始没有的,要自己手动创建才可以,而且名字是固定的:application.yml### 2.基本语法:##### ①普通的值v: 值空 ...
- Spring Boot从0开始学的个人笔记11 --安全security
一.简述 spring security是spring家族的一个安全认证的东西,在spring boot中只要导入maven就行了.该功能可以认证安全登陆的问题,通过连接数据库,拿到用户和密码后验证. ...
- SpringBoot2.0(一):【重磅】Spring Boot 2.0权威发布
就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...
- (转)Spring Boot 2(一):【重磅】Spring Boot 2.0权威发布
http://www.ityouknow.com/springboot/2018/03/01/spring-boot-2.0.html 就在今天Spring Boot2.0.0.RELEASE正式发布 ...
- 终于把 Spring Boot 3.0 写成书了!
大家好,我是R哥. 我的新书<Spring Boot 3 核心技术与最佳实战>打磨一年多,今天终于上市了,定价 158 元,今天刚上市搞 5 折促销,80 元不到上车,这可能是全网最便宜的 ...
- Java Spring Boot 3.0.0 RC1 震撼登场!
备受期待的Spring Boot 3.0.0 RC1现在已经推出. 微信搜索关注<Java学研大本营>,加入读者群,分享更多精彩 Phil Webb两天前在Spring博客上宣布,备受期待 ...
- 使用最新版(2020)IntelliJ IDEA 新建 Spring Boot 2.0 项目
使用最新版(2020)IntelliJ IDEA 新建 Spring Boot 2.0 项目 一:创建项目,并添加相应依赖 新建Project,并指定Project为Spring Initializr ...
- spring boot 2.0 源码分析(二)
在上一章学习了spring boot 2.0启动的大概流程以后,今天我们来深挖一下SpringApplication实例变量的run函数. 先把这段run函数的代码贴出来: /*** Run the ...
- Spring Boot 3.0.0 发布第一个里程碑版本M1,你的 Java 升到 17 了吗?
欢迎关注方志朋的博客,回复"666"获面试宝典 文章来源:程序猿DD 2022年1月20日,Spring官方发布了Spring Boot 3.0.0的第一个里程碑版本M ...
最新文章
- python交互式命令_在python中运行交互式命令
- 基于MFC相机采集的实现与采集回调函数的应用实例
- php if require,关于php:required_if Laravel 5验证
- 百度地图之添加覆盖物
- 石头剪刀布 -2013编程之美全国测试赛 每日一练
- 解码(五):sws_getContext和sws_scale像素格式和尺寸转换函数详解
- javascript深入浅出——学习笔记(六种数据类型和隐式转换)
- 一致性哈希算法及其应用
- java中的@Override标签
- 直觉模糊有计算机知识嘛,直觉模糊集理论及应用 上册
- 2021 年“泰迪杯”数据分析技能赛 B 题 肥料登记数据分析
- 怎么在档案写自己的计算机水平,关于个人档案怎么写范文
- 数论-------数的倍数
- 紫罗兰永恒花园rust简谱_Letter《紫罗兰永恒花园》4.5话ED 简谱
- V831——脱机实现通信行程卡识别
- 如何在Flatter中以正确的方式存储登录凭证
- ABB机器人动作监控和无动作执行的使用
- 项目管理办公室(PMO)和项目经理(PM)的区别
- pdf转换成word转换器在线使用效果
- xctf misc基础题