当前位置:网站首页>异步、邮件、定时三大任务
异步、邮件、定时三大任务
2022-07-03 00:46:00 【奔走的王木木Sir】
异步任务
创建
service包,在其下创建类AsyncService伪造正在处理数据,导致线程延时,模拟同步等待。
package com.hxl.service; import org.springframework.stereotype.Service; @Service public class AsyncService { public void hello(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("数据处理中...."); } }创建
controller包,在其下创建类AsyncControllerpackage com.hxl.controller; import com.hxl.service.AsyncService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class AsyncController { @Autowired AsyncService asyncService; @RequestMapping("/hello") public String hello(){ asyncService.hello(); return "没毛病了"; } }访问测试
http://localhost:8080/hello,此时可以发现网页等待3秒之后会出现没毛病了的字样。这就是同步等待的情况。为了让用户体验好,先让用户得到消息,然后后台使用多线程的方式处理结果。我们需要加一个注解,这样,Springboot就会开一个线程池,进行调用。
//告诉Spring这是一个异步方法 @Async public void hello(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("数据处理中...."); }还需要在主启动程序上添加一个注解,让异步注解开启
@EnableAsync //开启异步注解功能 @SpringBootApplication public class SpringbootAsyncApplication { public static void main(String[] args) { SpringApplication.run(SpringbootAsyncApplication.class, args); } }此时测试发现,网页会瞬间打开,但是后台数据会持续进行,直到数据处理结束。
邮件任务
引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>在其中可以看到相关的邮件发送依赖

查看
JavaMailSenderImpl找到之后,看配置文件也就是properties下的,我们可以看到相关的配置将QQ邮箱里面的服务打开,我们使用IMAP/SMTP服务

配置文件
[email protected] spring.mail.password=自己的QQ授权码 spring.mail.host=smtp.qq.com spring.mail.properties.mail.smtp.ssl.enable=true测试
package com.hxl; 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 SpringbootAsyncApplicationTests { @Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { //一个简单的邮件 SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("明天放假"); message.setText("直接10天小长假"); message.setTo("[email protected]"); message.setFrom("[email protected]"); mailSender.send(message); } @Test public void contextLoads2() throws MessagingException { //一个复杂的邮件 MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("明天放假"); helper.setText("<b style='color:red'>你想啥呢</b>",true); //发送附件 helper.addAttachment("1.jpg",new File("文件的路径")); helper.addAttachment("2.jpg",new File("比如:D:\\2.jpg")); helper.setTo("[email protected]"); helper.setFrom("[email protected]"); mailSender.send(mimeMessage); } }然后就可以看到我们的邮件发送成功了。
定时任务
TaskExecutor接口
TaskScheduler接口
两个注解:
- @EnableScheduling //开启定时功能的注解,在主启动类中
- @Scheduled //什么时候执行
测试
创建
Service包,创建类ScheduledServicepackage com.hxl.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class ScheduledService { //在一个特定的时间执行这个方法 //cron表达式 //秒 分 时 日 月 周几 //下面这句话就是从星期一到星期日,的0秒 @Scheduled(cron = "0 * * * * 0-7") public void hello(){ System.out.println("hello,你被执行了"); } }在主程序上增加@EnableScheduling 开启定时任务功能
package com.hxl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @EnableAsync //开启异步注解功能 @EnableScheduling //开启定时功能的注解 @SpringBootApplication public class SpringbootAsyncApplication { public static void main(String[] args) { SpringApplication.run(SpringbootAsyncApplication.class, args); } }然后就可以了
cron表达式
cron:https://baike.baidu.com/item/cron/10952601?fr=aladdin
cron的生成:https://www.bejson.com/othertools/cron/
边栏推荐
- excel表格计算时间日期的差值,并转化为分钟数
- 【日常训练】871. 最低加油次数
- FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
- Leetcode-934: the shortest Bridge
- ROS2之ESP32简单速度消息测试(极限频率)
- leetcode-934:最短的桥
- 2022中国3D视觉企业(引导定位、分拣场景)厂商名单
- Meaning of Tencent cloud free SSL certificate extension file
- [AUTOSAR five methodology]
- Basic use of sringcloud & use of component Nacos
猜你喜欢

用Go+绘制爱心给心爱的她表白

Initial order of pointer (basic)

电话网络问题

How to convert Quanzhi a40i/t3 to can through SPI

Win10 can't be installed in many ways Problems with NET3.5

【AutoSAR 一 概述】

1.12 - Instructions
![[case sharing] let the development of education in the new era advance with](/img/11/af88d16dc66f00840cbfc5ba5d68bd.jpg)
[case sharing] let the development of education in the new era advance with "number"

【爱死机】《吉巴罗》被忽略的细节

瑞萨电子RZ/G2L开发板上手评测
随机推荐
Infrared thermography temperature detection system based on arm rk3568
正确甄别API、REST API、RESTful API和Web Service之间的异同
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
ROS2之ESP32简单速度消息测试(极限频率)
数学建模之线性规划(含MATLAB代码)
递归处理组织的几种情况
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
KingbaseES ALTER TABLE 中 USING 子句的用法
数据分析思维分析犯法和业务知识——分析方法(一)
Test shift right: Elk practice of online quality monitoring
Basic use of sringcloud & use of component Nacos
Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
First hand evaluation of Reza electronics rz/g2l development board
Initial order of pointer (basic)
Key detection and sinusoidal signal output developed by Arduino
[shutter] image component (cached_network_image network image caching plug-in)
【AutoSAR 一 概述】
leetcode-224:基本计算器
excel去除小数点后面的数据,将数字取整