当前位置:网站首页>异步、郵件、定時三大任務
异步、郵件、定時三大任務
2022-07-03 01:05: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/
边栏推荐
- tail -f 、tail -F、tailf的区别
- 1.11 - 总线
- Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
- 比较版本号
- Meaning of Tencent cloud free SSL certificate extension file
- 1.12 - 指令
- 指针初阶(基础)
- leetcode:701. 二叉搜索树中的插入操作【bst的插入】
- Rust string slicing, structs, and enumeration classes
- 这不平凡的两年,感谢我们一直在一起!
猜你喜欢

Hdu3507 (slope DP entry)

1.11 - bus

excel IF公式判断两列是否相同

matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决

1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】

What is needed to develop a domestic arm intelligent edge computing gateway

Vulkan performance and refinement

Infrared thermography temperature detection system based on arm rk3568

Rust string slicing, structs, and enumeration classes

Thank you for being together for these extraordinary two years!
随机推荐
The difference between relational database and non relational database
What is needed to develop a domestic arm intelligent edge computing gateway
Vulkan practice first bullet
【AutoSAR 八 OS】
拥抱平台化交付的安全理念
leetcode-224:基本计算器
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
lex && yacc && bison && flex 配置的问题
删除有序链表中重复的元素-II
1038 Recover the Smallest Number
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem
leetcode:701. 二叉搜索树中的插入操作【bst的插入】
Infrared thermography temperature detection system based on arm rk3568
关于Fibonacci数列
excel表格计算时间日期的差值,并转化为分钟数
1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
mysql 多表联合删除
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size